Checking calculations and showing message

T

Tony Williams

I am trying to write code that checks the input in controls on a form and if
they are incorrect to show a message.Here is my code which I have in the
Before Update Event of the form

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Cash_Price As Integer
Dim Deposit As Integer
Dim Amount_of_Credit As Integer
Dim Charges As Integer
Dim Initial_Balance_Outstanding As Integer
Dim Payments_Made As Integer
Dim Current_Balance_Outstanding As Integer
Dim strMessage As String

If Cash_Price - Deposit <> Amount_of_Credit Then
strMessage = strMessage & "The Amount of Credit " & _
"does not equal the Cash Price less Deposit" & vbCrLf & _
"It should be " & Amount_of_Credit
End If
If Len(strMessage) > 0 Then
If MsgBox(strMessage & " - Do you want to accept the error(s)?", _
vbYesNo, "Calculation Error") = vbNo Then
Cancel = True
End If
End If
End Sub

Firstly I realise I have problems with the control names having spaces but I
have inherited this database.
The code at the moment includes other controls that I want to check but I am
trying to get the first one right. The user will input the Cash Price,
Deposit and Amount of Credit from a paper form. I want the code to check
that the Amount of Credit = Cash Price Less Deposit and if it doesn't show a
message asking the user whether or not to accept the error.

This code doesn't work although I have compiled it and there are no errors.
Anyone help?
Thanks
Tony
 
S

Stuart McCall

Tony Williams said:
I am trying to write code that checks the input in controls on a form and
if they are incorrect to show a message.Here is my code which I have in the
Before Update Event of the form

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Cash_Price As Integer
Dim Deposit As Integer
Dim Amount_of_Credit As Integer
Dim Charges As Integer
Dim Initial_Balance_Outstanding As Integer
Dim Payments_Made As Integer
Dim Current_Balance_Outstanding As Integer
Dim strMessage As String

If Cash_Price - Deposit <> Amount_of_Credit Then
strMessage = strMessage & "The Amount of Credit " & _
"does not equal the Cash Price less Deposit" & vbCrLf & _
"It should be " & Amount_of_Credit
End If
If Len(strMessage) > 0 Then
If MsgBox(strMessage & " - Do you want to accept the error(s)?", _
vbYesNo, "Calculation Error") = vbNo Then
Cancel = True
End If
End If
End Sub

Firstly I realise I have problems with the control names having spaces but
I have inherited this database.
The code at the moment includes other controls that I want to check but I
am trying to get the first one right. The user will input the Cash Price,
Deposit and Amount of Credit from a paper form. I want the code to check
that the Amount of Credit = Cash Price Less Deposit and if it doesn't show
a message asking the user whether or not to accept the error.

This code doesn't work although I have compiled it and there are no
errors. Anyone help?
Thanks
Tony

Cash_Price, Deposit, Amount_of_Credit etc. are all declared as variables,
yet nothing is assigned to them. So they will all have the value 0.

Are they really the names of controls, or the names of fields in the form's
recordsource? If so, delete all the Dim statements except strMessage (which
is actually being used). If, as you say, the control names have spaces in
their names, make changes like changing Cash_Price into [Cash Price]
(whenever field or control names contain spaces, surround with brackets).

Other than that the code looks good and won't be causing trouble.
 
T

Tony Williams

Thanks Stuart. They are names of controls and I followed your instructions
by deleting the Dim statements and putting all the control names in brackets
and everything worked fine.
Thanks again
Tony
Stuart McCall said:
Tony Williams said:
I am trying to write code that checks the input in controls on a form and
if they are incorrect to show a message.Here is my code which I have in
the Before Update Event of the form

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Cash_Price As Integer
Dim Deposit As Integer
Dim Amount_of_Credit As Integer
Dim Charges As Integer
Dim Initial_Balance_Outstanding As Integer
Dim Payments_Made As Integer
Dim Current_Balance_Outstanding As Integer
Dim strMessage As String

If Cash_Price - Deposit <> Amount_of_Credit Then
strMessage = strMessage & "The Amount of Credit " & _
"does not equal the Cash Price less Deposit" & vbCrLf & _
"It should be " & Amount_of_Credit
End If
If Len(strMessage) > 0 Then
If MsgBox(strMessage & " - Do you want to accept the error(s)?", _
vbYesNo, "Calculation Error") = vbNo Then
Cancel = True
End If
End If
End Sub

Firstly I realise I have problems with the control names having spaces
but I have inherited this database.
The code at the moment includes other controls that I want to check but I
am trying to get the first one right. The user will input the Cash Price,
Deposit and Amount of Credit from a paper form. I want the code to check
that the Amount of Credit = Cash Price Less Deposit and if it doesn't
show a message asking the user whether or not to accept the error.

This code doesn't work although I have compiled it and there are no
errors. Anyone help?
Thanks
Tony

Cash_Price, Deposit, Amount_of_Credit etc. are all declared as variables,
yet nothing is assigned to them. So they will all have the value 0.

Are they really the names of controls, or the names of fields in the
form's recordsource? If so, delete all the Dim statements except
strMessage (which is actually being used). If, as you say, the control
names have spaces in their names, make changes like changing Cash_Price
into [Cash Price] (whenever field or control names contain spaces,
surround with brackets).

Other than that the code looks good and won't be causing trouble.
 

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