473,406 Members | 2,217 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,406 software developers and data experts.

VBA Validation

I would like some help in how I am to validate a text field to ensure that it is numeric and cannot be negative. Also in this text box the user is not allowed to continue without entering the correct data in the first text field.
This will need to be using VBA in Microsoft Access 2003.
I have attempted using IsNumeric; however characters are still allowed to be entered. I am unsure on how to proceed to clear the characters that have been entered, or this method that I am using is incorrect and there is a far better method. Would appreciate any help. Thank you.
Jul 23 '08 #1
4 3932
RuralGuy
375 Expert 256MB
Me.YourControlName.UnDo will put a bound control back to its previous value. IsNumeric should work just fine and > 0 for the non negative test. Validation should be accomplished in the control's BeforeUpdate event and set Cancel = True to cancel the update. I would think just letting the user know that *all* numbers is a requirement would be sufficent, but that's your call.
Jul 23 '08 #2
I have attempted to do all that has been stated and this is what it looks like, however I think there are still some flaws. I have attempted to uncover what the flaws are, but what ever I have done does not make any of the slightest difference. Can somebody please give me an idea of where about I am going wrong. Would be very much appreciated, thank you.

Private Sub Branch_Code_BeforeUpdate(Cancel As Integer)

Me.YourControlName.Undo

Branch_Code.SetFocus
If Not IsNumeric(Branch_Code) Then 'This makes sure that the number used is numeric
MsgBox "Invalid Format" 'This is the message box that is shown when a character has been entered
Else
Branch_Code.Undo ' This clears the character that has been entered

End If

If Branch_Code = "" Or IsNull(Branch_Code) Then 'This makes sure that the field cannot be left blank
MsgBox " You must enter a value" ' This is the message box that is given when nothing is entered
Cancel = True
ElseIf Len(Branch_Code) <> 2 Then ' This makes sure that there must be two characters in this field
MsgBox " This field must be 2 characters only"
Cancel = True
End If

If Branch_Code >= 0 Then 'The code must not be a negative number
MsgBox "Must not be negative" ' This is the message box that is given when a negative number is entered.
End If
Jul 26 '08 #3
missinglinq
3,532 Expert 2GB
There are a number of flaws. Your first line of code
Expand|Select|Wrap|Line Numbers
  1. Me.YourControlName.Undo
  2.  
would be undoing any data that you've entered, before it's even been tested, assuming that you’d used the name of the your actual control, Branch_Code, rather that the “generic” name Allan used in his example! Once you correct the textbox name, you’ll never be able to add/edit a record., and
Expand|Select|Wrap|Line Numbers
  1. Branch_Code.SetFocus
will place you in an endless loop, repeatedly sending you back to the textbox.

Next, what Dataype is the Branch_Code? Is it actually Numeric or is it Text. Identifying numbers such as this, or telephone numbers or Social Security Numbers should always be Text. You should only use a Numeric datatype if you’re actually going to do math with the “numbers.” If you actually use a Numeric datatype, Access will actually strip off any leading zeros, which will give you problems if you have Branch_Code's like 01, 02, etc.

These lines actually check if the data consists of the characters 0-9, sending up a messagebox if it isn’t, but only undoes the data, with the Else, if it actually is made up of numeric characters!
Expand|Select|Wrap|Line Numbers
  1. If Not IsNumeric(Branch_Code) Then 
  2. MsgBox "Invalid Format" 
  3. Else
  4. Branch_Code.Undo 
  5. End If
  6.  
These lines need to be in the Form’s Before update event, otherwise the user can simply enter nothing in the field. If nothing is entered in the field, the textbox BeforeUpdate event is never triggered
Expand|Select|Wrap|Line Numbers
  1. If Branch_Code = "" Or IsNull(Branch_Code) Then 
  2. MsgBox " You must enter a value" 
  3. Cancel = True
  4. ElseIf Len(Branch_Code) <> 2 Then ' This makes sure that there must be two characters in this field
  5. MsgBox " This field must be 2 characters only"
  6. Cancel = True
  7. End If
These lines can go in the Branch_Code_BeforeUpdate, but this
Expand|Select|Wrap|Line Numbers
  1. If Branch_Code >= 0 Then 
needs to be this
Expand|Select|Wrap|Line Numbers
  1. If Branch_Code <= 0 Then
to throw up your message if it’s negative. The general rule is that code checking the correctness of entered data goes in the control’s BeforeUpadate event, but code checking whether any data has been entered at all goes in the Form’s BeforeUpdate event.

Welcome to Bytes!

Linq ;0)>
Jul 26 '08 #4
missinglinq
3,532 Expert 2GB
I think this code does what you want. I've changed some of the messages a little to make it clearer to the users which field you're talking about. This is always a good idea, especially when using the form's BeforeUpdate event , as the textbox in question may not be the last field filled in.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Branch_Code_BeforeUpdate(Cancel As Integer)
  2.  
  3. If Not IsNumeric(Branch_Code) Then 'This makes sure that the number used is numeric
  4.   MsgBox "Invalid Format in Branch_Code field! Only Numeric characters allowed!" 'This is the message box that is shown when a character has been entered
  5.   Cancel = True
  6.   Me.Branch_Code.Undo ' This clears the character that has been entered
  7. End If
  8.  
  9. If Len(Branch_Code) <> 2 Then ' This makes sure that there must be two characters in this field
  10.  MsgBox " This field must be 2 Numeric characters only!"
  11.  Cancel = True
  12.  Me.Branch_Code.Undo
  13. End If
  14.  
  15. If Branch_Code <= 0 Then 'The code must not be a negative number
  16. MsgBox "Branch_Code field must not be negative" ' This is the message box that is given when a negative number is entered.
  17. End If
  18. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  If Branch_Code = "" Or IsNull(Branch_Code) Then 'This makes sure that the field cannot be left blank
  3.   MsgBox " You must enter a value in Branch_Code field" ' This is the message box that is given when nothing is entered
  4.   Cancel = True
  5.   Me.Branch_Code.SetFocus
  6. End If
Linq ;0)>
Jul 26 '08 #5

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

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
2
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
4
by: Tim Meagher | last post by:
I am trying to use both validation controls and to add submit button attributes, but when I add the button attributes, the javascript fpr the validation controls is no longer created for the page. ...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
6
by: Stephen | last post by:
Hi, the validation controls dont work on Netscape or Mozilla and only on Internet Explorer why? How do i correct this problem? Thanks
7
by: Ryan Ternier | last post by:
We're running a site that has required field validation on the login page. It works fine on our development / test machines. However, when I upload this site to our live server i get this error. ...
5
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
2
by: dustbort | last post by:
I recently had a problem where my required field validator stopped working. But, the page still posted back and tried to insert a record into the database without performing server-side validation....
6
by: Jon Paal | last post by:
validation doesn't fire what's missing ????? /////// ---- code -----/////////////////////////// Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.