Results 1 to 5 of 5
  1. #1
    dweekley is offline Advanced Beginner
    Windows XP Access 2002 (version 10.0)
    Join Date
    May 2010
    Posts
    52

    Validating Form Data using VBA

    I have been trying to get this validation working for the form BeforeUpdate. Catches the empty field but makes it all the way to the Me.Dirty. Not Exiting. I Rem'd out a couple of the actions trying different scenarios. The Me.Dirty works fine as stand alone.

    'Validate Entries


    If Len(txtCustomerID.Value & "") = 0 Then
    MsgBox "Customer ID must have a value", vbOKOnly, "Missing Value"

    Cancel = True
    'Me.txtCustomerID.SetFocus
    'Exit Sub


    ElseIf Len(txtCustomerAbbr.Value & "") = 0 Then
    MsgBox "Customer Abbreviation must have a value", vbOKOnly, "Missing Value"

    Cancel = True
    'Me.txtCustomerAbbr.SetFocus
    'Exit Sub


    ElseIf Len(txtCustomer_Name.Value & "") = 0 Then
    MsgBox "Customer Name must have a value", vbOKOnly, "Missing Value"



    Cancel = True
    'Me.txtCustomer_Name.SetFocus
    'Exit Sub


    ElseIf Len(txtDwg_File_Loc.Value & "") = 0 Then
    MsgBox "Customer File Location must have a value", vbOKOnly, "Missing Value"

    Cancel = True
    'Me.txtDwg_File_Loc.SetFocus
    'Exit Sub

    Else

    If Me.Dirty Then
    If MsgBox("Do you want to Save?", vbYesNo + vbQuestion, _
    "Save Record") = vbNo Then
    Me.Undo
    Exit Sub

    End If

    End If

    End If

    TIA

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Me.Dirty will *always* be true in the BeforeUpdate event. That's how you got there.

  3. #3
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    Further to RuralGuy's observation, I would re-structure your code.
    Code:
        If MsgBox("Do you want to Save?", vbYesNo + vbQuestion, "Save Record") = vbYes Then
            'Validate Entries
            If Len(txtCustomerID.Value & vbNullString) = 0 Then
                strMessage = strMessage & vbCrLf & "Customer ID must have a value"
            End If
            If Len(txtCustomerAbbr.Value & vbNullString) = 0 Then
                strMessage = strMessage & vbCrLf & "Customer Abbreviation must have a value"
            End If
            If Len(txtCustomer_Name.Value & vbNullString) = 0 Then
                strMessage = strMessage & vbCrLf & "Customer Name must have a value"
            End If
            If Len(txtDwg_File_Loc.Value & vbNullString) = 0 Then
                strMessage = strMessage & vbCrLf & "Customer File Location must have a value"
            End If
            
            If Len(strMessage & vbNullString) <> 0 Then
                'Display message to user
                MsgBox "Data Entry is incomplete:" & strMessage, vbCritical, "AppNameHere"
                Cancel = True
            End If
        Else
            Cancel = True
        End If
    Consider revising logic so users can correct their entry mistakes without loosing other changes that are valid. Also, asking them to save every time can get tiresome. Instead, have an Undo/cancel button so they can get out without saving. If they click [Close] or close the form, then validate and give message if some input is missing and Undo the update - allowing them to correct the mistakes or simply Undo/Cancel the editing themselves.

  4. #4
    dweekley is offline Advanced Beginner
    Windows XP Access 2002 (version 10.0)
    Join Date
    May 2010
    Posts
    52
    Ah...so having the Me.Dirty left me no option but to deal with it. No need to have it if I am attempting to save and/or exit anyways.

    Thank you much.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I believe you are on your way to success!

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

Similar Threads

  1. Validating form data
    By dweekley in forum Access
    Replies: 5
    Last Post: 05-16-2017, 06:53 AM
  2. Validating data against two tables
    By MTSPEER in forum Programming
    Replies: 3
    Last Post: 03-30-2015, 10:48 AM
  3. Validating Data on Form Entry
    By JoeM in forum Access
    Replies: 6
    Last Post: 09-23-2013, 02:13 PM
  4. Validating data entry in a form
    By bdhFS in forum Programming
    Replies: 1
    Last Post: 05-18-2010, 03:09 PM
  5. Validating Field Data Across Tables?
    By venomshot in forum Forms
    Replies: 4
    Last Post: 02-04-2010, 05:04 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