mgae2m
mgae2m

Reputation: 1142

InputBox Cancel different from vbNullString (null)

I want to use an InputBox for checking password.

If the user presses "OK" without data entry, the InputBox should run again, and if the user pressed "Cancel" or "ESC", corporate subroutine have exit.

How can I recognize "ESC" or "Cancel" input, different from just "null" or empty?

And how specially do that for my customize created Input box with API, so for changing key-pressed shown as "*" in password input, named as InputBoxDk:

'API functions to be used
Private Declare Function CallNextHookEx _
Lib "user32" ( _
ByVal hHook As Long, _
ByVal ncode As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long

Private Declare Function GetModuleHandle _
Lib "kernel32" _
Alias "GetModuleHandleA" ( _
ByVal lpModuleName As String) _
As Long

Private Declare Function SetWindowsHookEx _
Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) _
As Long

Private Declare Function UnhookWindowsHookEx _
Lib "user32" ( _
ByVal hHook As Long) _
As Long

Private Declare Function SendDlgItemMessage _
Lib "user32" Alias "SendDlgItemMessageA" ( _
ByVal hDlg As Long, _
ByVal nIDDlgItem As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long

Private Declare Function GetClassName _
Lib "user32" _
Alias "GetClassNameA" ( _
ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) _
As Long

Private Declare Function GetCurrentThreadId _
Lib "kernel32" () _
As Long


Private Declare Sub sapiSleep Lib "kernel32" _
    Alias "Sleep" _
    (ByVal dwMilliseconds As Long)
'Constants to be used in our API functions
Private Const EM_SETPASSWORDCHAR = &HCC
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private Const HC_ACTION = 0

Private hHook As Long

Public Function NewProc(ByVal lngCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Dim RetVal
Dim strClassName As String, lngBuffer As Long

If lngCode < HC_ACTION Then
    NewProc = CallNextHookEx(hHook, lngCode, wParam, lParam)
    Exit Function
End If

strClassName = String$(256, " ")
lngBuffer = 255

If lngCode = HCBT_ACTIVATE Then 'A window has been activated
    RetVal = GetClassName(wParam, strClassName, lngBuffer)
    If Left$(strClassName, RetVal) = "#32770" Then 'Class name of the Inputbox
         'This changes the edit control so that it display the password character *.
         'You can change the Asc("*") as you please.
        SendDlgItemMessage wParam, &H1324, EM_SETPASSWORDCHAR, Asc("*"), &H0
    End If
End If

 'This line will ensure that any other hooks that may be in place are
 'called correctly.
CallNextHookEx hHook, lngCode, wParam, lParam

End Function

'// Make it public = avail to ALL Modules
 '// Lets simulate the VBA Input Function
Public Function InputBoxDK(Prompt As String, Optional Title As String, _
Optional Default As String, _
Optional Xpos As Long, _
Optional Ypos As Long, _
Optional Helpfile As String, _
Optional Context As Long) As String

Dim lngModHwnd As Long, lngThreadID As Long

 '// Lets handle any Errors JIC! due to HookProc> App hang!
On Error GoTo ExitProperly
lngThreadID = GetCurrentThreadId
lngModHwnd = GetModuleHandle(vbNullString)

hHook = SetWindowsHookEx(WH_CBT, AddressOf NewProc, lngModHwnd, lngThreadID)
If Xpos Then
    InputBoxDK = InputBox(Prompt, Title, Default, Xpos, Ypos, Helpfile, Context)
Else
    InputBoxDK = InputBox(Prompt, Title, Default, , , Helpfile, Context)
End If

ExitProperly:
UnhookWindowsHookEx hHook

End Function

Sub TestDKInputBox() 
Dim x 

x = InputBoxDK("Type your password here.", "Password Required") 
If x = "" Then End 
If x <> "yourpassword" Then 
    MsgBox "You didn't enter a correct password." 
    End 
End If 

MsgBox "Welcome Creator!", vbExclamation 

End Sub 

Code reference: http://www.ozgrid.com

Upvotes: 0

Views: 4424

Answers (3)

Mathieu Guindon
Mathieu Guindon

Reputation: 71167

How can I recognize "ESC" or "Cancel" input, different from just "null" or empty?

It's not quite documented, but a cancelled inputbox does not return just any "" empty string:

Debug.Print StrPtr("") ' returns some address
Debug.Print StrPtr(vbNullString) ' returns 0

The problem is that comparing vbNullString with "" will return True.

So the trick is to validate the StrPtr of the returned value:

Dim result As String
result = InputBox(...)

If StrPtr(result) = 0 Then
    ' definitely cancelled
    Exit Sub
End If

If result = vbNullString Then
    ' legit empty string
    '...
Else
    ' non-empty string
    '...
End If

This solution works in VB6, as well as in any VBA host.

Upvotes: 0

Kostas K.
Kostas K.

Reputation: 8518

The Application.InputBox() returns False on Cancel or Esc, where InputBox() returns "".

Sub ProcedureName()

    Dim response As Variant
    Do Until Len(Trim(response)) > 0
        response = Application.InputBox("Type something: ", "InputBox")
    Loop

    If response = vbFalse Then ' in case the use press "Cancel"
        MsgBox "Pressed Cancel"
    End If
End Sub

Upvotes: 5

Slai
Slai

Reputation: 22866

InputBox("prompt", "title", "default")

I can't test it, but If the user clicks OK the result should be "default". If something else is clicked the result should be "". If the user clears the input and clicks OK, the result is "" too. There is no way to make sure that OK was clicked in a InputBox, so you might need a custom UserForm for that.

Upvotes: 0

Related Questions