You see, if the user presses cancel, vbNullString is returned. However if they press OK, the empty string ("") is sent back. But in Visual Basic, vbNullString equates to an empty string, so you can't compare the two - even though in reality, they're completely different.
However you can use the StrPtr ('string pointer') function to determine whether the return string is indeed a vbNullString - as by definition, a vbNullString 'pointer' is zero.
Private Sub Form_Load()
Dim strInput
strInput = InputBox("Enter something:")
If StrPtr(strInput) = 0 Then
MsgBox "You pressed Cancel button!"
ElseIf strInput = "" Then
MsgBox "You press OK button with blank value!"
End If
End
End Sub
No comments:
Post a Comment