Search code examples
excelvbadrop-down-menudropdown

Use Like operator on a loop on dropdown menu


https://i.sstatic.net/3D3xS.png I have 13 dropdown menues. The for loop works(Checks to make sure I don't select the same device and number more than once). Trying to solve the If statements at the end, validating if when I select device A, B or "channel not available" that the two text inputs have values in them. 1) If device A is selected, verify that a number is provided in first text input box. 2) If device B is selected, verify that a number is provided for the second text input box. 3) Verify that at least a number is provided for either device a or b text input box. Currently, my messages are popping up, but when I click the Next button, nothing happens? Even if I enter values in the text input and click next, nothing seems to be happening. I figured it is the structure of my If statements, need some help creating the validation. Any thoughts?

If (HTSelection.DeviceDropDown1.List(0)) <> Empty Then
        
    Else
        DeviceDropDown1.AddItem "Device A: HT 1"
        DeviceDropDown1.AddItem "Device A: HT 2"
        DeviceDropDown1.AddItem "Device A: HT 3"
        DeviceDropDown1.AddItem "Device A: HT 4"
        DeviceDropDown1.AddItem "Device A: HT 5"
        DeviceDropDown1.AddItem "Device A: HT 6"
        DeviceDropDown1.AddItem "Device A: HT 7"
        DeviceDropDown1.AddItem "Device A: HT 8"
        DeviceDropDown1.AddItem "Device B: HT 1"
        DeviceDropDown1.AddItem "Device B: HT 2"
        DeviceDropDown1.AddItem "Device B: HT 3"
        DeviceDropDown1.AddItem "Device B: HT 4"
        DeviceDropDown1.AddItem "Device B: HT 5"
        DeviceDropDown1.AddItem "Device B: HT 6"
        DeviceDropDown1.AddItem "Device B: HT 7"
        DeviceDropDown1.AddItem "Device B: HT 8"
        DeviceDropDown1.AddItem "Channel_Not_Available"
    End If
    End Sub

Private Sub HTNextButton_Click()
    On Error Resume Next
    
    DDi = 1
    DDj = 1
    Numberflag = 0
    DeviceFlagA = 0
    DeviceFlagB = 0
    For DDi = 1 To 13
        Device = Me.Controls.Item("DeviceDropDown" & DDi)
        If Device = "Channel_Not_Available" Then
        ElseIf Device = "Select Device" Then
            MsgBox "Please select Number channel for Device" & DDi, vbCritical, "Error"
            Exit For
        Else
            If InStr(1, Device, "Device A") Then
                DeviceFlagA = 1
            End If
            If InStr(1, Device, "Device B") Then
                DeviceFlagB = 1
            End If
                
            For DDj = 1 To 13
            
                If DDi <> DDj Then
                    Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
                    If Device1 = "Channel_Not_Available" Then
                    Else
                        If Device = Device1 Then
                            MsgBox "Please select different number for Device" & DDj, vbCritical, "Error"
                            Numberflag = Numberflag + 1
                        Exit For
                    End If
                End If
    End If
            Next
            If Numberflag >= 1 Then
                Exit For
            End If
        End If
        
    Next

        If DeviceFlagA = 1 Then
         If HTSelection.DeviceSAInput.Text <> Empty Then
          Else
            MsgBox "Please enter valid number for device A", vbCritical, "Error"
            End If

        If DeviceFlagB = 1 Then
         If HTSelection.DeviceSAInputB.Text <> Empty Then
          Else
            MsgBox "Please enter valid number for device B", vbCritical, "Error"
            End If

        If Numberflag = 0 Then
         If (HTSelection.DeviceSAInput.Text <> Empty Or HTSelection.DeviceSAInputB <> Empty) Then
            Number = HTSelection.DeviceSAInput.Text
            Numberb = HTSelection.DeviceSAInputB.Text
              Set clientNumber = CreateObject("Device.usb")
              Set clientNumberb = CreateObject("Deviceb.usb")
            End If
            Me.Hide
            Chart.Show

            Else
            MsgBox "Please enter valid number", vbCritical, "Error"
            End If
   End If
End Sub

Solution

  • I took your Sub HTNextButton_Click() and made some corrections in the if-then-else statements. Please check my comments as I don't know all your requirements and conditions.

    Private Sub HTNextButton_Click()
        Dim Numberflag As Boolean
        
    '    On Error Resume Next
        
    ' not required
    '    DDi = 1
    '    DDj = 1
    
        Numberflag = False
        DeviceFlagA = 0
        DeviceFlagB = 0
        
        For DDi = 1 To 13
            Device = Me.Controls.Item("DeviceDropDown" & DDi)
            
            If Device = "Select Device" Then
                MsgBox "Please select Number channel for Device" & DDi, vbCritical, "Error"
                Exit For
            End If
            
            If Device = "Channel_Not_Available" Then
    ' Do nothing (?)
            Else
           
    ' Determine if it is Device A or B
                If InStr(1, Device, "Device A") Then
                    DeviceFlagA = 1
                End If
                
                If InStr(1, Device, "Device B") Then
                    DeviceFlagB = 1
                End If
                    
    ' Check that the selection is not duplicate
                For DDj = 1 To 13
                    If DDi <> DDj Then
                        Device1 = Me.Controls.Item("DeviceDropDown" & DDj)
    ' The case of Device1 = "Channel_Not_Available" is also covered with the comparison
    ' as we should never get here with Device = "Channel_Not_Available"
                        
    ' If duplicate entry found, leave the for-next loop DDj
                        If Device = Device1 Then
                            MsgBox "Please select different number for Device" & DDj, vbCritical, "Error"
                            Numberflag = True
                            Exit For
                        End If
                    End If
                Next DDj
                
    ' if any of the entries is duplicate, leave the for-next loop DDi
                If Numberflag Then
                    Exit For
                End If
            End If
            
        Next DDi
    
    ' Do you need to execute the operations for "DeviceFlag" if Numberflag =True? What happens in case of "Select Device"?
    
        If DeviceFlagA = 1 Then
            If HTSelection.DeviceSAInput.Text = "" Then
                MsgBox "Please enter valid number for device A", vbCritical, "Error"
            Else
                Number = HTSelection.DeviceSAInput.Text
            End If
    
        End If
        
        If DeviceFlagB = 1 Then
            If HTSelection.DeviceSAInputB.Text = "" Then
                MsgBox "Please enter valid number for device B", vbCritical, "Error"
            Else
                Numberb = HTSelection.DeviceSAInputB.Text
            End If
        End If
    
        If Numberflag = False Then
    ' I guess you only want those objects where the flag is set. consider moving them into the if statements above
            If DeviceFlagA = 1 Then
               Set clientNumber = CreateObject("Device.usb")
            End If
            
            If DeviceFlagB = 1 Then
                Set clientNumberb = CreateObject("Deviceb.usb")
            End If
            Me.Hide
            Chart.Show
        Else
            MsgBox "Please enter valid number", vbCritical, "Error"
        End If
    End Sub