April 19, 2019
Hot Topics:

VB TIP: InputBox - OK or Cancel?

  • September 20, 2002
  • By Alan Young
  • Send Email »
  • More Articles »

You display an InputBox. But did the user press Cancel - or OK without entering anything? If you thought there was no way to tell the difference, you're wrong.

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.

And this code demonstrates how to do that.


Dim strInput As String

strInput = InputBox("Enter something:")

If StrPtr(strInput) = 0 Then
	MsgBox "You pressed cancel!"
End If

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date