Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52

    Using VBA instead of Validation Rule

    I have been trying to use to a validation rule on 2 fields, and then using the OnError event to tell the program what to do if the validation rule fails. I could not get this to work and tried several different ways. I am thinking the best way to do this is to make this happen with VBA alone, and throw the validation rule from the table out.



    Heres what I need to do:

    I have 1 textbox in a form that needs to be monitored- MO_ID

    If the user enters a number greater than 99999 and less than 999999, I need a pop up message box to appear, default the MO_ID text box to being blank, and then go to the control box MO_ID for re-entry.



    Here's my best attempt at the code, but every time it sends the pop up box and never will go to the else statment, regardless of what was in the textbox:

    Code:
    If Me.MO_ID > 99999
           MsgBox("You can not enter an employee number here!")
           Me.MO_ID = ""
           DoCmd.GoToControl(MO_ID)
    Else
          GoTo Main_Code
    End If
    End Sub
    Thanks for any input!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    I don't build validation in tables - only on forms.

    Is MO_ID a text type field.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    I'm starting to feel the same way and don't plan on using the validation rule anymore myself.

    MO_ID is an Integer field

  4. #4
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    I just noticed a typo (I said it backwards earlier)- basically, the only values for MO_ID to be ok is 99999 < MO_ID < 999999

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    If Me.MO_ID < 99999 Or Me.MO_ID > 999999 Then

    Why not a combobox with LimitToList set to Yes?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    Combo box won't work, I need it to be as described in my OP. This field is entered via a barcode scanner with automatic carriage return programmed in.

  7. #7
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    Some reason my original code that I posted still doesn't work properly. What would be the best event to use this after? I was using When Lost Focus.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Don't know why combobox wouldn't work with barcode scanner.

    Try BeforeUpdate event.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    Ill give that a shot with that event and see what happens. If i used a combobox, I would need over 1 million possibilities to appear in the drop down menu.

  10. #10
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    I tried running the code with the before update event, and it only partially works for me. It give the msgbox pop up as its supposed to, but it does not return to the text box and it does not clear the data from the text box either. It gives the error:

    Run-time error '-2147352567 (80020009)

    The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing the Microsoft Office Access from saving data in the field.


    How do I prevent this error from popping up? Also, how do I make it properly default the text box back to being blank if it is beyond the desired range of the results. I thought I had the correct code for that to happen, but apparently not.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Post your revised code for analysis. Review http://www.blueclaw-db.com/access_ev...foreupdate.htm
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    If I try this on BeforeUpdate for the Form, nothing works at all. The code below (for the field on the form) only works partially and gives the error that I posted above.

    Code:
    Private Sub Text0_BeforeUpdate(Cancel As Integer)
    
    If Me.Text0 > 99999 Then
           MsgBox ("You can not enter a MO number here!")
           DoCmd.Close acForm, "Query Form"
           DoCmd.OpenForm "Query Home"
           Cancel = True
    Else
          MsgBox ("Sample Code - Working Properly")
          
    End If
    End Sub

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    What does 'partially' mean - under what conditions does it not work and what happens?

    Code looks good to me.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I prefer to use the Case construct, instead of IF() in cases like this.
    Code:
    Private Sub Text0_BeforeUpdate(Cancel As Integer)
        Dim MinNum As Long
        Dim MaxNum As Long
        Dim EnteredNum As Long
    
    
        MinNum = 99999
        MaxNum = 999999
    
        '    EnteredNum = Me.MO_ID
        EnteredNum = Me.Text0
    
        Select Case EnteredNum
            '99999 < MO_ID < 999999
            Case Is < MinNum, Is > MaxNum
                'Outside the limits, so
                msgbox ("You can not enter a MO number here!")
                DoCmd.Close acForm, "Query Form"
                DoCmd.OpenForm "Query Home"
                Cancel = True
                
            Case Else
                'Inside the limits, so
                msgbox ("Sample Code - Working Properly")
        End Select
    
    End Sub
    However, you said
    MO_ID is an Integer field
    and that the minimum number is GT 99,999 and the maximum number is LT 999,999.

    Given that:
    Integer variables are stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767
    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647

    , shouldn't the field type for "MO_ID" be a Long Integer??? The code might execute, but the number couldn't be stored in an integer field.

    I'm just sayin........

  15. #15
    Mearntain is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    52
    Quote Originally Posted by June7 View Post
    What does 'partially' mean - under what conditions does it not work and what happens?

    Code looks good to me.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    The only problem I am having is that if the text box has a value that is not acceptable for the code, I would like it to defualt that text box to being blank and then return the control back to that box. If I try to do the code for that, it gives an error, regardless of whether I use my original code or add it in to yours.

    I have been trying to do it like this:

    Code:
    Private Sub Text0_BeforeUpdate(Cancel As Integer)
        Dim MinNum As Long
        Dim MaxNum As Long
        Dim EnteredNum As Long
    
    
        MinNum = 99999
        MaxNum = 999999
    
        '    EnteredNum = Me.MO_ID
        EnteredNum = Me.Text0
    
        Select Case EnteredNum
            '99999 < MO_ID < 999999
            Case Is < MinNum, Is > MaxNum
                'Outside the limits, so
                msgbox ("You can not enter a MO number here!")
                Me.text0 = ""
                       DoCmd.GoToControl "text0"
                Cancel = True
                
            Case Else
                'Inside the limits, so
                msgbox ("Sample Code - Working Properly")
        End Select
    
    End Sub
    But it doesnt default to being blank and doesnt go to the control, not sure why

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

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2015, 03:52 AM
  2. Help with validation rule
    By Desstro in forum Programming
    Replies: 5
    Last Post: 02-27-2011, 08:05 PM
  3. Validation Rule
    By Christopher in forum Forms
    Replies: 15
    Last Post: 04-20-2010, 06:05 AM
  4. Validation Rule
    By robomose in forum Access
    Replies: 1
    Last Post: 09-09-2009, 05:59 AM
  5. Validation Rule
    By smitstev in forum Access
    Replies: 5
    Last Post: 06-30-2009, 09:58 AM

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