Error Checking CheckBoxes - vba

I have 6 CheckBoxes right now under an Audience category and want to make it so that they have to select at least 1 of the 6 CheckBoxes or an error message saying "Please select an Audience" will appear.
Right now with the code below, the project will still be entered regardless of if they check one of the 6 boxes or not.
My current code looks like:
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.initiativeCombobox, "Please select an Initiative") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
If Not CheckControl(Me.lengthListbox, "") Then If Not CheckControl(Me.lengthListbox2, "Please enter project length") Then Exit Function
If Not CheckControl(Me.rvpCheckbox, "") Then If Not CheckControl(Me.umCheckbox, "") Then If Not CheckControl(Me.uwCheckbox, "") Then If Not CheckControl(Me.baCheckbox, "") Then If Not CheckControl(Me.uaCheckbox, "") Then If Not CheckControl(Me.otherCheckbox, "Please select an Audience") Then Exit Function
CheckInputs = True
End Function
Private Function CountSelectedListBoxItems(lb As MSForms.ListBox) As Long
Dim i As Long
With lb
For i = 0 To .ListCount - 1
If .Selected(i) Then CountSelectedListBoxItems = CountSelectedListBoxItems + 1
Next i
End With
End Function
Function CheckControl(ctrl As MSForms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
Case "ListBox"
CheckControl = CountSelectedListBoxItems(ctrl) > 0
Case "CheckBox"
CheckControl = ctrl.Value = False
' Case Else
End Select
If errMsg = "" Then Exit Function
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
Would setting a CheckControl function for CheckBox as ctrl.Value = False be the appropriate route? Or did I not set my CheckInputs function correctly?

Yes, it seems to me that (if I understand correctly) your CheckInputs function is currently incorrect.
The following line of code:
If Not CheckControl(Me.rvpCheckbox, "") Then If Not CheckControl(Me.umCheckbox, "") Then If Not CheckControl(Me.uwCheckbox, "") Then If Not CheckControl(Me.baCheckbox, "") Then If Not CheckControl(Me.uaCheckbox, "") Then If Not CheckControl(Me.otherCheckbox, "Please select an Audience") Then Exit Function
needs to be change to the following:
If UserForm1.rvpCheckbox.Value = False And _
UserForm1.umCheckbox.Value = False And _
UserForm1.uwCheckbox.Value = False And _
UserForm1.baCheckbox.Value = False And _
UserForm1.uaCheckbox.Value = False And _
UserForm1.otherCheckbox.Value = False Then
UserForm1.otherCheckbox.Caption = "Please select an Audience"
'...or maybe a message box instead?
MsgBox "Please select an Audience"
Exit Function
End If

Related

Message box for null fields

I am trying to create a database for part locations. Three of the fields (JobNumber, PartNumber and Location) are required fields and I have an If statement written to check for Nulls and error handling to give a message box. After the message box is closed, it will let me return to the form but it will not allow me to edit the field again AND it still adds the incomplete data to the table. Any advice?
Private Sub cmdAddNew_Click()
If IsNull(Me.JobNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.PartNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.Location) Then
GoTo cmdAddNew_Click_Err
End If
On Error GoTo cmdAddNew_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
cmdAddNew_Click_Exit:
Exit Sub
cmdAddNew_Click_Err:
MsgBox "Job Number, Part Number and Location are required."
End Sub
Adding Cancel = True in your second if statement should fix this.
Private Sub cmdAddNew_Click()
If IsNull(Me.JobNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.PartNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.Location) Then
GoTo cmdAddNew_Click_Err
End If
On Error GoTo cmdAddNew_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Cancel = True
End If
cmdAddNew_Click_Exit:
Exit Sub
cmdAddNew_Click_Err:
MsgBox "Job Number, Part Number and Location are required."
End Sub
Alternatively, you could define functions to check for a null value and return a boolean result:
Function isvalid(Field as string) As Boolean
If IsNull(field)
isvalid = False
Else
isvalid = True
End If
End Function
Sub Check_valid()
Call isvalid(field 1)
Call isvalid(f2)
Call isvalid(f3)
If isvalid(field 1) = false Or isvalid(f2) = false Or isvalid(f3) = false Then
msgbox "Job Number, Part Number and Location are required."
Exit Sub
End If
End Sub
Hope this helps!

Selected Value(not selected Text) in Combobox: VBA Access

I am writing the following code to populate Combobox.
strSQL = "Select BankID, BankName As [Please Select Bank] from tblBank"
With Me.cmbBank
.RowSource = strSQL
.ColumnCount = 2
.BoundColumn = 2
.ColumnWidths = "0in.;1in."
.ColumnHeads = True
.LimitToList = True
.Requery
.Value = "Please Select Bank"
End With
on Button CLick, I am checking combobox selected value and code is here.
MsgBox Me.cmbBank.Value
It shows Text, Is there any way to get Selected Value(BankID)?
Change this to a valid value of BankID:
.Value = "Please Select Bank"
or your combo returns Null which cause the MsgBox to fail, or correct with Nz:
MsgBox Nz(Me!cmbBank.Value, "No bank selected.")
Private Function Validate() As Boolean
If IsNull(Me.cmbBank.Column(0)) = True Then
MsgBox "No bank selected"
Me.cmbBank.SetFocus
Validate = False
Exit Function
End If
Validate = True
End Function

Error Checking Two Possibilities

My current code is checking for either lengthListbox or lengthListbox2 to have a selection or it will display an error message. What's the easiest way to make it so it is a selection from either lengthListbox or lengthListbox2 or the message "Please enter project length" will be displayed?
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
If Not CheckControl(Me.lengthListbox, "Please select a current year month") Then Exit Function
If Not CheckControl(Me.lengthListbox2, "Please select a next year month") Then Exit Function
CheckInputs = True
End Function
Private Function CountSelectedListBoxItems(lb As MSForms.ListBox) As Long
Dim i As Long
With lb
For i = 0 To .ListCount - 1
If .Selected(i) Then CountSelectedListBoxItems = CountSelectedListBoxItems + 1
Next i
End With
End Function
Function CheckControl(ctrl As MSForms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
Case "ListBox"
CheckControl = CountSelectedListBoxItems(ctrl) > 0
' Case Else
End Select
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
Some things I've tried:
Trying to enter an error check in the enter button
Private Sub enterButton_Click()
If Me.lengthListbox.ListIndex = -1 And Me.lengthListbox2.ListIndex = -1 Then
MsgBox "Please enter Project Length"
End If
If Not CheckInputs Then Exit Sub 'check for fields to have values
Process GetWs(Me.impactCombobox.Value) ' process data passing the proper worksheet got from GetWs() function
MsgBox "Project Entered Successfully"
ClearUFData 'clear the data
End Sub
I've also tried making a separate subfunction that is meant for checking listboxes only but couldn't make it work within CheckInputs
try two functions little modifications:
1) nest the listboxes checks (see last line before CheckInputs = True)
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
If Not CheckControl(Me.lengthListbox, "") Then If Not CheckControl(Me.lengthListbox2, "Please enter project length") Then Exit Function
CheckInputs = True
End Function
so that the first listbox possible negative check will have the second listbox check decide about the "exit"
2) slightly modify CheckControl (see first line right after End Select)
Function CheckControl(ctrl As msforms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
Case "ListBox"
CheckControl = CountSelectedListBoxItems(ctrl) > 0
' Case Else
End Select
If errMsg = "" Then Exit Function
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
so that no action is taken if the passed error message string is a void one

Code Fluency - ErrorChecking, Click/Unclick, Pulling New Values

So here is my initial code:
Option Explicit
Private Sub clearButton_Click()
ClearUFData 'clears the form
End Sub
Private Sub enterButton_Click()
If Not CheckInputs Then Exit Sub 'check for fields to have values
Process GetWs(Me.impactCombobox.Value) ' process data passing the proper worksheet got from GetWs() function
MsgBox "Project Entered Successfully"
ClearUFData 'clear the data
End Sub
Private Sub Process(ws As Worksheet)
Dim iRow As Long
Dim MonthNumber As Byte
Dim ColumnNumber As Long: ColumnNumber = 4
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).row + 1
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
'.Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.nameTextbox.Value
.Cells(iRow, 2).Value = Me.projectTextbox.Value
.Cells(iRow, 3).Value = Me.audienceCombobox.Value
For MonthNumber = 0 To 11
If lengthListbox.Selected(MonthNumber) Then
.Cells(iRow, ColumnNumber).Value = "Yes"
Else
.Cells(iRow, ColumnNumber).Value = "No"
End If
'Increase the column Index for each time through the loop
ColumnNumber = ColumnNumber + 1
Next MonthNumber
If rvpCheckbox.Value = True Then
.Cells(iRow, 28).Value = "RVP"
End If
If uwCheckbox.Value = True Then
.Cells(iRow, 29).Value = "UW"
End If
If uaCheckbox.Value = True Then
.Cells(iRow, 30).Value = "UA"
End If
If umCheckbox.Value = True Then
.Cells(iRow, 31).Value = "UM"
End If
If baCheckbox.Value = True Then
.Cells(iRow, 32).Value = "BA"
End If
If otherCheckbox.Value = True Then
.Cells(iRow, 33).Value = "Other"
End If
End With
End Sub
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
CheckInputs = True
End Function
Function CheckControl(ctrl As MSForms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
' Case Else
End Select
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
Function GetWs(impact As String) As Worksheet
Select Case impact
Case "High"
Set GetWs = Worksheets("HI Project Database")
Case "Low"
Set GetWs = Worksheets("LI Project Database")
' Case Else
End Select
End Function
Sub ClearUFData()
Dim i As Integer
'clear the data
Me.nameTextbox.Value = ""
Me.projectTextbox.Value = ""
Me.nameTextbox.SetFocus
Me.audienceCombobox.Value = Null
Me.impactCombobox.Value = Null
Me.q1Checkbox.Value = False
Me.q2Checkbox.Value = False
Me.q3Checkbox.Value = False
Me.q4Checkbox.Value = False
For i = lengthListbox.ListCount - 1 To 0 Step -1
If lengthListbox.Selected(i) = True Then
lengthListbox.Selected(i) = False
End If
Next i
End Sub
Private Sub exitButton_Click()
' exits the form
Unload Me
End Sub
Private Sub q1Checkbox_Click()
lengthListbox.Selected(0) = True
lengthListbox.Selected(1) = True
lengthListbox.Selected(2) = True
If q1Checkbox.Value = False Then
lengthListbox.Selected(0) = False
lengthListbox.Selected(1) = False
lengthListbox.Selected(2) = False
End If
End Sub
Private Sub q2Checkbox_Click()
lengthListbox.Selected(3) = True
lengthListbox.Selected(4) = True
lengthListbox.Selected(5) = True
If q2Checkbox.Value = False Then
lengthListbox.Selected(3) = False
lengthListbox.Selected(4) = False
lengthListbox.Selected(5) = False
End If
End Sub
Private Sub q3Checkbox_Click()
lengthListbox.Selected(6) = True
lengthListbox.Selected(7) = True
lengthListbox.Selected(8) = True
If q3Checkbox.Value = False Then
lengthListbox.Selected(6) = False
lengthListbox.Selected(7) = False
lengthListbox.Selected(8) = False
End If
End Sub
Private Sub q4Checkbox_Click()
lengthListbox.Selected(9) = True
lengthListbox.Selected(10) = True
lengthListbox.Selected(11) = True
If q4Checkbox.Value = False Then
lengthListbox.Selected(9) = False
lengthListbox.Selected(10) = False
lengthListbox.Selected(11) = False
End If
End Sub
Private Sub q1Checkbox2_Click()
lengthListbox2.Selected(0) = True
lengthListbox2.Selected(1) = True
lengthListbox2.Selected(2) = True
If q1Checkbox2.Value = False Then
lengthListbox2.Selected(0) = False
lengthListbox2.Selected(1) = False
lengthListbox2.Selected(2) = False
End If
End Sub
Private Sub q2Checkbox2_Click()
lengthListbox2.Selected(3) = True
lengthListbox2.Selected(4) = True
lengthListbox2.Selected(5) = True
If q2Checkbox2.Value = False Then
lengthListbox2.Selected(3) = False
lengthListbox2.Selected(4) = False
lengthListbox2.Selected(5) = False
End If
End Sub
Private Sub q3Checkbox2_Click()
lengthListbox2.Selected(6) = True
lengthListbox2.Selected(7) = True
lengthListbox2.Selected(8) = True
If q3Checkbox2.Value = False Then
lengthListbox2.Selected(6) = False
lengthListbox2.Selected(7) = False
lengthListbox2.Selected(8) = False
End If
End Sub
Private Sub q4Checkbox2_Click()
lengthListbox2.Selected(9) = True
lengthListbox2.Selected(10) = True
lengthListbox2.Selected(11) = True
If q4Checkbox2.Value = False Then
lengthListbox2.Selected(9) = False
lengthListbox2.Selected(10) = False
lengthListbox2.Selected(11) = False
End If
End Sub
Private Sub UserForm_Initialize()
' sets name textbox to focus on load and loads combobox list
nameTextbox.SetFocus
audienceCombobox.AddItem "HR Activities/Initiatives"
audienceCombobox.AddItem "BI Underwriting"
audienceCombobox.AddItem "Product Management"
audienceCombobox.AddItem "CI Operations"
audienceCombobox.AddItem "UW Systems"
audienceCombobox.AddItem "Regional Initiatives"
audienceCombobox.AddItem "Other"
lengthListbox.AddItem "January"
lengthListbox.AddItem "February"
lengthListbox.AddItem "March"
lengthListbox.AddItem "April"
lengthListbox.AddItem "May"
lengthListbox.AddItem "June"
lengthListbox.AddItem "July"
lengthListbox.AddItem "August"
lengthListbox.AddItem "September"
lengthListbox.AddItem "October"
lengthListbox.AddItem "November"
lengthListbox.AddItem "December"
'New Year
lengthListbox2.AddItem "January"
lengthListbox2.AddItem "February"
lengthListbox2.AddItem "March"
lengthListbox2.AddItem "April"
lengthListbox2.AddItem "May"
lengthListbox2.AddItem "June"
lengthListbox2.AddItem "July"
lengthListbox2.AddItem "August"
lengthListbox2.AddItem "September"
lengthListbox2.AddItem "October"
lengthListbox2.AddItem "November"
lengthListbox2.AddItem "December"
impactCombobox.AddItem "High"
impactCombobox.AddItem "Low"
End Sub
Now, some specific areas I need to address are that I just added in a new lengthListbox2 that is an exact replica and does everything lengthListbox does, it just starts the data starting from ColumnNumber = 16 but I can't figure how to adjust the DIMs to address this.
I am also trying to figure out the proper way to error check the q1-q4 checkboxes and the lengthListbox(2) so that they have to have clicked either one of the q1-q4 boxes or one of the options in the lengthListbox.
Also another small addition I'm trying to add is that if they click the first 3 properties in the listbox (months) q1 is checked, and the next 3 then q2 is selected. Right now my code only does vice versa, if Q1 is selected then it selects the first 3 months.
Excuse the messiness in some areas, I've had a lot of help putting the code together for fluency in certain areas and my newbie skills did the rest.
EDIT:
Project length is now trying to pull a requirement from both current and next year when it just needs to be one or the other.
I've tried using this by setting up another control check but for lists specifically and separating two controlcheck situations but I need more so an or statement in there:
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
If Not CheckControlList(Me.lengthListbox, Me.lengthListbox2, "Please Select Project Length") Then Exit Function
CheckInputs = True
End Function
Private Function CountSelectedListBoxItems(lb As MSForms.ListBox) As Long
Dim i As Long
With lb
For i = 0 To .ListCount - 1
If .Selected(i) Then CountSelectedListBoxItems = CountSelectedListBoxItems + 1
Next i
End With
End Function
Function CheckControlList(ctrl As MSForms.Control, ctrl2 As MSForms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "ListBox" '<--| add the case of a ListBox control passed to check
CheckControlList = CountSelectedListBoxItems(ctrl) > 0 '<--| call new function to check listboxes
' Case Else
End Select
Select Case TypeName(ctrl2)
Case "ListBox" '<--| add the case of a ListBox control passed to check
CheckControlList = CountSelectedListBoxItems(ctrl) > 0 '<--| call new function to check listboxes
' Case Else
End Select
If CheckControlList Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
Function CheckControl(ctrl As MSForms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
Case "ListBox" '<--| add the case of a ListBox control passed to check
CheckControl = CountSelectedListBoxItems(ctrl) > 0 '<--| call new function to check listboxes
' Case Else
End Select
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
I've tried using an OR statement to separate the two as well but it just ends up displaying "Please enter Project Length" prior to saying it was successfully entered:
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
If Not (CheckControl(Me.lengthListbox) Or (CheckControl(Me.lengthListbox2)) Then MsgBox "Please Enter Project Length": Exit Function
CheckInputs = True
End Function
to process two listboxes in a similar way but with some different parameters add a ProcessListBox() sub to handle a ListBox control processing, and specify as its parameters those that can change, like:
Sub ProcessListBox(lb As msforms.ListBox, ws As Worksheet, iRow As Long, ByVal iniCol As Long)
Dim MonthNumber As Byte
With ws
For MonthNumber = 0 To 11
If lb.Selected(MonthNumber) Then
.Cells(iRow, iniCol).Value = "Yes"
Else
.Cells(iRow, iniCol).Value = "No"
End If
iniCol = iniCol + 1 'Increase the column Index for each time through the loop
Next MonthNumber
End With
End Sub
so that your processing "core" of Process() sub would be:
With ws
'.Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.nameTextbox.Value
.Cells(iRow, 2).Value = Me.projectTextbox.Value
.Cells(iRow, 3).Value = Me.audienceCombobox.Value
If rvpCheckbox Then .Cells(iRow, 28).Value = "RVP"
If uwCheckbox Then .Cells(iRow, 29).Value = "UW"
If uaCheckbox Then .Cells(iRow, 30).Value = "UA"
If umCheckbox Then .Cells(iRow, 31).Value = "UM"
If baCheckbox Then .Cells(iRow, 32).Value = "BA"
If otherCheckbox Then .Cells(iRow, 33).Value = "Other"
End With
ProcessListBox lengthListbox, ws, iRow, 4 '<--| process lengthListbox passing "4" as starting column
ProcessListBox lengthListbox2, ws, iRow, 16 '<--| process lengthListbox2 passing "16" as starting column
as for the listboxes and corresponding checkboxes cross checking, since you actually only care about the former, just check listboxes for any selected value
and you could that by means of a specific Function like:
Private Function CountSelectedListBoxItems(lb As msforms.ListBox) As Long
Dim i As Long
With lb
For i = 0 To .ListCount - 1
If .Selected(i) Then CountSelectedListBoxItems = CountSelectedListBoxItems + 1
Next i
End With
End Function
to add to your CheckControl() function Case block:
Function CheckControl(ctrl As msforms.Control, errMsg As String) As Boolean
Select Case TypeName(ctrl)
Case "TextBox"
CheckControl = Trim(ctrl.Value) <> ""
Case "ComboBox"
CheckControl = ctrl.ListIndex <> -1
Case "ListBox" '<--| add the case of a ListBox control passed to check
CheckControl = CountSelectedListBoxItems(ctrl) > 0 '<--| call new function to check listboxes
' Case Else
End Select
If CheckControl Then Exit Function
ctrl.SetFocus
MsgBox errMsg
End Function
and therefore update your CheckInputs() function
Function CheckInputs() As Boolean
If Not CheckControl(Me.nameTextbox, "Please enter your name") Then Exit Function
If Not CheckControl(Me.projectTextbox, "Please enter a Project Name") Then Exit Function
If Not CheckControl(Me.audienceCombobox, "Please select an Audience") Then Exit Function
If Not CheckControl(Me.impactCombobox, "Please select Impact Type") Then Exit Function
If Not CheckControl(Me.lengthListbox, "Please select a current year month") Then Exit Function '<--| check "lengthListbox"
If Not CheckControl(Me.lengthListbox2, "Please select a next year month") Then Exit Function '<--| check "lengthListbox2"
CheckInputs = True
End Function
I also think you need to update your ClearUFData() sub to handle both lengthListbox and lengthListbox2. and qXCheckbox2, too...
Sub ClearUFData()
'clear the data
With Me
.nameTextbox.Value = ""
.projectTextbox.Value = ""
.nameTextbox.SetFocus
.audienceCombobox.Value = Null
.impactCombobox.Value = Null
.q1Checkbox.Value = False
.q2Checkbox.Value = False
.q3Checkbox.Value = False
.q4Checkbox.Value = False
.q1Checkbox2.Value = False '<-- uncheck q1Checkbox2
.q2Checkbox2.Value = False '<-- uncheck q2Checkbox2
.q3Checkbox2.Value = False '<-- uncheck q3Checkbox2
.q4Checkbox2.Value = False '<-- uncheck q4Checkbox2
DeselectListBox lengthListbox '<-- deselect all listbox items
DeselectListBox lengthListbox2 '<-- deselect all lkistbox items
End With
End Sub
where I'd use a DeselectListBox() sub like follows:
Private Sub DeselectListBox(lb As msforms.ListBox)
Dim i As Long
With lb
For i = 0 To .ListCount - 1
.Selected(i) = False
Next i
End With
End Sub
Finally some little improvements
checkbox control default property is Value that returns True or False whether it is checked or not
so instead of
If q1Checkbox.Value = False Then
you can simply use
If Not q1Checkbox Then
and the likes
this can have you simplify and shorten the following code:
Private Sub q1Checkbox_Click()
lengthListbox.Selected(0) = True
lengthListbox.Selected(1) = True
lengthListbox.Selected(2) = True
If Not q1Checkbox Then
lengthListbox.Selected(0) = False
lengthListbox.Selected(1) = False
lengthListbox.Selected(2) = False
End If
End Sub
Private Sub q2Checkbox_Click()
lengthListbox.Selected(3) = True
lengthListbox.Selected(4) = True
lengthListbox.Selected(5) = True
If Not q2Checkbox Then
lengthListbox.Selected(3) = False
lengthListbox.Selected(4) = False
lengthListbox.Selected(5) = False
End If
End Sub
Private Sub q3Checkbox_Click()
lengthListbox.Selected(6) = True
lengthListbox.Selected(7) = True
lengthListbox.Selected(8) = True
If Not q3Checkbox Then
lengthListbox.Selected(6) = False
lengthListbox.Selected(7) = False
lengthListbox.Selected(8) = False
End If
End Sub
Private Sub q4Checkbox_Click()
lengthListbox.Selected(9) = True
lengthListbox.Selected(10) = True
lengthListbox.Selected(11) = True
If Not q4Checkbox Then
lengthListbox.Selected(9) = False
lengthListbox.Selected(10) = False
lengthListbox.Selected(11) = False
End If
End Sub
Private Sub q1Checkbox2_Click()
lengthListbox2.Selected(0) = True
lengthListbox2.Selected(1) = True
lengthListbox2.Selected(2) = True
If Not q1Checkbox2 Then
lengthListbox2.Selected(0) = False
lengthListbox2.Selected(1) = False
lengthListbox2.Selected(2) = False
End If
End Sub
Private Sub q2Checkbox2_Click()
lengthListbox2.Selected(3) = True
lengthListbox2.Selected(4) = True
lengthListbox2.Selected(5) = True
If q2Checkbox2.Value = False Then
lengthListbox2.Selected(3) = False
lengthListbox2.Selected(4) = False
lengthListbox2.Selected(5) = False
End If
End Sub
Private Sub q3Checkbox2_Click()
lengthListbox2.Selected(6) = True
lengthListbox2.Selected(7) = True
lengthListbox2.Selected(8) = True
If Not q3Checkbox2 Then
lengthListbox2.Selected(6) = False
lengthListbox2.Selected(7) = False
lengthListbox2.Selected(8) = False
End If
End Sub
Private Sub q4Checkbox2_Click()
lengthListbox2.Selected(9) = True
lengthListbox2.Selected(10) = True
lengthListbox2.Selected(11) = True
If Not q4Checkbox2 Then
lengthListbox2.Selected(9) = False
lengthListbox2.Selected(10) = False
lengthListbox2.Selected(11) = False
End If
End Sub
UserForm_Initialize can be also made more "codeable" and readable with the use of With statement as follows:
Private Sub UserForm_Initialize()
' sets name textbox to focus on load and loads combobox list
nameTextbox.SetFocus
With audienceCombobox
.AddItem "HR Activities/Initiatives"
.AddItem "BI Underwriting"
.AddItem "Product Management"
.AddItem "CI Operations"
.AddItem "UW Systems"
.AddItem "Regional Initiatives"
.AddItem "Other"
End With
With lengthListbox
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
End With
'New Year
With lengthListbox2
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
End With
With impactCombobox
.AddItem "High"
.AddItem "Low"
End With
End Sub

How to Separate commands on save

This code is working but now i want if all the textboxes(Tbxs) are filled but the picturebox (Pbx1) contains no image the Msgbox to show"Now Load Photo"
If Tbx1.Text = "" Or Tbx2.Text = "" Or Tbx3.Text = "" Or Tbx4.Text = "" Or Pbx1.Image Is Nothing Then
MsgBox("Kindly Fill the Blank Cells", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly)
Exit Sub
End If
How will i do that?
Is this what you mean?
If (Tbx1.Text = "" Or Tbx2.Text = "" Or Tbx3.Text = "" Or Tbx4.Text = "") And Pbx1.Image Is Nothing Then
MsgBox("Kindly Fill the Blank Cells", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly)
Exit Sub
ElseIf CheckFilled({Tbx1, Tbx2, Tbx3, Tbx4}) And Pbx1.Image Is Nothing Then
MsgBox("Now Load Photo", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly)
End If
Private Function CheckFilled(Tb As TextBox()) As Boolean
Dim retVal As Boolean = True
For Each t As TextBox In Tb
If t.Text.Length <= 0 Then retVal = False
Next
Return retVal
End Function