Block If not executing.

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

Guest

Hello all. I hope this is in the right place, I haven't used the MS
newsgroups before.
Code:
Private Sub Form_Current()

If (txtPcDate.Value > date And txtAdmDate.Value > date And
txtClinDate.Value > date) Then
cboCurrent.Value = True

ElseIf (cboCurrent.Value = True) Then
cboCurrent.Value = False

End If

DoCmd.GoToControl ("txtName")

End Sub

The database contains two tables with slightly different fields
(unfortunately I am not able to collapse them into one table) so I have two
almost identical forms to use to enter data into the tables.
On the first form, the above code works perfectly, but on the second form I
would only get the DoCmd working. The object names are all correct.
When I replaced [txtPcDate.Value > date And txtAdmDate.Value > date And
txtClinDate.Value > date] with [true] it did execute. What has me stumped is
that I am using exactly the same code and test dates in both forms but the
conditional doesn't evaluate the same on the second form.
 
Presumably txtPcDate and txtAdamDate and txtClinDate are text boxes,
cboCurrent is a combo, and Date is the system date (*not* the name of
another field or control)?

Any chance that txtPcDate or txtAdmDate or txtClinDate could be null? If any
one is null, the If will not result in true, so the Else will execute. More
info in:
Common errors with Null'
at:
http://allenbrowne.com/casu-12.html

Hopefully cboCurrent is not bound to a control. Firstly it is wrong to store
the value that is the result of a calculation. Secondly, dirtying a record
in Form_Current just because the user moves there makes no sense. Thirdly,
if you did want to break the rules and store the value, it should be done in
the AfterUpdate of the affected controls (or the BeforeUpdate event of the
form), and not just because somebody happened to visit the record.

HTH.
 
Allen Browne said:
Presumably txtPcDate and txtAdamDate and txtClinDate are text boxes,
cboCurrent is a combo, and Date is the system date (*not* the name of
another field or control)?
Correct.

Allen Browne said:
Any chance that txtPcDate or txtAdmDate or txtClinDate could be null? If any
one is null, the If will not result in true, so the Else will execute. More
info in:
Common errors with Null'
at:
http://allenbrowne.com/casu-12.html
No, none of these can be Null. All three fields must have a valid date entered for the record to be accepted.

Allen Browne said:
Hopefully cboCurrent is not bound to a control. Firstly it is wrong to store
the value that is the result of a calculation. Secondly, dirtying a record
in Form_Current just because the user moves there makes no sense. Thirdly,
if you did want to break the rules and store the value, it should be done in
the AfterUpdate of the affected controls (or the BeforeUpdate event of the
form), and not just because somebody happened to visit the record.

HTH.
cboCurrent is unbound, it is meant to merely act as an indicator for the user that one or more of the dates has expired. I just don't understand why, with two identical blocks of code and two identical sets of test data I get the if working perfectly in one case, and always evaluating as false in the other.

EDIT: Well, I still don't know what caused this problem, but I came across a similar-ish problem which was solved by exporting everything into a new database. Gave it a try and it worked for me too. Thanks for the reply Allen, and nice site.
 
Last edited:
Back
Top