Basic coding question

  • Thread starter Thread starter JohnH
  • Start date Start date
J

JohnH

I'm looking for a method of prompting a user to enter a
value and using that value in a simple calculation. For
example, an inputbox would ask for an integer and subtract
that number from an existing value in another control.
Right now I have a command button that fires an inbox
command to prompt the user. Now I need to handle the
input (or find a better way to carry out this whole
procedure). Thanks!
 
JohnH said:
I'm looking for a method of prompting a user to enter a
value and using that value in a simple calculation. For
example, an inputbox would ask for an integer and subtract
that number from an existing value in another control.
Right now I have a command button that fires an inbox
command to prompt the user. Now I need to handle the
input (or find a better way to carry out this whole
procedure). Thanks!

It would be something like:

Dim strInput As String

strInput = InputBox("Enter a number:")

If IsNumeric(strInput) Then
Me!txtOtherNumber =
Me!txtOtherNumber - CLng(strInput)
Else
MsgBox "That's not a valid number!"
End If

That's pretty rough, but you haven't given enough information for
anything more detailed.
 
This code does the job. With one exception. If you enter
a value in the inputbox and click ok, it works fine.
However, if you close the dialog box or cancel, it results
in a run-time '13 type mismatch error. It doesn't seem to
like the mix of str and int on line 4. Any way I can make
this work if the inputbox is canceled. Thanks again for
the code sample

Where txtSilo3 contains an existing integer value

Dim strMsg As String
Dim intInputValue As Integer
strMsg = "Enter Value"
intInputValue = inputbox(Prompt:=strMsg, Title:="Here")
txtSilo3 = txtSilo3 - intInputValue
 
JohnH said:
This code does the job. With one exception. If you enter
a value in the inputbox and click ok, it works fine.
However, if you close the dialog box or cancel, it results
in a run-time '13 type mismatch error. It doesn't seem to
like the mix of str and int on line 4. Any way I can make
this work if the inputbox is canceled. Thanks again for
the code sample

Where txtSilo3 contains an existing integer value

Dim strMsg As String
Dim intInputValue As Integer
strMsg = "Enter Value"
intInputValue = inputbox(Prompt:=strMsg, Title:="Here")
txtSilo3 = txtSilo3 - intInputValue

Look again at the code sample I posted. It handles the situation you
describe, where your version does not. Be aware that InputBox() returns
a string; if the user clicks the Cancel button it returns a zero-length
string. Obviously, in that case or if the user enters some non-numeric
value, assigning the return value directly to an integer variable will
raise an error, since such values can't be converted to an integer.
That's why I used the IsNumeric() function to test for that.
 
Back
Top