1

I have a non-VBA program that takes user input from a series of InputBoxes activated by a loop. I need it so that, if the user clicks the "cancel"button, it breaks out of the loop. Unfortunately, the "cancel" button doesn't do this, and instead returns an empty string, which then gets passed to my input validation function, fails, and gives a failed validation message. Clicking "ok" on the validation message just pops up the InputBox again. This creates an infinite loop of cancel > ok > cancel > ok. I need it to work so that, when the user clicks "cancel", the InputBox closes without sending any input. Is this possible? Thanks for any help :)

Here is my code:

'Assign value to variable
strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth)


If Validation(strAmountInput) = True Then
    'Set Value for dblAmount
    dblAmount = CDbl(strAmountInput)

    'Add monthly rainfall amount to array, using i as index
    dblMonthlyRain(i) = dblAmount

    'Add item displaying monthly rainfall to listbox
    lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & CStr(dblMonthlyRain(i)))
Else
    'Prevent loop from advancing if validation fails, forces user to enter valid input before moving on
    i -= 1

End If

_______________________________________________________________________________________

Private Function Validation(Amount As String) As Boolean

    'Validate user input for InputBox
    If IsNumeric(Amount) = False Then
        MessageBox.Show("Please enter numbers only")
        Return False
    Else
        Return True
    End If

End Function
Ethan Malloy
  • 555
  • 1
  • 4
  • 16
  • `If String.IsNullOrEmpty(strAmountInput) Then Exit Do` or `Exit For` or `Exit While`. Maybe, get rid of the quite ugly InputBox and build a Form dedicated to this task. – Jimi Jun 29 '19 at 11:02

2 Answers2

0

Based on this answer the InputBox returns a zero-length string when Cancel is pressed, so you must check that.

strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth)

If strAmountInput = "" Then
   'Cancel was pressed or no value was entered
Else

  If Validation(strAmountInput) = True Then
     'Set Value for dblAmount
     dblAmount = CDbl(strAmountInput)

     'Add monthly rainfall amount to array, using i as index
     dblMonthlyRain(i) = dblAmount

     'Add item displaying monthly rainfall to listbox
     lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & 
     CStr(dblMonthlyRain(i)))
  Else
     'Prevent loop from advancing if validation fails, forces user to enter valid input before moving on
      i -= 1

  End If
End If
jme
  • 16
  • 4
0

I need it to work so that, when the user clicks "cancel", the InputBox closes without sending any input. Is this possible? Thanks for any help :)

The other answers address using a native InputBox function. This is an area where a custom form comes in handy. There are two steps to this:

  • Creating the form itself
  • Creating a handler routine top emulate the native InputBox call

Create a user form that looks like the InputBox: A title, label, text box and the two buttons ("OK" and "Cancel"). The advantage here is that you can do data validation on the fly in the text box (e.g. prevent non-numeric entries) and you can also disable the "OK" button until a valid entry exists in the text box. This is a good user interface design consideration - trains the user to do correct input.

Create a Function that takes similar arguments to InputBox. Within this function you instantiate the user form, stuff it with any data required at first (e.g. set the label to the question), show the user form, await user input and then grab any responses.

The part that you are currently interested in: what if they Cancel?

This is easier in VBA with the Variant type, because you can return a Boolean or a String (or in a smart inputbox approach an integer!). However Vb.Net does not have this luxury.

To achieve this, add an additional parameter to your custom input box call ByRef IsCancelled as Boolean. When the user closes the form, your custom Function (the one that emulates the InputBox call) can then set the cancelled flag based on your custom form settings. An example of how you would use this in your main program:

Dim noUserInput as Boolean
noUserInput = False
strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth, noUserInput)
If Not no UserInput Then
    dblAmount = CDbl(strAmountInput)

    'Add monthly rainfall amount to array, using i as index
    dblMonthlyRain(i) = dblAmount

    'Add item displaying monthly rainfall to listbox
    lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & CStr(dblMonthlyRain(i)))
End If

Obviously, if the user cancelled, then there must be a reason, so putting them in an endless loop to keep asking for input would be annoying.

What I have suggested may seem like some extra work over the other answers. However, this approach is re-useable and, with experience, you can adapt it to multiple return types, making a useful permanent utility for many programs.

The only code examples I have of this are on another machine, and in VBA (see my note above about the use of a Variant type that makes this easier).

AJD
  • 2,400
  • 2
  • 12
  • 22