Form with a command button that is not working

lori_ann

New Member
Joined
Aug 2, 2019
Messages
5
After I fill in the three boxes with drop downs I have a pop up after I click the enter button with a message that says Qualification....Please select the inspection type from drop down. It highlights that box in red and populates this long number in the box. I'm not sure where the number comes from as it's a drop down list of text. It shouldn't even throw this message box as a inspection type was filled in.

Option Explicit


Function ValidateForm() As Boolean

monthinspected.BackColor = vbWhite
Inspector.BackColor = vbWhite
inspectiontype = vbWhite

ValidateForm = True

If monthinspected.Text <> "January" And monthinspected.Text <> "February" And monthinspected.Text <> "March" And monthinspected.Text <> "April" And monthinspected.Text <> "May" And monthinspected.Text <> "June" And monthinspected.Text <> "July" And monthinspected.Text <> "August" And monthinspected.Text <> "September" And monthinspected.Text <> "October" And monthinspected.Text <> "November" And monthinspected.Text <> "December" Then
MsgBox "Please select the correct month from drop down.,", vbOKOnly + vbInformation, "Qualification"
monthinspected.BackColor = vbRed
monthinspected.Activate
ValidateForm = False

ElseIf Inspector.Text <> "Chuck" And Inspector.Text <> "Rob" And Inspector.Text <> "Other" Then
MsgBox "Please select the correct Inspector from drop down.,", vbOKOnly + vbInformation, "Qualification"
Inspector.BackColor = vbRed
Inspector.Activate
ValidateForm = False

ElseIf inspectiontype.Text <> "Footings" And inspectiontype.Text <> "Foundation" And inspectiontype.Text <> "Underground Plumbing" And inspectiontype.Text <> "Underground Heating" And inspectiontype.Text <> "Framing" And inspectiontype.Text <> "Rough Plumbing" And inspectiontype.Text <> "Rough Heating" And inspectiontype.Text <> "Rough Electric" And inspectiontype.Text <> "Roof" And inspectiontype.Text <> "Electrical Service" And inspectiontype.Text <> "Electrical Temporary" And inspectiontype.Text <> "Final" And inspectiontype.Text <> "Pre Construction Inspection" And inspectiontype.Text <> "Courtesy Inspection" And inspectiontype.Text <> "Unsafe" Then
MsgBox "Please select the inspection type from drop down.,", vbOKOnly + vbInformation, "Qualification"
inspectiontype.BackColor = vbRed
inspectiontype.Activate
ValidateForm = False
End If

End Function
Function Reset()

Application.ScreenUpdating = False

monthinspected.Text = ""
monthinspected.BackColor = vbWhite

Inspector.Text = ""
Inspector.BackColor = vbWhite

inspectiontype = ""
inspectiontype.BackColor = vbWhite

Application.ScreenUpdating = True



End Function
Private Sub CommandButton1_Click()

Application.ScreenUpdating = False
Dim iRow As Long

iRow = Sheets("Data").Range("A2:A1048576").End(xlUp).Row + 1


If ValidateForm = True Then

With ThisWorkbook.Sheets("Data")
.Range("A" & iRow).Value = iRow - 1
.Range("B" & iRow).Value = monthinspected.Text
.Range("C" & iRow).Value = Inspector.Text
.Range("D" & iRow).Value = inspectiontype.Text

End With
Call Reset
Else
Application.ScreenUpdating = True
Exit Sub
End If
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,
bit of a guess what you are trying to do but see if these updated codes help you with your project

Make a BACKUP of your workbook & place ALL following codes in your USERFORM CODE PAGE
ensure that you delete any duplicate named code

Code:
Private Sub CommandButton1_Click()
    Dim iRow As Long
    
    If ValidateForm Then
        With ThisWorkbook.Sheets("Data")
            iRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            .Range("A" & iRow).Value = iRow - 1
            .Range("B" & iRow).Value = Me.monthinspected.Text
            .Range("C" & iRow).Value = Me.Inspector.Text
            .Range("D" & iRow).Value = Me.inspectiontype.Text
            
        End With
        Call Reset
        MsgBox "Record Submitted", 48, "Record Submitted"
    End If
End Sub


Private Sub inspectiontype_Change()
    Me.inspectiontype.BackColor = vbWhite
End Sub


Private Sub Inspector_Change()
    Me.Inspector.BackColor = vbWhite
End Sub


Private Sub monthinspected_Change()
    Me.monthinspected.BackColor = vbWhite
End Sub


Sub Reset()
    Dim DropBox As Variant
    For Each DropBox In Array(monthinspected, Inspector, inspectiontype)
        DropBox.ListIndex = -1
    Next DropBox
End Sub



Function ValidateForm() As Boolean
    Dim DropBox As Variant
    Dim i As Integer
    For Each DropBox In Array(monthinspected, Inspector, inspectiontype)
        i = i + 1
        With DropBox
            ValidateForm = CBool(.ListIndex <> -1)
            If Not ValidateForm Then
                .BackColor = vbRed
                .SetFocus
                MsgBox "Please select the correct " & Choose(i, "month", "Inspector", "inspection type") & _
                " from drop down.,", 64, "Qualification"
                Exit For
            End If
        End With
    Next DropBox
End Function

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top