473,379 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Validating a txt box / Catching an error msg

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
5 1571
ADezii
8,834 Expert 8TB
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
3,080 Expert 2GB
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
648 Expert 512MB
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
3,080 Expert 2GB
"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
32,556 Expert Mod 16PB
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

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

Similar topics

1
by: davisjoseph | last post by:
Hi All, I'm using Xerces C++ 2.50 DOM version for Validating a valid XML againt the Schema(XSD) available; But it always shows an error in XSD I suppose; This is the error I'm getting using...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
1
by: Bob Rock | last post by:
Hello, always having to validate an XML stream against a XSD may add up an important overhead. My XMLs are usually the result of serializing a class instance and often in my applications what I...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
4
by: easoftware | last post by:
I am using VS .Net 2003 and VB. I have an app with one parent and two Mdi child forms. I need to validate data in the Mdi form. The Form.Validating event works when I try to close a Mdi form,...
2
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly...
76
by: Michael Stemper | last post by:
The W3C Validator is a great help, as far as it goes. However, I'm looking for something stricter. My coding style does not allow for implicit termination of an element; my intention and desire is...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.