简体   繁体   English

未显示ADODB Recordset的字段

[英]Fields not showing for ADODB Recordset

I have an ADODB recordset which I passed from a module to a form. 我有一个ADODB记录集,我从模块传递给表单。 I need to show the existing fields of the recordset, but it does not seem to work. 我需要显示记录集的现有字段,但它似乎不起作用。 The recordset is passed successfully, as a quick Debug.Print shows. 记录集成功传递,如快速Debug.Print所示。 Here is my code: 这是我的代码:

Module: 模块:

Public Sub dofilter() 
   ...
   With ADORec
     Set .ActiveConnection = ADOConn
     .Source = SQL
     .LockType = adLockReadOnly
     .CursorType = adOpenKeyset
     .CursorLocation = adUseClient
     .Open
   End With

   Call Form_FormA.setrst(ADORec)
End Sub

And then in FormA: 然后在FormA中:

Public Sub setrst(ByVal rst As ADODB.Recordset)
    Set Me.Recordset = rst
    Me.txtField.ControlSource = rst.Fields("ID").Name
End Sub

The ID field in the Form is still empty. 表单中的ID字段仍为空。 I hope you guys can help. 我希望你们能帮忙。

First of all I'd recommend to have a look at the documentation of ControlSource . 首先,我建议您查看ControlSource的文档。 This property is not the right one to fill the textbox from a recordset. 此属性不是从记录集填充文本框的正确属性。

And then I'd replace Me.txtField.ControlSource = rst.Fields("ID").Name with Me.txtField.value = rst.Fields("ID").Name . 然后我将替换Me.txtField.ControlSource = rst.Fields("ID").NameMe.txtField.value = rst.Fields("ID").Name This is probaly what you want. 这可能是你想要的。

Another recommendation is not to use the global default instance of a userform 另一个建议是不要使用userform全局默认实例

Update : Based on the comment you need a listbox if you want to display all records and the following line of code ListBox1.Column = rst.GetRows . 更新 :根据评论,如果要显示所有记录,则需要列表框,以及以下代码行ListBox1.Column = rst.GetRows You should define the listbox with enough columns. 您应该使用足够的列定义列表框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM