Results 1 to 4 of 4
  1. #1
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74

    Prevent moving to next record by validating current record

    I have a form textbox that represents a watershed. The code is 8 digits, and under no circumstances can it be any other length. I'd like to ensure that the number entered is 8 digits long before the user can move on to another or new record.



    Essentially if the # is anything but 8 digits i'd like an error message to pop-up beside the text-box "Must be 8 digits." This form element is present and set to not-visible by default. At this point the error message pops-up correctly, but it moves to the next record rather than staying focused on the textbox with incorrect data.

    Should this sort of validation be in the form_BeforeUpdate event?

    Code:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Len(Me.HUC8) <> 8 Then
            Me.txtError.Visible = True
            Me.HUC8.SetFocus
        ElseIf Len(Me.HUC8) = 8 Then
            Me.txtError.Visible = False
        End If
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can, but you're missing the Cancel = True:

    http://www.baldyweb.com/BeforeUpdate.htm

    You might also be able to use a validation rule on the field or textbox.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rhewitt is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    74
    Thanks!

    Initially I was going the textbox validation route, but I read somewhere that I should be using form on-update. The Cancel = True works in both cases, so I went back to the textbox method.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 08-24-2012, 10:32 AM
  2. Replies: 8
    Last Post: 02-09-2012, 02:02 PM
  3. prevent diplicate record
    By miziri in forum Access
    Replies: 3
    Last Post: 08-22-2011, 12:34 PM
  4. Moving a record.
    By Cutter596 in forum Queries
    Replies: 1
    Last Post: 08-02-2011, 02:49 PM
  5. copy current record to new record
    By er_manojbisht in forum Forms
    Replies: 1
    Last Post: 02-27-2010, 05:31 PM

Tags for this Thread

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