Prevent data entry if another field contains data

  • Thread starter Thread starter A_Classic_Man
  • Start date Start date
A

A_Classic_Man

I need code to prevent data entry into Form Field B if Form Field A
has data in it?

Access version is 97.

Thanks

Ron
 
Ron,

I would put this code in the Forms Current event, so that as soon as you
move to a record, it will do this. I would also put it in the AfterUpdate
event of the control that is bound to [Field A] on your form. This assumes
that [Field B] is a textbox with the name txt_FieldB

me.txt_FieldB.Locked = (LEN(me.txt_FieldA & "") > 0)

The code on the right of the equal sign will evaluate to be either True or
False, depending on whether there is a value in txt_FieldA. If it evaluates
to True, this code will lock control txt_FieldB so that it cannot be edited.
If the expression evaluates to False, then txt_FieldB will not be locked and
can be edited.

HTH
Dale
 
The code needs, I think, to be

If LEN(Me.txt_FieldA & "") > 0 Then
Me.txt_FieldB.Locked = True
Else
Me.txt_FieldB.Locked = False
End If

Otherwise, if you move from a record where txt_FieldA has data, to a record
where txt_FieldA doesn't have data, txt_FieldB will still be locked!

Also, this allows for the possibility of txt_FieldA having data, then (if a
mistake was made, for instance) having that data deleted.

On the chance that your user has already entered data in txt_FieldB before
going to txt_FieldA (users do illogical things!) you might want to include a
line, in the AfterUpdate event, to take out anything already in txt_FieldB:

If LEN(Me.txt_FieldA & "") > 0 Then
Me.txt_FieldB = ""
Me.txt_FieldB.Locked = True
Else
Me.txt_FieldB.Locked = False
End If

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
 
Linq,

My one line of code accomplishes the exact same thing as your 5 lines of
code. The expression to the right of the equal sign will return either True
or False, either locking or unlocking txt_FieldB.

Your second set of code does raise a good point though.

Dale
 
Linq,

My one line of code accomplishes the exact same thing as your 5 lines of
code. The expression to the right of the equal sign will return either True
or False, either locking or unlocking txt_FieldB.

Your second set of code does raise a good point though.

Dale












- Show quoted text -

Thanks to both Linq and Dale. With your help, I figured to out

Ron
 
Back
Top