问题
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