我如何填充组合框从数据库中查询?组合、数据库中

2023-09-08 11:30:24 作者:以他为氧

我要填充在访问查询的结果组合框。我只是没有看到如何做到这一点。据我了解,我们必须首先创建一个记录集,读取查询结果到记录集,然后写入记录设置为组合框的行源属性。它是否正确?有一个简单的例子地方,我可以遵循?我还没有找到一个在任何其他线程。

I want to populate a combo box with the results of a query in Access. I'm just not seeing how to do it. As far as I understand, one must first create a record set, read the query results into the record set, then write the record set to the combo box's row source property. Is this correct? is there a simple example somewhere that I can follow? I haven't found one in any of the other threads.

下面是我的尝试至今:

    Dim RS As Recordset
    Dim myDB As Database

    Set RS = myDB.OpenRecordset("SourcesNotDisposed", dbOpenDynaset)
    Do While Not RS.EOF
    With Me.cmbSN
        RowSource.AddItem
    End With
    Loop

通过这个code,我得到的行来源行的所需的对象错误。 cmbSN有数据属性:         行源类型=表/查询         绑定列= 0         限于列表=是         允许值列表编辑=是         继承值列表=是         只显示一行源=否

With this code, I'm getting an "Object required" error at the RowSource line. cmbSN has data properties: Row source Type = Table/Query Bound Column = 0 Limit to List = Yes Allow value list edits = Yes Inherit value list = Yes Show only row source = No

查询只有一个可见的列被称为序列号

The query only has one visible column called "Serial Number"

在此先感谢

推荐答案

下面code插入表中的字段成组合框。添加该code在的OnEnter 事件组合

Below code insert table fields into combo box. Add this code under onEnter event of combo

 Private Sub  CM_Enter()

  'CM is  combobox name

    Dim strItem1 As String
    Dim strItem2 As String

   On Error Resume Next

   Dim i As Integer 'Index for loop
   With Me.CM
    .RowSourceType = "Value List" 'Set rowsource type as Value list
    .RowSource = "" 'Clean combo contents
  End With

 'Loop through field names of table and add them to your combo:
    For i = 1 To CurrentDb.TableDefs("table1").Fields.Count - 1
    Me.CM.AddItem (CurrentDb.TableDefs("table1").Fields(i - 1).Name)

  Next i

 '/***Delete unwanted items from the combo
   strItem1 = "col1"
   strItem2 = "col2"
  'CM.RemoveItem strItem1
  'CM.RemoveItem strItem2

  End Sub