Required field on subform VBA

Slab_Rankle

Registered User.
Local time
Today, 11:35
Joined
Aug 10, 2011
Messages
36
Hey guys,

I'm having problems trying to make a required field pop up a message when it's left blank. It's for a subform and I don't think the code I'm using as the destination is right. Here's what I have at the moment:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Forms![TabForm]![Notes subform].Form.txtNarrative) Then
MsgBox "Field needs to be filled in"
Me.txtNarrative.SetFocus
End If

End Sub

Can anyone point me in the right direction?
 
Hi
The code below should work if this the BeforeUpdate event of the SUB form.
Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.txtNarrative) Then
MsgBox "Field needs to be filled in"
Me.txtNarrative.SetFocus
End If

End Sub
 
Well, that's what I originally had and it didn't seem to work. I'm really confused as to why it's not working. I'm 100% sure that txtNarrative is the name of the text box that needs checking. I thought maybe because I wasn't specifying the sub form it wasn't finding the field but it's definitely on the subform and the field is there....any ideas as to why this isn't working?
 
Hi
You could try replacing:
Code:
If IsNull(Me.txtNarrative) Then
with
Code:
If IsNull(Me.txtNarrative) or Me.txtNarrative = "" Then
just in case the control is a zero length string.
 
I tried that too...however, even when the field is empty it still allows it to be submitted and no error message pops up. I'm probably doing something really stupid because I know this has worked for other people after doing a fair bit of research online...
 
Hi
I'm probably doing something really stupid
ME TOO :)
We have forgotten to cancel the update!
After the line
Code:
Me.txtNarrative.SetFocus
put this line:
Code:
Cancel = True
Sometimes we miss the obvious.
 

Users who are viewing this thread

Back
Top Bottom