Connecting Tech Pros Worldwide Help | Site Map

Validating a txt box / Catching an error msg

Newbie
 
Join Date: Dec 2006
Location: Newcastle, England
Posts: 16
#1: Jan 11 '07
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
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#2: Jan 11 '07

re: Validating a txt box / Catching an error msg


Quote:

Originally Posted by JonnyB

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
nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#3: Jan 11 '07

re: Validating a txt box / Catching an error msg


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)
markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#4: Jan 11 '07

re: Validating a txt box / Catching an error msg


Quote:

Originally Posted by ADezii

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
nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#5: Jan 11 '07

re: Validating a txt box / Catching an error msg


"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)
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,706
#6: Jan 12 '07

re: Validating a txt box / Catching an error msg


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).
Reply