0
votes

I have very strange problem in VBA. I'm trying to compare 2 values, one is varible assigned from table (AfterUpdate event on ComboBox), and other one is value that user type in textbox on form.

nosivost = DLookup("max_nosivost", "tbl_vozila", "[registarska_oznaka]='" & Me.vozilo.Column(2) & "'")
If nosivost < Me.kolicina Then MsgBox "Some text", vbCritical, "Title"

Me.vozilo is name of ComboBox that displays records from tbl_vozila. Me.kolicina is name of TextBox.

The problem is that I'm getting MsgBox every time, even if the If-Then statement is not true. For example, if I type 10 in TextBox and select record from ComboBox which have max_nosivost = 30 i would still get MsgBox.

I checked value of variable nosivost and DLookup is returning correct value.

1

1 Answers

2
votes

Always do a vartype on variables. You are probably comparing a string and a number.

A=30
B="30"

'wont match
If A = B then msgbox "hi"

msgbox vartype(A)
Msgbox Vartype(B)
If Clng(A) = CLng(B) then msgbox "hi"

The values returned by vartype. Use

Return Values

Constant Value Description 
vbEmpty 0 Empty (uninitialized) 
vbNull 1 Null (no valid data) 
vbInteger 2 Integer 
vbLong 3 Long integer 
vbSingle 4 Single-precision floating-point number 
vbDouble 5 Double-precision floating-point number 
vbCurrency 6 Currency value 
vbDate 7 Date value 
vbString 8 String 
vbObject 9 Object 
vbError 10 Error value 
vbBoolean 11 Boolean value 
vbVariant 12 Variant (used only with arrays of variants) 
vbDataObject 13 A data access object 
vbDecimal 14 Decimal value 
vbByte 17 Byte value 
vbUserDefinedType 36 Variants that contain user-defined types 
vbArray 8192 Array