Determine If The User Pressed Cancel On InputBox

This code uses the StrPtr (string pointer) function, that return pointer to string.
If the user pressed cancel on the inputbox, the result from the inputbox will be pointer that points to Null (vbNullString Constant), that is equal to 0.
Note that empty string ("") is Not Null.

Form Code

Private Sub Form_Load()
    Dim str As String

    str = InputBox("Press OK or Cancel")

    If StrPtr(str) = 0 Then
       MsgBox "You pressed Cancel"
    End If

End Sub

Go Back