首页 > 解决方案 > 如何用 ADO Recordset 中的值填充 Recordsource?

问题描述

我正在尝试拥有一个记录集,该记录集包含从另一个记录集复制的数据以及两个附加列。我之前在另一个问题中提到过这一点。现在,以下代码中的某个地方出现了我无法识别的问题。

如果您阅读了我链接的帖子,您已经看到了此代码。我只更改了将字段附加到 Recordset 的部分,因为我认为某处存在错误。它现在看起来和我的代码一模一样,只是名称已更改。

Dim cpRS As New ADODB.Recordset, RS As DAO.Recordset, cb As checkBox, addr As String
'Creating copy of previously displayed result table
Set cpRS = New ADODB.Recordset
With cpRS
'
    .Fields.Append "val0", adInteger
    .Fields.Append "val1", adInteger
    .Fields.Append "val2", adVarChar, 80
    .Fields.Append "val3", adVarChar, 80
    .Fields.Append "val4", adVarChar, 30
    .Fields.Append "val5", adVarChar, 30
    .Fields.Append "val6", adVarChar, 10
    .Fields.Append "val7", adVarChar, 10
    .Fields.Append "val8", adVarChar, 50, adFldIsNullable
    .Fields.Append "val9", adDBTimeStamp
    .Fields.Append "val10", adVarChar, 50
    .Fields.Append "val11", adSmallInt
    .Fields.Append "val12", adVarChar, 100
    .Fields.Append "val13", adVarChar, 255, adFldIsNullable
    .Fields.Append "val14", adVarChar, 255, adFldIsNullable
    .Fields.Append "val15", adInteger
    .Fields.Append "val16", adInteger
    .Fields.Append "val17", adSmallInt
    .Fields.Append "val18", adSmallInt

    'new Fields for temporary purposes
    .Fields.Append "val19", adBoolean
    .Fields.Append "val20", adVarChar, 50

    .CursorLocation = adUseClient
    .Open , , adOpenDynamic, adLockOptimistic, 8
End With

'get result set of previous window by applying filter to the same query used before
Dim argv() As String
Dim argRest As String
Dim qdef As DAO.QueryDef
Dim restrictedQuery As String

'When opening this form I hand over OpenArgs which i restore here
'took the code out but "argv" and "argRest" will be used later    

'this is the query that is used in the previous form. i need an extra where clause though so i had to rewrite it.
restrictedQuery = "some very long SQL statement I feel I don't need to put here because it doesn't contribute to the problem." & _
    "If I'm incorrect, please let me know and I will rewrite it to protect the data in it"

Set qdef = CurrentDb.CreateQueryDef("")
qdef.SQL = restrictedQuery
Set RS = qdef.OpenRecordset
Set RS = CurrentDb.OpenRecordset(restrictedQuery, dbOpenSnapshot)
RS.MoveLast
RS.MoveFirst
If RS.RecordCount = 0 Then
    MsgBox "some error text", vbOKOnly, "error title"
    DoCmd.Close acForm, Me.Name
    Exit Sub
End If

'populate new recordset with data from table in previous form
Do Until RS.EOF
'putting the data from the "old" recordset into the new one, shortened again, you get the idea
    cpRS.AddNew
    cpRS.Fields("val1") = RS("some_value")
    cpRS.Fields("val2") = RS("some_value2")
    '...
    'fill the two columns that don't come from the query with default values
    cpRS.Fields("val19") = False
    cpRS.Fields("val20") = ""
    cpRS.Update
    RS.MoveNext
Loop

Set Me.Recordset = cpRS

RS.Close
Set RS = Nothing
'cpRS.Close  - I removed this
Set cpRS = Nothing

现在有一些奇怪的行为(至少对我来说)。当我调试时,我可以看到 ADO 记录集被来自 DAO 记录集的数据填充。没有错误,所有值都是正确的。但是,当表单打开时,仅填充了一些单元格。其余单元格包含“#Name?” 这让我认为a)我为字段设置的数据类型错误或b)表单不知道将哪些值放入字段中,因为有多个来源试图填充它(我想我已经阅读过这个 #Name? 错误与前一段时间的那种错误相关联,但我不确定,我不知道双重分配发生在哪里)。

结构大致如下: 表单包含子表单。子表单包含几个文本框,这些文本框具有我添加到 ADO 记录集作为控制源的值的名称。(这是我的前任处理填写表单的方式,我不知道这是否是执行此任务的常用方法。)子表单的Recordsource 未设置,因为我在 Form_Load 事件中将 ADO Recordset 设置为 Recordsource(这是代码的来源)。

我希望这是足够的信息来定位问题,否则请告诉我,我会尝试提供更多信息。

编辑:我想我应该补充一点,只有一列使它成为 adVarChar 类型的子表单(代码中的 val4)

标签: vbams-access

解决方案


.ControlSource是控件属性(如文本框)而不是记录集字段属性。

它将记录集字段绑定到表单/报告控件。

如果您将 adodb 记录集字段命名为与追加时的 dao 字段相同。Controlsource 中无需更改:

.Fields.Append "NameOfDaoField0", adInteger

或使用

Me.Controls("MyTextbox").ControlSource = "valX"

其中 valX 是字段的附加新名称(在您的情况下为 val0、val1、...)。

在代码中的位置应该无关紧要。


推荐阅读