Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126

    Validate a field

    Hi

    I have a few forms that are used to enter customer details and product details. i would like to ensure a field is entered and if not the display an error message and then force the user to go back to that field and correct the problem.

    I have tried placing validation in the table and the forms, but wasnt too happy with the error message as its not always displaying my message i typed in



    Click image for larger version. 

Name:	1.png 
Views:	3 
Size:	34.6 KB 
ID:	11681 Click image for larger version. 

Name:	2.png 
Views:	1 
Size:	19.2 KB 
ID:	11682


    i dont the idea of having a help button????
    i have had alook online but cant apply it to my system, any ideas?

    thanks

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I do all of my field validation in the BeforeUpdate event of the control and the BeforeUpdate event of the form.

  3. #3
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    can you give me an example please, of the code.
    i would like to have it so that a field must have some data and not missed out, if it is missed out then a message box appears and set focus back to that control

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Something like:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Len(Me.MyControl & "") = 0 Then
       Cancel = True
       MsgBox "Please complete this control", vbCritical + vbOKOnly
       Me.MyControl.SetFocus
    End If
    End Sub
    ...using your control name of course.

  5. #5
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    Thanks for the code, i have put it into the before update on the form event.


    i have 3 other feilds that need the same error messages too, if you can help out
    1. a town field which has a default value set from the table.
    2. post code\zip code with >LA99\ 0LL;0;_ as the input mask
    3. contact number with \(9999") "0009000;;_ as the input mask

    what code is needed to move these to vba coding? as i can have the error messages (with out help button)

    missingling also gave me this code Me.ControlName.Text = StrConv(Me.ControlName.Text, vbProperCase) to convert the text to proper case, this is in the control name on exit event, shall i keep it there or should it be moved into your code?

    thank you

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I would convert to ProperCase in the BeforeUpdate event of the control and use the .Value property rather than the .Text property. As for the other controls, just add another If...EndIf to the code in the Form's BeforeUpdate event to test for validity.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    When validating multiple Fields, in the Form_BeforeUpdate event, you have to add one more line, as shown below in red:
    Code:
    If Len(Me.MyControl & "") = 0 Then
       Cancel = True
       MsgBox "Please complete this control", vbCritical + vbOKOnly
       Me.MyControl.SetFocus
       Exit Sub 
    End If

    If you don't have the Exit Sub line, Access will drop immediately from one If...Then to the next, not stopping at the first offending Control, before moving on to the next If...Then, despite the MyControl.SetFocus line of code.

    Linq ;0)>

  8. #8
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    thanks for that, my code looks like.....

    Option Compare Database

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Len(Me.ShopName & "") = 0 Then
    Cancel = True
    MsgBox "Please enter a shop name", vbCritical + vbOKOnly
    Me.ShopName.SetFocus
    Exit Sub
    End If

    If Len(Me.ContactName & "") = 0 Then
    Cancel = True
    MsgBox "Please enter a contact name", vbCritical + vbOKOnly
    Me.ContactName.SetFocus
    Exit Sub
    End If

    If Len(Me.AddressLine1 & "") = 0 Then
    Cancel = True
    MsgBox "Please enter address line 1", vbCritical + vbOKOnly
    Me.AddressLine1.SetFocus
    Exit Sub
    End If
    End Sub

  9. #9
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    i can change where the code is from the exit event to beforeupdate, but i didnt understand the value and text bit? should it now be
    Me.ShopName.Value = StrConv(Me.ShopName.Value, vbProperCase) rather then
    Me.ShopName.Text = StrConv(Me.ShopName.Text, vbProperCase)

    can you please write the code out for me to validate the zip code\postcode, contact number and town please (town default value is Leicester)

    thanks again

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Yes, change .Text to .Value as you have shown. You write the code for the other three controls and try it and let us know how it works.

  11. #11
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    i have changed the .text to .value and placed the code in the beforeupdate event, but now iam getting this error message lol
    I have checked the system and there is no validation in the tables or the form, everything is in the vba coding

    Click image for larger version. 

Name:	Validation.png 
Views:	2 
Size:	27.3 KB 
ID:	11692

    any ideas???
    iam trying to write some code out for the other fields.

    thanks people

  12. #12
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You can check input in a Control's BeforeUpdate event, but you cannot do something like changing the formatting of that data in this event, or you'll get the error you just posted. Don't remember your previous thread, or why OnExit was used, at that time, but you can move the formatting to the Control's AfterUpdate event, in which case you will need to change the .Text to .Value:

    Code:
    Me.ShopName.Value = StrConv(Me.ShopName.Value, vbProperCase)


    which can be simplified to

    Code:
    Me.ShopName = StrConv(Me.ShopName, vbProperCase)

    Linq ;0)>

  13. #13
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    i managed to get this from the net
    Me.Town.Value = Leicester

    after testing it, when you create a new record it comes up (town displays leicester) but once you tab into the field and tab out again (as its already filled) it gets cleared?

    any ideas, also for the other 2 fields iam not sure where to start from, if you can help out


    thanks

  14. #14
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    Is this ok to validate a phone number????

    If Not IsNumeric(ContactNumber1) Then
    Cancel = True
    MsgBox "Please enter contact number 1", vbCritical + vbOKOnly
    Me.txtContactNumber1.SetFocus
    End If
    If Len(ContactNumber1) <> 11 Then
    Cancel = True
    MsgBox "Please enter contact number 1", vbCritical + vbOKOnly
    Me.txtContactNumber1.SetFocus
    Exit Sub
    End If

  15. #15
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    I have played around with the leicester as the defualt value for leicester, i have moved the code to the event when a new customer button is clicked and it seems to stay there now without being moved, on one more to go now, the postcode (zip code)

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 09-05-2012, 02:49 PM
  2. validate based on another value
    By subnet11 in forum Programming
    Replies: 3
    Last Post: 06-11-2012, 12:12 AM
  3. If Statement to validate Field on a Form
    By ETCallHome in forum Forms
    Replies: 4
    Last Post: 04-05-2011, 05:22 PM
  4. Validate Form Field based on Duplicate Data
    By bornfattom23 in forum Forms
    Replies: 3
    Last Post: 11-05-2010, 02:02 PM
  5. using Functions to validate a date field
    By jamin14 in forum Programming
    Replies: 1
    Last Post: 03-18-2010, 12:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums