Test for Inputbox cancel

K

Kevin Sprinkel

Pressing Cancel from Inputbox returns a zero-length
string. Either you've declared astring as a numeric, or
Access has typed it for you based on your first listed
case.

The solution is to assign InputBox to a String or Variant;
test first for Cancel, and then do what you want with the
value of the string. Since there are only 2 cases, > 0
and not, you can use If:

Dim astring As Variant

astring = InputBox("Enter amount for each
payment: ", "Calculate Number of Payments")

If astring = "" Then
' User pressed Cancel
Exit Sub
End If

' Convert astring to a number
astring = Val(astring)

If astring > 0 Then
' Do some code here...

Else 'Entered illegal or negative number
' Do other code

End If

HTH
Kevin Sprinkel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top