2013-10-13 32 views
0

我正在寻求一些建议/提示/更正我的代码,为什么它不能正常工作。如何通过另一个窗体的组合框循环?

我有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 

回答

0

NewCharter必须包含一个有效的参考打开NewCharter形式的实例。

VB6,您可以通过它的类名来访问的形式,没有明确地创建它。这可能会导致混乱。你应该总是创建表单和明确地传递引用。

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

Dim another_form As NewCharter 
Set another_form = New NewCharacter 

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

+0

先生,根据你的回答,我把这些代码给了我form1在我的菜单栏(在这个)但不知何故不添加新的数据并检查数据是否已经存在。 – blackmaler

+0

@blackmaler我给你的代码只创建一个表单并存储引用。您必须确保创建的表单在您尝试访问它的位置包含一些数据。我不知道你如何用数据填写表格。尝试使用'another_form.Visible = True'扩展代码并触发提取数据的方法。然后点击你的'mnuAYT'。 – GSerg

+0

我从form2通过用户输入cmbTypeYacht.text 和form1得到了munebar(mnuAYT),在“cmbTypeYacht”有一些数据已经存在,所以这就是为什么我需要循环它通过清单,以确保它不重复什么都已经存在。 – blackmaler

相关问题