Forms - Mandatory field

T

Ted

Hello,

I have 2 fields on a form... FieldA and FieldB.

FieldA is a Yes / No box
FieldB is a Date field

Both fields are not 'required' at table level.

What I want to be able to do is make data-entry into FieldB mandatory, only
IF FieldA = "yes".

Any help / advice would be appreciated.

Ted
 
D

Dirk Goldgar

Ted said:
Hello,

I have 2 fields on a form... FieldA and FieldB.

FieldA is a Yes / No box
FieldB is a Date field

Both fields are not 'required' at table level.

What I want to be able to do is make data-entry into FieldB
mandatory, only IF FieldA = "yes".

Any help / advice would be appreciated.

Ted

You can create an event procedure for the form's BeforeUpdate event to
enforce this rule. It might look like this:

'----- start of example code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.FieldA = True Then
If IsNull(Me.FieldB) Then
Cancel = True
MsgBox "FieldB is required when FieldA is True."
End If
End If

End Sub
'----- end of example code -----
 
B

Brendan Reynolds

In the BeforeUpdate event of the form ...

If (Me![FieldA]) Then
If Len(Trim$(Me![FieldB])) = 0 Then
MsgBox "Uh-huh"
Cancel = True
End If
End If

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
D

Dirk Goldgar

Brendan Reynolds said:
In the BeforeUpdate event of the form ...

If (Me![FieldA]) Then
If Len(Trim$(Me![FieldB])) = 0 Then
MsgBox "Uh-huh"
Cancel = True
End If
End If

I don't think that'll work if FieldB is Null. You could fix it by
concatenating to a zero-length string:

If Len(Trim$(Me![FieldB] & "")) = 0 Then

but if FieldB is bound to a Date field, why force the conversion?
 
B

Brendan Reynolds

Yes, thanks Dirk, I forgot the empty string.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.


Dirk Goldgar said:
Brendan Reynolds said:
In the BeforeUpdate event of the form ...

If (Me![FieldA]) Then
If Len(Trim$(Me![FieldB])) = 0 Then
MsgBox "Uh-huh"
Cancel = True
End If
End If

I don't think that'll work if FieldB is Null. You could fix it by
concatenating to a zero-length string:

If Len(Trim$(Me![FieldB] & "")) = 0 Then

but if FieldB is bound to a Date field, why force the conversion?

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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