InputBox

benjamin.weizmann

Registered User.
Local time
Today, 06:38
Joined
Aug 30, 2016
Messages
78
hi,
I have this assignment:
msgzeropro = Nz(InputBox(Message, Title), 0)

if the user clicked on canceled , I got error type mismatch
how can I avoid it?
in addition I would like catch the knowledge if he clicked cancel for the last code.

thanks
Ben
 
If you include the error number in your error handling routines, its easy to provide exceptions for cases like this.

Type mismatch (error 13) normally indicates the Nz alternative isn't valid for that field.
So normally I would say msgzeropro isn't a number but a string?
If its text, try

Code:
msgzeropro = Nz(InputBox(Message, Title), "")

However in the case of an input box being cancelled, use code similar to this

Code:
Sub UpdateSelectedGradeAverageChanges()

On Error GoTo Err_Handler
    
    Some code here ....
    
    lngLatestSession = Nz(InputBox("Enter the latest session you want to use  e.g. 201405", "Enter Latest Session"), 0)
    
    If lngLatestSession = 0 Then
        MsgBox "Latest session not entered", vbCritical, "Critical error"
        Exit Function
    End If
    
    ...More code here ...
    
Exit_Handler:
    Exit Sub

Err_Handler:
    [B][COLOR="Red"]If Err = 13 Then Resume Next 'needed if user cancels input box[/COLOR][/B]
    
    MsgBox "Error " & Err.Number & " : This procedure has been aborted - " & vbNewLine & Err.Description
    Resume Exit_Handler

End Sub

You can do something similar for the other point:
in addition I would like catch the knowledge if he clicked cancel for the last code.
 
If you include the error number in your error handling routines, its easy to provide exceptions for cases like this.

Type mismatch (error 13) normally indicates the Nz alternative isn't valid for that field.
So normally I would say msgzeropro isn't a number but a string?
If its text, try

Code:
msgzeropro = Nz(InputBox(Message, Title), "")
However in the case of an input box being cancelled, use code similar to this

Code:
Sub UpdateSelectedGradeAverageChanges()

On Error GoTo Err_Handler
    
    Some code here ....
    
    lngLatestSession = Nz(InputBox("Enter the latest session you want to use  e.g. 201405", "Enter Latest Session"), 0)
    
    If lngLatestSession = 0 Then
        MsgBox "Latest session not entered", vbCritical, "Critical error"
        Exit Function
    End If
    
    ...More code here ...
    
Exit_Handler:
    Exit Sub

Err_Handler:
    [B][COLOR=red]If Err = 13 Then Resume Next 'needed if user cancels input box[/COLOR][/B]
    
    MsgBox "Error " & Err.Number & " : This procedure has been aborted - " & vbNewLine & Err.Description
    Resume Exit_Handler

End Sub
You can do something similar for the other point:


thanks u
but question: this inputbox expects to accept long ... if the user type "hjhjhjh" - will the same error 13 jump up?

I would like to alert the user he typed illegal type of variable
 
thanks u
but question: this inputbox expects to accept long ... if the user type "hjhjhjh" - will the same error 13 jump up?

I would like to alert the user he typed illegal type of variable

Yes it should but the easy way to test is to try it yourself...!
Adapt the code I've used in the If section to handle such scenarios
 
I've been doing something similar, and found this via Google?

https://social.msdn.microsoft.com/F...er-than-detecting-null-input-?forum=accessdev

as you do not take into account a cancel or close of the inputbox.?
Code:
Sub Test_inputbox()
Dim s As String
s = inputbox("What do you want to find?")
If StrPtr(s) = 0 Then
     MsgBox " user cancelled"
Else
    If Len(s) = 0 Then
        MsgBox "user Pressed OK, but there was no entry in the input control"
   Else
       MsgBox "User pressed OK and there was something in the input control"
   End If
End If
End Sub
 
Thanks Gasman

I like that code snippet and will include something like it in my own future code
 

Users who are viewing this thread

Back
Top Bottom