Disable field2 control if field1 is not null

G

Guest

How do I disable field2 control in a form if field1 control in the same
record on the form is not null. I've tried several things, but I think I'm
getting confused with how to write is not null.

I need to field to be enabled again if the user deletes the contents of
field1.

In a nut shell, I have field1 and field2 and I only want a user to be able
to fill in one or the other of the 2 fields.

I'm not sure if I put the code in field1_Exit or if I need a refresh
someone. Any help is appreciated. Thanks.
 
G

Guest

On the after update event of field1 enter the code
if isnull(me.field1) or me.field1="" then
me.field2.locked=false
else
me.field2.locked=True
End if
Dont forget to call this sub on the on current event of the form, that way
it will lock or enable the field when you open the form, or when you move
between records
 
G

George Nicholson

Not 100% sure what you really need.

1) If you are only worried about disabling Field2:
In Form_Current:
' This handles enabling Field2 when moving to a record
If Len(Me!Field1)>0 Then
Me.Field2.Enabled = False
Else
Me.Field2.Enabled = True
End If

In Field1_AfterUpdate:
' This handles enabling Field2 when Field1 has been changed
If Len(Me!Field1)>0 Then
Me.Field2.Enabled = False
Else
Me.Field2.Enabled = True
End If

Note:
Field1 is never disabled. If the user fills in Field 2 first, Field 1 is
still available. If Field1 is then filled in, you'll disable Field2 but not
"clear" it.

2) If you want one field disabled whenever the other is filled in (as long
as all data entry is done via Form, this should work):

In Form_Current:
' This handles enabling Field1 and Field2 when moving to a record
If Len(Me!Field1)>0 Then
Me.Field2.Enabled = False
Else
Me.Field2.Enabled = True
If Len(Me!Field2)>0 Then
Me.Field1.Enabled = False
Else
' Both fields are empty and enabled
Me.Field1.Enabled = True
End If
End If

In Field1_AfterUpdate:
' This handles enabling Field2 when Field1 has been changed
If Len(Me!Field1)>0 Then
Me.Field2.Enabled = False
Else
' Both fields are empty and enabled
Me.Field2.Enabled = True
End If

In Field2_AfterUpdate:
' This handles enabling Field1 when Field2 has been changed
If Len(Me!Field2)>0 Then
Me.Field1.Enabled = False
Else
' Both fields are empty and enabled
Me.Field1.Enabled = True
End If


HTH,
 

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