Calculator Decimal Issue

Learn2010

Registered User.
Local time
Today, 17:31
Joined
Sep 15, 2010
Messages
415
I created a calculator for a sales program. The code behind each button is:

START OF CODE

Private Sub btn1_Click()
Amount.SetFocus
If Amount > 0 Then
Amount = [Amount] & "1"
Else
Amount = 1
End If
btnEnterItem.Enabled = True
btn1.SetFocus
End Sub


END OF CODE

Amount is a text field. Does anyone know how to stop all the number buttons from working if there are two numbers to the right of the decimal point? Or, to put it another way, how can I programmatically know when this happens so I can program it to stop?

Thank you.
 
I'm confused by your code. You said that amount was text; if so, then your code would have to look like this

Private Sub btn1_Click()
Amount.SetFocus
If Amount > "0" Then
Amount = Amount & "1"
Else
Amount = "1"
End If
btnEnterItem.Enabled = True
btn1.SetFocus
End Sub





Also, this line:
Amount = [Amount] & "1"

Let's say that the character "5" is in the variable amount and then hits this statement. You will concatenate a "1" to the "5" yielding "51" Is that what you intended?

As to evaluating a string, you could use the InStr() function to look for a decimal point


Did you really mean to say that amount is the name of a textbox control on your form? What type of data is the user allowed to enter into that text box? Numeric data only or alphanumeric?
 
Let's say that the character "5" is in the variable amount and then hits this statement. You will concatenate a "1" to the "5" yielding "51" Is that what you intended?

Yes.

Amount is a text field from the underlying table.
 
I'm still not clear as to what you want to have happen if someone enters a decimal point & characters after it. Could you please explain a little further?
 
The calculator is being used to enter the amount of money received from the customer. After entry is complete the sales person clicks on the “Calculate Change” button and the Change value is displayed on the screen.

The control [Amount] is a text field from the underlying table and holds the value being entered. In order for my calculator to work, it has to be a text field. When entering numbers the result is based on what is already in the control. The default value is 0. Any number you enter will displace the 0. After that it will concatenate any character entered into the field.

Let’s use 51.16 as the result needed. While entering that number, you click the 5, the 1, the decimal point, the 1, and the 6. You will then have 51.16 in the control. If you then, for whatever reason, enter 7, or whatever, you will have 51.167 displayed. As it stands, as long as you enter numbers, they will concatenate to the value in the control, such as 51.1678374, and so on.

What I am trying to accomplish is to have the program not to allow that control to accept more than two numbers to the right of the decimal point. Is that possible?

Thank you.
 
You should be able to use the InStr() function in conjunction with the len() function to test for the presence of the decimal point. If so, adjust the value back to having only two digits after the decimal point. The code would look something like this:

If InStr(Me.amount, ".") <> 0 And Len(Me.amount) - InStr(Me.amount, ".") > 2 Then
Me.amount = Left(Me.amount, InStr(Me.amount, ".") + 2)
End If
 
I will try this later in the day. I will get back with you tomorrow.

Thanks.
 
Thanks again for your help. I couldn't get it to work. But, it did get me to thinking. I did solve it another way. The amount ends up in another table. I set the Field Size to Decimal, Scale to 2, and Decimal Places to 2.
 
I'm glad you got to a solution; good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom