Используйте оператор Like на al oop в раскрывающемся меню - PullRequest
0 голосов
/ 19 июня 2020

https://i.stack.imgur.com/3D3xS.png У меня есть 13 выпадающих меню. Для l oop работает (проверяет, не выбираю ли я одно и то же устройство и номер более одного раза). Попытка решить операторы If в конце, проверяя, если при выборе устройства A, B или «канал недоступен», два текстовых поля ввода содержат значения. 1) Если выбрано устройство A, убедитесь, что номер указан в первом поле ввода текста. 2) Если выбрано устройство B, убедитесь, что для второго поля ввода текста указан номер. 3) Убедитесь, что для поля ввода текста устройства a или b указан хотя бы номер. В настоящее время появляются мои сообщения, но когда я нажимаю кнопку «Далее», ничего не происходит? Даже если я ввожу значения в текстовый ввод и нажимаю «Далее», кажется, что ничего не происходит. Я решил, что это структура моих операторов If, мне нужна помощь в создании проверки. Есть мысли?

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

Ответы [ 2 ]

0 голосов
/ 01 июля 2020

Я взял ваш Sub HTNextButton_Click () и внес некоторые исправления в операторы if-then-else. Пожалуйста, проверьте мои комментарии, так как я не знаю всех ваших требований и условий.

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
0 голосов
/ 19 июня 2020

Это еще не ответ, а показатель, по которому нужно исследовать дальше. Я взял вашу конструкцию «FOR» и структурировал ее так, чтобы вы могли видеть, где блок начинается и где он заканчивается. Отсюда вы можете видеть, что в той части кода, которую вы предоставили, почти все «End If», а также «Next Hti» отсутствуют. Я предполагаю, что они есть, но если нет, то ясно, откуда взялась ошибка.

For Hti = 1 To 13
' If you want to check 2 dropdowns you must assign them to 2 different variables
    Device = Me.Controls.Item("DeviceDropDown")
' Device is a Control object, so you will need Device.Name or Device.ItemsSelected depending what you want to check
' instead of "like" you can also use Instr for searching a string in another
    If Device.ItemsSelected Like "*Device A*" And DeviceSAInput = Empty Then
        MsgBox "Please enter address for Device A"
    End If

    If Device.ItemsSelected Like "*Device B*" And DeviceSAInputB = Empty Then
        MsgBox "Please enter address for Device B"
        Exit For
    End If

' This part is missing in the code
Next Hti

Пожалуйста, проверьте еще раз свой код. В идеале обеспечивают полную функцию. Отформатируйте код, как мой фрагмент, и используйте

Option Explicit

, чтобы найти любую неопределенную переменную.

Тогда я могу еще раз взглянуть на проблему.

...