Bind field to temporary recordset field programmatically

与世无争的帅哥 提交于 2020-07-23 04:10:44

问题


I'm trying to display a recordset that is created programmatically. But I'm having problems binding the controlsource to the recordset fields.

The following code outputs "#Error" in the FirstField textbox.

Option Compare Database
Option Explicit

Private Sub Form_Load()
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    With rs.Fields
        .Append "A", adInteger
        .Append "B", adInteger
        .Append "C", adInteger
    End With

    rs.Open

    With rs
      .AddNew
        ![a] = 1
        ![b] = 1
        ![c] = 1
      .Update
    End With

    rs.Clone

    Set Me.Recordset = rs
    Me.FirstField.ControlSource = rs.Fields(0).Name 'outputs #Error in FirstField
End Sub

Help is much appreciated!


What I'm trying to do

I have a database which represent used parking spaces in a parking lot. A,B,C are position coordinates each of these have a validation rule set so they cannot exceed their limits. Now I want to display all available parking spaces. So I figured I'd create a "fake" database with all parking spaces and then do a query-thing where I only display entires from the fake database where the position is not in the "used parking space" database.

Creating a database with only the positions seemed silly to me and populating the "used parking space" database with all parking spaces and then adding a used boolean field also seemed like a bad idea.

I welcome input on how I could solve this in a better way.

Thank you for you time.


回答1:


From your comment, I see you intend to redesign, which seems like a good idea to me. Despite that, if you want to experiment further with an ADO disconnected recordset for a form, include .LockType before .Open.

rs.LockType = adLockPessimistic
rs.Open

With that change, and discarding the rs.Clone line, your Form_Load code works on my test form.

See this article from Database Journal for more details on this subject: Create In-Memory ADO Recordsets.



来源:https://stackoverflow.com/questions/9700767/bind-field-to-temporary-recordset-field-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!