如何在另一个表格的组合框中循环?

时间:2013-10-13 06:54:57

标签: vb6

我正在寻找有关我的代码的一些建议/提示/更正,为什么它无法正常工作。

我有2个表格 带有菜单栏的Form1(添加类型菜单栏),我想让它循环通过组合框列表(以检查列表中是否可以添加现有数据)。 我不明白为什么它不能在form1上工作?当我使用1表格在其他项目上测试我的代码时,它可以工作。有人能告诉我什么是错的吗?为什么?

使用2个表格。此代码仅添加新类型,但不检查组合框中是否存在数据:(

Private Sub mnuAYT_Click()
Dim TypeYacht As String 'Type of yacht added
Dim blnItem As Boolean
Dim intItem As Integer

' - - - - - - - LOOP THROUGHT the combo box all items - - - - - - -

blnItem = False
intItem = 0
Do Until blnItem = True Or intItem = NewCharter.cmbTypeYacht.ListCount
    If TypeYacht = NewCharter.cmbTypeYacht.List(intItem) Then
        blnItem = True
    End If
    intItem = intItem + 1
Loop
    If blnItem = True Then
     MsgBox TypeYacht & " " & "is already exist", vbInformation, "Yacht Type Match"
     NewCharter.cmbTypeYacht.ListIndex = intItem - 1
    Else
    NewCharter.cmbTypeYacht.AddItem NewCharter.cmbTypeYacht.Text
    MsgBox "Successfully added new Yacht Type", vbInformation, "Successfully Added"
    End If
End Sub

顺便说一下,这是我的代码,只使用1个表格(添加并检查是否存在数据)

Dim TypeYacht As String 'Type of yacht added
Dim blnItem As Boolean
Dim intItem As Integer

' ----------------------------- LOOP THROUGHT the combo box all items -------------------------

blnItem = False
intItem = 0
TypeYacht = cmbTypeYacht.Text

Do Until blnItem = True Or intItem = cmbTypeYacht.ListCount
    If TypeYacht = cmbTypeYacht.List(intItem) Then
        blnItem = True
    End If
    intItem = intItem + 1
Loop
    If blnItem = True Then
     MsgBox TypeYacht & " " & "is already exist", vbInformation, "Yacht Type Match"
     cmbTypeYacht.ListIndex = intItem - 1
    Else
    cmbTypeYacht.AddItem cmbTypeYacht.Text
    MsgBox "Successfully added new Yacht Type", vbInformation, "Successfully Added"

    End If

1 个答案:

答案 0 :(得分:0)

NewCharter必须包含对NewCharter表单的有效打开实例的引用。

VB6允许您通过类名访问表单,而无需显式创建表单。这可能会导致混淆。您应该始终创建表单并明确传递引用。

在一种形式中,您必须拥有NewCharter类型的变量。然后你必须创建第二个表单并告诉你第一个表单。如,

Dim another_form As NewCharter
Set another_form = New NewCharacter

然后在现有代码中使用another_form代替NewCharter