2013-10-13 33 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

相關問題