1

我正在尝试编写代码以在选择每个组合框时更改 3 个组合框。例如:在组合框 1 中,他们选择 Urban,它用 2010 和 2011 填充组合框 2,然后用 houston、austin 等填充组合框 3。我正在尝试使用 If then 循环,但我得到了“无效的限定符”的错误,我不明白,因为它是有效的,它已在整个脚本中使用。任何帮助都会很棒!

    Private Sub UserForm_Initialize()

    cboStations.Value = "Annual"
    cboYear.Value = "2012"

    Dim WorkDB As DAO.Database
    Dim workRecSetA As DAO.RecordSet
    Dim workRecSetB As DAO.RecordSet
    Dim x As Integer



    Set WorkDB = DBEngine.OpenDatabase("K:\TASS\2 - GEO-DATA PROCESSING SUPPORT\MICHELLE'S WORK_ENTER NOT!!\Work Folder\Map Automation Project\Access Tables\Map_Automation.mdb")
    Set workRecSetA = WorkDB.OpenRecordset(Name:="select * from Districts order by District_Name", Type:=dbOpenDynaset)
    Do Until workRecSetA.EOF
        cboDistrict.AddItem workRecSetA("District_Name")
        workRecSetA.MoveNext
    Loop
    Set workRecSetB = WorkDB.OpenRecordset(Name:="select * from Stations order by Station_Name", Type:=dbOpenDynaset)
    Do Until workRecSetB.EOF
        cboStations.AddItem workRecSetB("Station_Name")
        workRecSetB.MoveNext
    Loop

    For x = 2010 To 2015
        cboYear.AddItem x
    Next


End Sub


Private Sub cmdCancel_Click()

    frmMapSetUp.Hide

End Sub


Private Sub cboStations_Change()

    Dim cboYear As String

    If cboStations.Text = "Urban" Then
      cboYear.AddItem "2010", "2011", "2012"  > Here is where I am receiving the error!!

    End If


End Sub

Private Sub cboYear_Change()

    Dim cboDistrict As String

    If cboYear.Text = "2010" Then
        cboDistrict.AddItem "Abilene", "Amarillo", "Austin", "San_Antonio", "Waco", "Wichita_Falls"
    Else
        cboYear.Text = "2011"
        cboDistrict.AddItem "Beaumont", "Houston"

    Else
        cboYear.Text = "2012"                                  cboDistrict.AddItem "Brownwood", "Bryan", "Childress", "Corpus_Christi", "El_Paso", Lubbock, "Odessa", "Yoakum"
    End If


End Sub
4

1 回答 1

1

你的线

cboYear.AddItem "2010", "2011", "2012"

无效。查看用于操作组合框的MSDN 文档。

.AddItem接受一个或两个参数 - 第一个是项目,第二个是指示在哪里插入项目的数字。我会冒险猜测它正在将 '2011' 转换为一个数字,试图将其插入到 2011 的位置(这当然不存在,因为你的组合框中没有 2000 多个项目!)并抛出一个不稳定的.

尝试拆分它:

cboYear.AddItem "2010"
cboYear.AddItem "2011"
cboYear.AddItem "2012"
于 2013-03-13T02:12:53.873 回答