What is wrong with this code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

X1 and X2 are calculated controls in a form.
The code below sets the X3 to true in all records without checking if X1=X2

What is wrong with?

Thank you in advance

Private Sub Form_Current()
If (Me!X1= Me!X2) Then
Me!X3 = True
End If
End Sub
 
Mattias,

I think the Current event is too early in the process to expect an
evaluation of calculated controls. I am not sure that this will work,
but try a ReCalc first, i.e.
Private Sub Form_Current()
Me.Recalc
If (Me!X1= Me!X2) Then
Me!X3 = True
End If
End Sub

or, simpler...
Private Sub Form_Current()
Me.Recalc
Me.X3 = Me.X1 = Me.X2
End Sub
 
If X3 has ever been set to True, it will remain so.
You don't have any code to ever set it to false.
 
Thank you all for your replies

I tried the me.recalc first in the code...and it works.
I have also added code for setting to false

Mattias
 
Mattias said:
Thank you all for your replies

I tried the me.recalc first in the code...and it works.
I have also added code for setting to false

Mattias

But you did not pick the best solution.
The field is not needed.
If you base your forms and reports on a query and use a calculated field you
will never have to worry about placing code here and there.
 
Mike makes a good point that this code is probably
inappropriate. Unless you have something going on here that
you haven't mentioned yet, you can just set the X3 control's
control source expression to =(X1 = X2) and be done with
it.
 
Back
Top