Null for TextBox

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

Guest

I have a bound TextBox to key in data and it is not allowed to be empty. I don't want to set the rule in the Table and like to set it in the TextBox. In the TextBox's AfterUpdate, I set
If IsNull(TextBox) The
MsgBox "You must key in something
Exit Su
End I
It works if the user has typed something and then delete it. Howerver, if the user forgets to type something and press ENTER, it jumps to the next TextBox directly and nothing shows up. Can anyone help? Thanks.
 
There is no guarantee that the user will ever move the mouse into this text
box before they try to save the record, so the only way to catch this is the
BeforeUpdate event of the *form*.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Mytextbox) then
Cancel = True
MsgBox "Enter something in Mytextbox."
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Jeff said:
I have a bound TextBox to key in data and it is not allowed to be empty. I
don't want to set the rule in the Table and like to set it in the TextBox.
In the TextBox's AfterUpdate, I set
If IsNull(TextBox) Then
MsgBox "You must key in something"
Exit Sub
End If
It works if the user has typed something and then delete it. Howerver, if
the user forgets to type something and press ENTER, it jumps to the next
TextBox directly and nothing shows up. Can anyone help? Thanks.
 
Jeff, you have used the BeforeUpdate event of the text box.
Use the BeforeUpdate event of the FORM instead.

In an event-driven environment such as Access, you cannot assume that the
user will ever visit to text box, let alone assume an order in which they
will visit them.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Jeff said:
Thank you for your help, Allen. But it still can not work properly as I
want. It works only when the user types something in the TextBox and then
deletes the content. If the user doesn't type anything and press ENTER
directly to jump to the next TextBox, it doesn't work. Besides, I want it be
able to trap the error and show the message immediately before the user can
proceed to the next TextBox, not at the time when the user press SAVE.
 
it is not allowed to be empty. I don't want to set the rule in the
Table and like to set it in the TextBox.

I would recommend doing both. The field-level Required property will
protect the data against all edits, being VBA, queries, Excel, another form
etc etc. The form-level validation will perform some intelligent avoiding
behaviour, but the back end protection should still be there.

B Wishes


Tim F
 
Back
Top