Consulting

Results 1 to 7 of 7

Thread: Validation Code Problem

  1. #1
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location

    Exclamation Validation Code Problem

    Ok so I have a 2 page Userform when the first page contains textboxes, comboboxes, and dropdowns, and the second has a listbox, textboxes, and buttons. After data is put in, the userform (current version being used) unloads. The problem (on a user-friendly) standpoint, is that it is a mail log, with potentially hundreds of entries per day. I was trying to speed up the process, by using the click event of the enter button to insert the data, and then reload the form, but the validation code for the first textbox was tripped. Now if I remove the “SetFocus” property, the validation code isn’t tripped, but like I said this is about making the form even more User-Friendly, so I need the SetFocus property. Now we could just say ignore the validation, but I use “rgbPink” for validation, along with caption changes. This is can intrusive when you reload the form and expect a fresh start. I also want to maintain the validation code as is because this is all pertinent data.

    Below is the code that pertains to the issue:

    Private Sub EnterButton_Click()
     
        If Not EHSShowError Then Exit Sub
       
        Call AddDataToList
        If MsgBox("Do you have another Tier II to log?", vbYesNo) = vbYes Then
            Tier2Pages.Value = 0
            DateSubmitted.Value = ""
            BusinessName.Value = ""
            Address.Value = ""
            ZipCode.Value = ""
            County.Value = ""
            SubjectComboBox.Value = ""
            SelectedEHSList.Value = ""
            DateSubmitted.SetFocus
        Else
            Unload Me
            wsMenu.Activate
            wsMenu.Select
        End If
       
    End Sub
     
    Private Sub AddDataToList()
       
        Dim LastRow As Long
        Dim ws As Worksheet
       
        Set ws = wsTierII
       
        LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
       
        ws.Range("A" & LastRow).Value = DateSubmitted.Value
        ws.Range("A" & LastRow).Offset(0, 1).Value = BusinessName.Value
        ws.Range("A" & LastRow).Offset(0, 2).Value = Address.Value
        ws.Range("A" & LastRow).Offset(0, 3).Value = City.Value
        ws.Range("A" & LastRow).Offset(0, 4).Value = State.Value
        ws.Range("A" & LastRow).Offset(0, 5).Value = ZipCode.Value
        ws.Range("A" & LastRow).Offset(0, 6).Value = County.Value
        ws.Range("A" & LastRow).Offset(0, 7).Value = SubjectComboBox.Value
        ws.Range("A" & LastRow).Offset(0, 8).Value = SelectedEHSList.Value
        ws.Range("A" & LastRow).Offset(0, 9).Value = YesOrNo(ePlanCheckbox.Value)
        ws.Range("A" & LastRow).Offset(0, 10).Value = DateFiled.Value
       
        Application.ScreenUpdating = True
       
        wsTierII.Activate
        wsTierII.Select
       
        MsgBox BusinessName.Value & " was added to Tier II Log!"
       
    End Sub
     
    Private Sub DateSubmitted_AfterUpdate()
     
        If DateSubmitted.Value <> "" Then
            DateSubmitted.BackColor = rgbWhite
            DateSubmittedLabel.ForeColor = Me.ForeColor
            DateSubmittedLabel.Caption = "Date Submitted"
        End If
       
        If IsDate(DateSubmitted.Value) Then
            DateSubmitted.Value = Format(CDate(DateSubmitted.Value), "M/D/YY")
        End If
       
    End Sub
     
    Private Sub DateSubmitted_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
     
        Dim ActualDate As Date
       
        If Not IsDate(DateSubmitted.Value) Then
            Call DateSubmittedShowError
            Cancel = True
        Else
            ActualDate = CDate(DateSubmitted.Value)
           
            If ActualDate >= Date Then
                Call DateSubmittedShowError
                Cancel = True
            End If
        End If
       
    End Sub
     
    Private Sub DateSubmittedShowError()
     
        DateSubmitted.BackColor = rgbPink
        DateSubmittedLabel.ForeColor = rgbRed
        DateSubmittedLabel.Caption = "Submission Date on Form"
        DateSubmitted.SelStart = 0
        DateSubmitted.SelLength = Len(DateSubmitted.Value)
     
    End Sub
    Last edited by Paul_Hossler; 02-02-2018 at 03:36 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    1. I added CODE tags for you -- use the [#] icon to insert them and them paste your macro between

    2. Might be typo but you're missing some DOTs below. Same in DateSubmitted_AfterUpdate()

    Private Sub DateSubmittedShowError() 
         
        DateSubmitted.BackColor = rgbPink 
        DateSubmittedDOTLabel.ForeColor = rgbRed 
        DateSubmittedDOTLabel.Caption = "Submission Date on Form" 
        DateSubmitted.SelStart = 0 
        DateSubmitted.SelLength = Len(DateSubmitted.Value) 
         
    End Sub


    3. You can save some typing and make it a a little easier to read by using With ... End With


    Private Sub DateSubmittedShowError() 
         
        With DateSubmitted.
            .BackColor = rgbPink 
            .Label.ForeColor = rgbRed 
            .Label.Caption = "Submission Date on Form" 
             .SelStart = 0 
              .SelLength = Len(DateSubmitted.Value) 
        End With
         
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    No dots were missing. My labels are named DateSubmittedLabel. And thanks for the tip on how to insert code, and not have it look haphazard.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by cpounds217 View Post
    No dots were missing. My labels are named DateSubmittedLabel.
    Yea, I didn't look close enough
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    Does anyone have any other input on this?

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Have you tried to set the .Enabled property of the Enter button to false until all the data is validated as OK?

  7. #7
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    It wasn’t a problem with the enter button itself. It was the validation was activated when the userform would auto-reload. So my current solve has been to add the validation to the enter button click and remove it from the BeforeUpdate sub. This is working for now, so unless, someone has a better suggestion, I would say this can be marked as solve.

    Thanks for all the help and suggestions, though!

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
  •