我正在尝试编写代码,以便在选择每个代码框时更改3个组合框。例如:在组合框1中,他们选择Urban,在2010年和2011年填充组合框2,然后将组合框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
答案 0 :(得分:1)
你的行
cboYear.AddItem "2010", "2011", "2012"
无效。看一下MSDN docs来操作组合框。
.AddItem
需要一个或两个参数 - 第一个是项目,第二个是指示插入项目的位置的数字。我猜想它会将'2011'转换成一个数字,试图将它插入2011的位置(当然不存在,因为你的组合框中没有2000多个项目!)并且摇摆不定
尝试拆分:
cboYear.AddItem "2010"
cboYear.AddItem "2011"
cboYear.AddItem "2012"