我如何填充组合框从数据库中查询? [英] How can I populate combo box from a database query?

查看:552
本文介绍了我如何填充组合框从数据库中查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于我如何填充组合框从数据库中查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆