type mismatch on mathematical calculation

  • Thread starter Thread starter taco
  • Start date Start date
T

taco

Hi everyone;

Thanks in advance for your help. This time the problem is like this. There
are two text box named "DClassLei" and "DClass" with "euro" format and 2
decimal places. Both of them are getting their value by a vb code regarding
different text box's values. There is another text box named "ticket" and
should get it's value as (DClassLei+DClass)/25. I wrote the code below but
first thing is, if only "DClassLei" or "DClass" has value "ticket" is not
getting it's value as 2 decimal places and if both "DClassLei" and "DClass"
have values, code is ending with type mismatch error.

Here is my unsuccessful code;

Private Sub DClass_LostFocus()
Dim dcl1 As String
Dim dcl2 As String
Dim tic As String

dcl1 = Me!DClassLei
dcl2 = Me!DClass
tic = (dcl1 + dcl2) / 25
Me!Ticket.SetFocus
Me!Ticket = tic
Me!New.SetFocus

End Sub
 
You're declaring dcl1 and dcl2 as String. If you're doing arithmetic with
them, they should be numeric.
 
Hi taco,

Dim dcl1 As double
Dim dcl2 As double
Dim tic As double

dcl1 = nz(Me!DClassLei,0)
dcl2 = nz(Me!DClass,0)
tic = (dcl1 + dcl2) / 25
Me!Ticket.SetFocus
Me!Ticket = format(tic,"0.00")
Me!New.SetFocus

This is aircode so you have to test it.

HTH Paolo
 
Thanks a lot for your help Paolo. type mismatch problem is disappeared with
null to zero addition. But result of mathematical transaction is still
rounding itself up. if dcl1 is 14.68 and dcl2 is 32.45 tic should be 1.88 but
it's still rounding up to 2.

Best Regards;

taco
 
Presumably textbox Ticket is bound to a field in the form's recordset.
What's the data type of the field to which it's bound?

I'm guessing that you set it to Numeric, but left it as the default Long
Integer. Change that to a field size that can accept decimals, such as
Single, Double or Decimal.
 
Yeap! You were right. I've forgot to change property settings on the table.
Now it works perfectly. Thanks a lot.

Best Regards;
 
Back
Top