By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

Validating a txt box / Catching an error msg

P: 16
Hi all,

I have a txt box that users enter a number in to create a booking. When a user doesn't enter a value access brings up an error message because the value cant be null which is fine but looks rubish and has the debug button on it etc. Is there a way of catching the error to display my own msg box.

I've tried an if statement but the access error msg pops up before it can run.

Does anyone have any ideas?

Jonny
Jan 11 '07 #1
Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 8,636
Hi all,

I have a txt box that users enter a number in to create a booking. When a user doesn't enter a value access brings up an error message because the value cant be null which is fine but looks rubish and has the debug button on it etc. Is there a way of catching the error to display my own msg box.

I've tried an if statement but the access error msg pops up before it can run.

Does anyone have any ideas?

Jonny
Assuming your Text Box is named txtTest, placing this code in the BeforeUpdate() Event Procedure should solve your problem:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtTest_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me![txtTest]) Then
  3.   MsgBox "You must enter a value in this Field", vbExclamation, "Entry Required"
  4.     Cancel = True
  5. End If
  6. End Sub
Jan 11 '07 #2

nico5038
Expert 2.5K+
P: 3,072
I always prefer to post a [Cancel] and [Save] button on a form to have the user forced to close the form either way.
In the [Cancel] buton I place the code:

IF me.dirty then
me.undo
endif
me.close

In the [Save] button I test all fields like:
Private Sub btnSave_Click()
Dim txtMessage As String
On Error GoTo Err_btnSave_Click
' init error message
txtMessage = ""
' Check fields in reverse order to set focus to the first
If Not Len(NZ(Me.Description)) > 0 Then
txtMessage = "Description empty ?" & vbCrLf
Me.Description.SetFocus
End If
If Not Len(NZ(Me.Severity)) > 0 Then
txtMessage = "No Severity?" & vbCrLf & txtMessage
Me.Severity.SetFocus
End If
If Not Len(NZ(Me.Type)) = 0 Then
txtMessage = "Recordtype empty ?" & vbCrLf & txtMessage
Me.Type.SetFocus
End If
' Check error found
If Len(txtMessage) > 0 Then
MsgBox txtMessage
Exit Sub
End If

DoCmd.Close

Exit_btnSave_Click:
Exit Sub

Err_btnSave_Click:
MsgBox Err.Description
Resume Exit_btnSave_Click

End Sub

Nic;o)
Jan 11 '07 #3

markmcgookin
Expert 100+
P: 648
Assuming your Text Box is named txtTest, placing this code in the BeforeUpdate() Event Procedure should solve your problem:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtTest_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me![txtTest]) Then
  3.   MsgBox "You must enter a value in this Field", vbExclamation, "Entry Required"
  4.     Cancel = True
  5. End If
  6. End Sub
Hi I am working on a similar thing and I found your post, that only seems to catch if you enter a space, or enter something then delete it,

Would a solution be to bind the box with a space in it as default? or is there a way of checking a null value?

like straight after btnClick()
IF me.txtInput=null Then
Msgbox "Enter a value"
Else
...
End If

That code won't work though, it doesn't seem to recognise it, I put it before my Dims and everything, right at the top of the code.

And also, it won't let a user click a cancel button for the form until they enter a value, that could be annoying. Ideally I am looking for something to catch a null value on the btnClick
Jan 11 '07 #4

nico5038
Expert 2.5K+
P: 3,072
"Locking" a user on a field is considered to be against the Windows GUI.
That's why we use the [Save] and [Cancel] button.

To test for an empty field you can use:

IF LEN(NZ(fieldname)) > 0 then
'filled
else
'empty
endif

Nic;o)
Jan 11 '07 #5

NeoPa
Expert Mod 15k+
P: 31,489
I think you need to change the Validation Rule property of the TextBox first (or the Required and Allow Zero Length properties of the field if bound).
Jan 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.