在内存方面,独立的,断开的ADO记录集记录集、内存、独立、方面

2023-09-08 10:59:20 作者:星期⑧娶妳

我在我的数据表子窗体运行此code时,我的形式加载,我没有得到任何错误消息或code休息。我debug.print显示,记录RS充满了2131的记录像它应该的,但我的表格显示了#NAME单行?在各个领域。在我的控制控制源属性肯定做符合我上面列出的字段名称。 RS是一个表单级变量,我不收,或将其设置为无,直到窗体关闭。

I'm running this code on my datasheet subform when my form loads and I'm not getting any error messages or code breaks. My debug.print shows that the Recordset rs is filled with 2131 records like it should be, but my form shows a single row with #Name? in every field. The control source properties on my controls most certainly do match the field names I have listed above. RS is a form level variable and I'm not closing it or setting it to nothing until the form closes.

知不知道我做错了?

Set rs = New ADODB.Recordset
rs.Fields.Append "TimesUsed", adInteger
rs.Fields.Append "strWorkType", adVarWChar, 150
rs.Fields.Append "DateLastUsed", adDate
rs.Fields.Append "SelectedYN", adBoolean
Set rs.ActiveConnection = Nothing
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
rs.Open

Dim sSQL As String
sSQL = "MyComplicated SQL Statement Ommitted from this SO Question"

Dim r As DAO.Recordset
Set r = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)
If Not (r.EOF And r.BOF) Then
    r.MoveFirst
    Dim fld
    Do Until r.EOF = True
        rs.AddNew
        For Each fld In r.Fields
            rs(fld.Name) = r(fld.Name).value
        Next
        rs.Update
        r.MoveNext
    Loop
End If
r.Close
Set r = Nothing
Debug.Print rs.RecordCount '2131 records
Set Me.Recordset = rs

好了,我刚刚看了这对在MSDN网站:

的记录必须包含唯一索引一个或多个字段,诸如表的主键。的

(注:此信息似乎是错误在这种情况下)

(Note: This information seems to be erroneous in this context.)

推荐答案

我发现我可以使这项工作的唯一方法是使用的LockType ADLOCKPESSIMISTIC或adLockOptimisic。 ADLOCKREADONLY不显而易见的原因工作,出于某种原因,ADLOCKBATCHOPTIMISTIC不允许记录,即使记录似乎是全功能的在我的形式显示。

I found out that the only way I can make this work is to use LockType adLockPessimistic or adLockOptimisic. adLockReadOnly doesn't work for obvious reasons and for some reason adLockBatchOptimistic does not allow records to display in my form even though the recordset appears to be fully functional.

我也发现了,你不必有这种类型断开连接的记录集定义主键绑定到一个表单。我敢肯定,你将无法通过表单进行任何修改或更新的记录,但在我的测试中,我发现,我不能让任何编辑这种类型的表单/记录反正因为我得到错误3270 (是与缺少的属性)。这真的是对这个问题的范围之外。

I also found out that you do not have to have a primary key defined for this type of disconnected Recordset to be bound to a form. I'm sure you won't be able to make any edits or updates to the recordset via the form but in my testing I found that I couldn't make any edits to this type of form/recordset anyway because I was getting Error 3270 (something to do with a missing property). That's really outside the scope of this question.

下面是code创建一个工作内存的记录所需的最低金额:

Here's the minimum amount of code needed to create a working in-memory recordset:

Dim rs As ADODB.Recordset 'Form Level variable

Private Sub Form_Load()
    Set rs = New ADODB.Recordset
    rs.Fields.Append "ID", adInteger
    'Set rs.ActiveConnection = Nothing 'Not Required
    'rs.CursorType = adOpenKeyset 'Not Required
    'rs.CursorLocation = adUseClient 'Not Required
    rs.LockType = adLockPessimistic 'May also use adLockOptimistic
    rs.Open

    Dim i as Integer

    For i = 1 To 10
        rs.AddNew
        rs("ID").Value = i
        rs.Update
    Next i

    Set Me.Recordset = rs
End Sub

这首,我似乎觉得一种形式(在我的情况数据表视图)绑定到该类型的连接记录的将是我的特殊需要一个很好的,简单的解决方案。然而,我遇到了几个问题。默认的形式排序似乎没有工作的时候,你有你的窗体绑定到ADO记录集。此外,由于某种原因,我从来没有得到这个记录可编辑/更新这是对我的需求的要求(我基本上是用它作为多核对表)。如果你从一个表(即使它是一个空表)获取记录集,然后断开连接,你可以解决这个问题。显然,餐桌用品某种结构或者说我没有在我的code设置上面的属性,通过我得到了3270的错误信息来看,当我尝试添加/编辑记录。我还没有想出什么样的属性或如何设置。

It first appeared to me that binding a form (datasheet view in my case) to this type of disconnected recordset would be a good, simple solution for my particular needs. However, I ran into several problems. The default form sorting does not appear to work when you have your form bound to an ADO recordset. Also, for some reason I never could get this recordset to be editable/updateable which was a requirement for my needs (I was basically using it as a multi-check list). If you obtain the recordset from a table (even if it's an empty table) and then disconnect you can work around this problem. Apparently the table supplies some kind of structure or properties that I've failed to set in my code above, judging by the 3270 error message I get when I try to add/edit a record. And I haven't figured out what those properties are or how to set them.

在最后,我想我会诉诸使用Access临时表,而不是因为它是那么复杂,没有我刚才上面列出的问题。

In conclusion, I think I'll resort to using an Access "temp" table instead since it will be less complicated and not have the problems I've just listed above.

 
精彩推荐
图片推荐