从另一个ComboBox VBA填充和排序ComboBox

时间:2017-05-04 16:09:14

标签: vba sorting combobox access-vba

大家好我选择另一个组合框时,我无法对ComboBox进行排序。 我想我有正确的SQL语法,但我似乎无法让vba运行它;目前,无论公司如何,vba都会返回记录集中的所有状态。

Private Sub CboCountry_Click()
Set db = CurrentDb
Dim SQLStr As String

Set RsState = db.OpenRecordset("T2States", dbOpenSnapshot, dbSeeChanges)

 'populates combobox with recordset, that is defined by the country input from the form

RsState.MoveFirst
Do While Not RsState.EOF
    Me.CboState.RowSource = Me.CboState.RowSource & RsState("StateID") & ";" & RsState("State") & ";"
    RsState.MoveNext
Loop

我认为这是正确的SQL字符串,但我无法让它工作。

'SQLStr = "SELECT T2States.StateID, T2States.States, T2States.CountryID" & _
            " FROM T2States GROUP BY T2States.StatesID" & _
            " WHERE T2States.CountryID = """ & Me.CboCountry.Value & """"

非常感谢任何帮助。

编辑#1 请参阅下面的完整代码,当我将SQLStr替换为Openrecordset时弹出的错误是运行时错误“3078”,微软访问数据库引擎无法找到输入表或查询“SQLStr”。确保它存在并且其名称拼写正确。 应该发生的事情是当从CboCountry组合框中选择一个国家时,它将通过按CountryID对记录集进行排序来加载CboState组合框

请参阅下面的两个代码部分

    Private Sub Form_Load()

Set db = CurrentDb
Set RsCompany = db.OpenRecordset("T1Company", dbOpenDynaset, dbSeeChanges)
Set RsCountry = db.OpenRecordset("T2Countries", dbOpenSnapshot, dbSeeChanges)
Set RsAddress = db.OpenRecordset("T1Addresses", dbOpenDynaset, dbSeeChanges)
Set RsAddressType = db.OpenRecordset("T2AddressType", dbOpenSnapshot, dbSeeChanges)
Set RsCompanyAddress = db.OpenRecordset("T3Company_Address", dbOpenDynaset, dbSeeChanges)

Me.CboCountry = Null
Me.TxtAddress1 = Null
Me.TxtAddress2 = Null
Me.TxtAddress3 = Null
Me.TxtCity = Null
Me.CboAddressType = Null
Me.CboCountry = Null
Me.CboState = Null
Me.TxtPostalCode = Null
Me.TxtCompanyID = Null
Me.TxtLegalName = Null
Me.TxtNickname = Null
Me.TxtAddressID = Null

    RsCountry.MoveFirst
    Do While Not RsCountry.EOF
        Me.CboCountry.RowSource = Me.CboCountry.RowSource & RsCountry("CountryID") & ";" & RsCountry("Country") & ";"
        RsCountry.MoveNext
    Loop

    RsAddressType.MoveFirst
    Do While Not RsAddressType.EOF
        Me.CboAddressType.RowSource = Me.CboAddressType.RowSource & RsAddressType("AddressTypeID") & ";" & RsAddressType("AddressType") & ";"
        RsAddressType.MoveNext
    Loop
Me.TxtLegalName.SetFocus

End Sub



Private Sub CboCountry_Click()
Set db = CurrentDb
Dim SQLStr As String

'SQLStr = "SELECT T2States.StateID, T2States.State, T2States.CountryID" & _
            " FROM T2States" & _
            " WHERE T2States.CountryID = """ & Me.CboCountry.Value & """"

Set RsState = db.OpenRecordset("T2States", dbOpenDynaset, dbSeeChanges)

    'populates combobox with recordset, that is defined by the country input from the form

    RsState.MoveFirst
    Do While Not RsState.EOF
        Me.CboState.RowSource = Me.CboState.RowSource & RsState("StateID") & ";" & RsState("State") & ";"
        RsState.MoveNext
    Loop

End Sub

2 个答案:

答案 0 :(得分:0)


让我们看看
1-确定你附加

Having T2States.States, T2States.CountryID


2-存在错误,额外的错误'在列名称中:

GROUP BY T2States.StatesID


3-填写所有代码,我会检查你错过了什么。

祝你好运

答案 1 :(得分:0)

这个结果是在DATA选项卡下的属性表中的快速修复,行源类型必须从'值'更改回'表/查询'。

VBA可以解释这一点,但只是改变了Row Source。

混淆的原因,如果它有帮助的话,快速的背景是我的所有组合框都是未绑定的,我用VBA Recordsets绑定它们,所以rowsource必须是一个值列表 - 本质上是VBA是每次加载时编写列表。

当我开始使用SQL生成记录集时,即使它在VBA中,我也必须将属性更改回Table / Query。

感谢。