RP1America
Technical User
This userform procedure seems to run out of order when the validation peice (first two if statements) is invoked.
Validator states that either optNoAct or optNoFund must be selected. Another states that text must be entered in txtSection.
When these things are done, everything works fine. However, when say no text is entered in txtSection, things go haywire. The validator itself seems to work okay, hiding the form, showing the message box, then showing the form again. The issue comes when you then do enter text into txtSection and click Submit, there is an error ("Run-time error '5941': The requested member of the collection does not exist.").
When debug is selected, this line is highlighted:
It "does not exist" because the macro has already entered today's date in "CurrentDate" and therefore has deleted that bookmark.
My question is, why does the code seem sort of 'loop' through the rest of the procedure during validating the selections, then once those selections are made, the code has already been through where it is erroring out now?
Thanks in advance!!
Code:
Private Sub cmdSubmit_Click()
If optNoAct.Value = False And optNoFund.Value = False Then
frmCASAT.Hide
MsgBox "Please Select Contract Termination Type", 0, "Field Must Be Valued"
frmCASAT.Show
End If
If txtSection.Value = "" Then
frmCASAT.Hide
MsgBox "Please Value Contract Termination Section", 0, "Field Must Be Valued"
frmCASAT.Show
End If
Dim strYearAgoToday As String
strYearAgoToday = DateAdd("yyyy", -1, Date)
Dim strLastYear As String
strLastYear = DatePart("yyyy", strYearAgoToday)
With ActiveDocument
.Bookmarks("CurrentDate").Range.Text = Format(Date, "MMMM d, yyyy")
.Bookmarks("CTS").Range.Text = txtSection.Value
.Bookmarks("Days30").Range.Text = DateAdd("d", 30, Date)
.Bookmarks("Current1").Range.Text = DatePart("yyyy", Date)
.Bookmarks("Current2").Range.Text = DatePart("yyyy", Date)
.Bookmarks("Current3").Range.Text = DatePart("yyyy", Date)
.Bookmarks("Current4").Range.Text = DatePart("yyyy", Date)
.Bookmarks("Current5").Range.Text = DatePart("yyyy", Date)
.Bookmarks("Prior1").Range.Text = strLastYear
.Bookmarks("Prior2").Range.Text = strLastYear
.Bookmarks("Prior3").Range.Text = strLastYear
.Bookmarks("Prior4").Range.Text = strLastYear
End With
If optNoAct = True Then
With ActiveDocument
.Bookmarks("NeverFunded").Range.Text = ""
End With
ElseIf optNoFund = True Then
With ActiveDocument
.Bookmarks("NoActivity").Range.Text = ""
End With
End If
If chkNonDis = False And chkAnnRep = False And chkForm5500 = False Then
With ActiveDocument
.Bookmarks("PFRK").Range.Text = ""
End With
ElseIf chkNonDis = False And chkAnnRep = True And chkForm5500 = True Then
With ActiveDocument
.Bookmarks("NonDis1").Range.Text = ""
.Bookmarks("NonDis2").Range.Text = ""
End With
ElseIf chkNonDis = True And chkAnnRep = False And chkForm5500 = True Then
With ActiveDocument
.Bookmarks("NoAnnRep").Range.Text = "; and"
.Bookmarks("AnnRep1").Range.Text = ""
.Bookmarks("AnnRep2").Range.Text = "."
End With
ElseIf chkNonDis = True And chkAnnRep = True And chkForm5500 = False Then
With ActiveDocument
.Bookmarks("Form55001").Range.Text = ""
.Bookmarks("Form55002").Range.Text = "."
End With
ElseIf chkNonDis = False And chkAnnRep = False And chkForm5500 = True Then
With ActiveDocument
.Bookmarks("NonDis1").Range.Text = ""
.Bookmarks("AnnRep1").Range.Text = ""
.Bookmarks("AnnRep2").Range.Text = "."
.Bookmarks("NonDis2").Range.Text = ""
End With
ElseIf chkNonDis = False And chkAnnRep = True And chkForm5500 = False Then
With ActiveDocument
.Bookmarks("NonDis1").Range.Text = ""
.Bookmarks("Form55001").Range.Text = ""
.Bookmarks("AnnRepOnly").Range.Text = ""
End With
ElseIf chkNonDis = True And chkAnnRep = False And chkForm5500 = False Then
With ActiveDocument
.Bookmarks("NonDisOnly").Range.Text = ""
.Bookmarks("Form55002").Range.Text = "."
End With
End If
Application.Visible = True
Unload Me
End Sub
Validator states that either optNoAct or optNoFund must be selected. Another states that text must be entered in txtSection.
When these things are done, everything works fine. However, when say no text is entered in txtSection, things go haywire. The validator itself seems to work okay, hiding the form, showing the message box, then showing the form again. The issue comes when you then do enter text into txtSection and click Submit, there is an error ("Run-time error '5941': The requested member of the collection does not exist.").
When debug is selected, this line is highlighted:
Code:
.Bookmarks("CurrentDate").Range.Text = Format(Date, "MMMM d, yyyy")
It "does not exist" because the macro has already entered today's date in "CurrentDate" and therefore has deleted that bookmark.
My question is, why does the code seem sort of 'loop' through the rest of the procedure during validating the selections, then once those selections are made, the code has already been through where it is erroring out now?
Thanks in advance!!