InputBox Cancel
I have created an Inputbox to get the username entered but stuck with the cancel button
Private Sub Form_Load()
fsUserName = UCase(InputBox("Please En开发者_如何学JAVAter your name.", "User Name", _
"dc"))
If fsUserName = "" Then
MsgBox "No name Entered." & Chr(13) & Chr(13) & _
"You must enter a name.", vbExclamation, "ERROR"
Form_Load
ElseIf VarType(fsUserName) = 0 Then 'If cancel clicked
cmdQuit_Click
End If
Also is there a way that when the X button on the form is clicked it executes cmdQuit_Click so that if the userclicks the command button Quit or X ,the Quit script is run.In the quit script there are message boxes and cleanup.
You can use StrPtr()
to detect if cancel was clicked;
Dim fsUserName As String
fsUserName = InputBox("Please Enter your name.", "User Name", "dc")
If (StrPtr(fsUserName) = 0&) Then
MsgBox "Cancelled or X clicked"
ElseIf fsUserName = "" Then
MsgBox "No name Entered." & vbCr & "You must enter a name.", vbExclamation, "ERROR"
Else
fsUserName = UCase$(fsUserName)
MsgBox fsUserName
End If
If you want to do something when the form unloads you can use the Form_Unload
event or better the Form_QueryUnload
event which fires before the actual unload & allows you to cancel it.
It will also tell you why the form is unloading (UnloadMode
will be 0 if the red X is clicked)
Using "Unload Me
" will raise both of the events.
Edit: Calling Form_Load
in Form_Load
like that will eventually fill up the stack, better to use a loop to look for a missing username.
Alex's answer using StrPtr, which I assume works as advertised, is good as far as it goes, but the better advice (IMO) is to avoid InputBox altogether. You can easily create your own using a dialog-style form, a textbox, and a couple of buttons, and maybe an icon if you like.
Rolling your own gives you complete flexibility and predictability, and once you have it you can use it in any future projects.
Excel InputBox
and Application.InputBox
are different functions.
Sub GetValue2()
Dim Monthly As Variant
Monthly = Application.InputBox _
(Prompt:="Podaj wysokość miesięcznej wypłaty:", _
Type:=1)
If Monthly = False Then Exit Sub
MsgBox "Zarobki rocznie: " & Monthly * 12
End Sub
Try this...
Name = Application.InputBox("Please enter Name")
If Name = "False" Then
MsgBox " Your message here"
Exit Sub
End If
精彩评论