What is wrong with this code

M

Mattias

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
 
S

Steve Schapel

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
 
M

MacDermott

If X3 has ever been set to True, it will remain so.
You don't have any code to ever set it to false.
 
M

Mattias

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
 
M

Mike Painter

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.
 
M

Marshall Barton

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.
 

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