Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50103

Disabling dependent combobox2 until selection is made in combobox1

$
0
0
Hello All,

I am new on this forum and should be grateful if you would, please, help me with a problem regarding dependent comboboxes.

I have a database that uses a userform to input data into it.
The userform has a few text boxes and two comboboxes – combobox1 and combobox2. Comboxbox2 is dependent on Combobox1.

The userform has the ‘Date’ textbox at the top, followed by Combobox1 and Combobox2, respectively. And at the bottom of the userform, I have the ‘ADD’ and ‘CLOSE’ buttons.

The VBA codes that I have to transfer data from the userform onto the database works fine, but for a nagging problem with the comboboxes!

When the userform is opened for the first time, Combobox2 is disabled. When a user clicks its drop-down without first choosing from Combobox1, nothing happens and no list shows. This is fine and that’s what I want.

However, a user keys data into the userform and selects from both comboboxes, gets to the end of the userform and clicks the‘ADD’ button. Everything is OK and the cursor goes back to the ‘Date’ textbox..

At this point, Combobox2 is no longer disabled. After the ‘Add’ button is clicked and the cursor goes back to ‘Date’, if a user clicks on the Combobox2 drop-down without first clicking on the Combobox1 drop-down, the list for combobox2 appears! This should not be the case. At this point, Combobox2 is no longer dependent on Combobox1.

The problem with this is that a user can at this point first make a selection from Combobox2, and then select from Combobox1 – and the two selections will not be related.

I need an adjustment to my Code that will ensure that at all times, Combobox2 drop-down will be disabled (will not show anything) UNTIL a selection is made from Combobox1 drop-down.

Finally, when the cursor is showing on the ‘Date’ textbox – i.e. the first box on the userform, the two comboboxes appears blank. Please, I also need a code added to ensure that a user cannot manually type anything into the blank spaces of any of the two.comboboxes. Any such attempt should trigger an error message.

I have included below the code that I use:
Code:

'*********** POPULATE SUB-CATEGORY COMBOBOX*****************

Private Sub cboMainCate_Change()
    Dim cSubCate As Range
    Dim ws As Worksheet
    Set ws = Worksheets("LookupLists")

    Select Case cboMainCate

    Case "Suppliers"
        cboSubCate.List = ws.Range("Supp_List").Value

    Case "Product1"
        cboSubCate.List = ws.Range("Prod1_List").Value

    Case "Product2"
        cboSubCate.List = ws.Range("Prod2_List").Value

    Case "Location"
        cboSubCate.List = ws.Range("Loc_List").Value
End Select

End Sub
 
''***********POPULATE CATEGORIES IN USERFORM**************
'
Private Sub UserForm_Initialize()
    Dim cCategory As Range
    Dim ws As Worksheet
    Set ws = Worksheets("LookupLists")


    For Each cCategory In ws.Range("PRIM_CATEGORY_List")
        With Me.cboMainCate
        .AddItem cCategory.Value
        .List(.ListCount - 1, 1) = cCategory.Offset(0, 1).Value
        End With
    Next cCategory
       
  '  Me.CboMainCate.Value = ""
 
End Sub

Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")

'find first empty row in database
    On Error Resume Next
   
    iRow = ws.Cells(Rows.Count, 1) _
      .End(xlUp).Offset(1, 0).Row
       
Dim Temp
Temp = Me.txtDate.Value
Date = DateSerial(Year(Temp), Day(Temp), Month(Temp))

' Check user input

If Me.txtDate.Value = "" Then
MsgBox "Enter correct date", vbExclamation, "Data Entry Error"
Me.txtDate.SetFocus
Exit Sub
End If
If Me.txtAmount.Value = "" Then
MsgBox "Enter Amount.", vbExclamation, "Data Entry Error"
Me.txtAmount.SetFocus
Exit Sub
End If
If Not IsDate(Me.txtDate.Value) Then
MsgBox "The 'Date' box must contain a date", vbExclamation, "Data Entry Error"
Me.txtDate.SetFocus
Exit Sub
End If
If Not IsNumeric(Me.txtAmount.Value) Or Me.txtAmount.Value <= 0 Then
MsgBox "You Must enter Amount.", vbExclamation, "Data Entry Error"
Me.txtAmount.SetFocus
Exit Sub
End If

'copy the data to the database
    ws.Cells(iRow, 1).Value = Format(Me.txtDate, "dd/mm/yyyy")
    ws.Cells(iRow, 2).Value = Me.cboMainCate.Text
    ws.Cells(iRow, 3).Value = Me.cboSubCate.Text
    ws.Cells(iRow, 4).Value = Me.txtDes.Text
    ws.Cells(iRow, 5).Value = Me.txtAmount.Value


'clear the data
    Me.txtDate.Value = ""
    Me.cboMainCate.Value =
    Me.cboSubCate.Value =
    Me.txtDes.Value = ""
    Me.txtAmount.Value = ""
    Me.txtDate.SetFocus

End Sub


Private Sub txtdate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtDate = vbNullString Then Exit Sub
If IsDate(Replace(txtDate, ".", "/")) Then
txtDate = Format(Replace(txtDate, ".", "/"), "dd/mm/yyyy")
Else
MsgBox "Not a valid date", vbExclamation, "Data Entry Error"
Cancel = True
End If
End Sub

Private Sub txtAmount_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    txtAmount.Value = Format(txtAmount.Value, "0.00")
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Use Close Button", vbExclamation, "Stop!"
End If
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

Sub PrepareFormat()
    Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Range(Cells(LastRow, "A"), Cells(LastRow, "G")).Copy
    Cells(LastRow + 1, "A").PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    End If
End Sub

Other than those two issues, the code works OK for me. I'm still learning and prepared to learn more.

I'll appreciate any help from members of the forum.

Thanks.

Newqueen

Viewing all articles
Browse latest Browse all 50103

Trending Articles