Inputbox Prompt & Incorrect Responses (1 Viewer)

wilderfan

Registered User.
Local time
Today, 10:01
Joined
Mar 3, 2008
Messages
172
I am using an Inputbox in a Function.

The user is supposed to enter a number between zero and some other (positive) number.

I want to check to ensure that the response falls within this range.

If the user keys in a number outside the range, I want to show a message, but I do NOT want to exit the Function.

Is there a way to push the focus back to the Inputbox without having to leave the Function?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 10:01
Joined
Aug 30, 2003
Messages
36,118
This type of thing?

Code:
Dispatch:      '###send driver out in car###
  intCarNo = InputBox(prompt:="Enter Vehicle Number", title:="Enter Vehicle Num", Default:=0)

  If intCarNo = "" Then    '###user clicked cancel###
    If booShopped = True And Me.txtShift = 3 Then
      MsgBox "Lease drivers must be put in another car"
      GoTo Dispatch
    Else
      GoTo ExitHandler
    End If
  End If

In this instance, inputting a number is only required under certain circumstances, but the key is using the GoTo to redirect code back to the InputBox. You would test the value of the input.
 

wilderfan

Registered User.
Local time
Today, 10:01
Joined
Mar 3, 2008
Messages
172
I will give that a try.

Thank you, Paul.
 

MarkK

bit cruncher
Local time
Today, 10:01
Joined
Mar 17, 2004
Messages
8,178
You can use recursion, so if the result fails you recall the same routine from inside the routine. Consider the following code...
Code:
Function GetResponse(Optional AdditionalMessage As Variant = Null) As Long
[COLOR="Green"]'  This recursive function returns -1 if the user cancels the operation
'  ...or a long between 0 and 10
'  This function calls itself if the user input was out of range
[/COLOR]   Dim tmp As String
[COLOR="Green"]   'present the InputBox
[/COLOR]   tmp = InputBox(AdditionalMessage + vbCrLf & "Enter some long integer value > 0 and < 10")
   If tmp = "" Then
[COLOR="Green"]      'user cancelled[/COLOR]
      GetResponse = -1
   Else
      If tmp > 0 And tmp < 10 Then
[COLOR="Green"]         'user response was in range[/COLOR]
         GetResponse = tmp
      Else
[COLOR="Green"]         'user response was out of range, so before exiting the current routine
         '...recall the current routine...[/COLOR]
         GetResponse = GetResponse("Your previous response was out of range...")
      End If
   End If
      
End Function

Sub TestPilot()
   Debug.Print GetResponse
End Sub
 

Taruz

Registered User.
Local time
Today, 17:01
Joined
Apr 10, 2009
Messages
168
Hi..

maybe this could be an alternative..



Code:
Private Sub CommandButtonName_Click()
Dim trz
trz = InputBox(" Enter only positive numbers!", "Info")
If trz <> "" Then
    If IsNumeric(trz) = True And trz >= 0 Then
        MsgBox "approved."
        Else
        Call CommandButtonName_Click 'type the name of command button
    End If
End If
End Sub
 

wilderfan

Registered User.
Local time
Today, 10:01
Joined
Mar 3, 2008
Messages
172
Thanks for the additional responses.

For the time being, I've used the "GoTo" option. It seems to work fine.

Here is the file I've been working on. Its purpose is to save names in a table. The user keys the name into a form's text box and then the VBA code asks the user to indicate what part of the name should be saved in the First Name field of the table.

I'm sure the VBA code could be streamlined or improved in some way.

I'd be interested to hear your comments / suggestions.
 

Attachments

  • Input Name.zip
    24.8 KB · Views: 68

Users who are viewing this thread

Top Bottom