Microsoft Office Tutorials and References
In Depth Information
Chapter 12: Custom Dialog Box Alternatives
The VBA InputBox function
The syntax for VBA’s InputBox function is
InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile, context])
h prompt : Required. The text displayed in the InputBox.
h title : Optional. The caption of the InputBox window.
h default : Optional. The default value to be displayed in the dialog box.
h xpos , ypos : Optional. The screen coordinates of the upper-left corner of the window.
h helpfile , context : Optional. The help file and help topic.
The InputBox function prompts the user for a single piece of information. The function always
returns a string, so your code may need to convert the results to a value.
The prompt may consist of up to 1,024 characters. In addition, you can provide a title for the
dialog box and a default value and specify its position on the screen. You can also specify a custom
Help topic; if you do, the input box includes a Help button.
The following example, which generates the dialog box shown in Figure 12-1, uses the VBA
InputBox function to ask the user for his full name. The code then extracts the first name and
displays a greeting in a message box.
Figure 12-1: VBA’s InputBox function at work.
Sub GetName()
Dim UserName As String
Dim FirstSpace As Integer
Do Until UserName <> “”
UserName = InputBox(“Enter your full name: “, _
“Identify Yourself”)
FirstSpace = InStr(UserName, “ “)
If FirstSpace <> 0 Then
UserName = Left(UserName, FirstSpace - 1)
End If
MsgBox “Hello “ & UserName
End Sub
Search JabSto ::

Custom Search