Check if combo value really changed

L

Lars Brownies

I have a combobox in which the afterupdate triggers a msgbox. In only want
this msgbox to show if the combobox value has really changed. Sometimes a
user selects a value for the combobox that is already the value of that
combobox. Is there a way that I can check if the user has really chosen a
new value? I can think of using a global var which the old value get filled
in the beforeupdate and checked in the afterupdate. But maybe there is a
more elegant way?

Thanks,

Lars
 
A

Allen Browne

If the combo is bound to a field in your table, compare its Value to its
OldValue, e.g.:

With Me.Combo1
If (.Value = .OldValue) OR (IsNull(.Value) = IsNull(.OldValue)) Then
'Nothing changed
Else
'It did change.
End If
End With

If it's unbound, you will need to use a module-level variable (i.e. declared
in the General Declarations section of the module of the form that contains
the combo.)
 
L

Lars Brownies

Excellent! Thanks.

Lars

Allen Browne said:
If the combo is bound to a field in your table, compare its Value to its
OldValue, e.g.:

With Me.Combo1
If (.Value = .OldValue) OR (IsNull(.Value) = IsNull(.OldValue))
Then
'Nothing changed
Else
'It did change.
End If
End With

If it's unbound, you will need to use a module-level variable (i.e.
declared in the General Declarations section of the module of the form
that contains the combo.)
 
D

David W. Fenton

If (.Value = .OldValue) OR (IsNull(.Value) =
IsNull(.OldValue)) Then

Is there really any advantage in shortcircuiting that? Why not just
run the 2nd test? It's not like you're doing it in a loop where a
tiny performance difference could eventually add up to something
significant.
 
A

Allen Browne

Not sure what 'shortcircuiting' refers to, David.

The cases where it hasn't changed would be identified as:
a) value is the same as it was OR
b) value was null and still is null.
 
D

David W. Fenton

Not sure what 'shortcircuiting' refers to, David.

The cases where it hasn't changed would be identified as:
a) value is the same as it was OR
b) value was null and still is null.

I was typing while asleep, I guess. I read your If clause as:

If (.Value = .OldValue) OR Nz(.Value) = Nz(.OldValue)) Then

In that ,case, the first comparison *is* unnecessary.

By shortcircuiting, I was referring to the idea that VBA would
evaluate only the first test and if it is True, skip evaluating the
second. But it seems that is not the case, either with If/Then/Else
or IIf() in VBA.
 

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