2

我想使用 ADO 记录集基于现有表创建新表。然后我想将新表设置为我的表单的记录源。我知道我可以创建一个查询并将查询设置为我的表单的记录源,但是如果我不想使用这种方法?我希望表单的记录源仅在表单加载时才存在。这是我所做的,但仍然无法将表单的记录源设置为我的记录集。

Private Sub Form_Load()

Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim rst As New ADODB.Recordset
rst.ActiveConnection = cnn
Dim mySql As String
'create tblfrmQryOnHold based on tblOnHold
mySql = "SELECT tblDisposition.ID, tblDisposition.DateRecorded, tblDisposition.OrderNo, tblDisposition.ArticleNo, "
mySql = mySql & "tblDisposition.Description, tblDisposition.Process, tblDisposition.Defects, tblDisposition.RefNo, "
mySql = mySql & "tblDisposition.PostedBy, tblDisposition.Status, tblDisposition.Attachment, tblDisposition.Engineer, "
mySql = mySql & "tblDisposition.Remarks, tblDisposition.ReviewClose, tblDisposition.ScrapNo, tblDisposition.HoldbackNo, "
mySql = mySql & "tblDisposition.ProductionRemarks, tblDisposition.HoldbackQuantity, tblDisposition.HoldbackNum INTO "
mySql = mySql & "frmQryOnHold FROM tblDisposition;"

rst.Open mySql
'set form frmOnHold record source to form frmQryOnHold
Forms![frmOnHold].RecordSource = frmQryOnHold
End Sub

我收到此错误“对象关闭时不允许操作”,此错误指的是哪个对象?

4

2 回答 2

1

您似乎正在处理两种不同的想法。

Private Sub Form_Load()

Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim rst As New ADODB.Recordset
rst.ActiveConnection = cnn
Dim mySql As String
'create tblfrmQryOnHold based on tblOnHold
''Using aslias t for tblDisposition for clarity
mySql = "SELECT t.ID, t.DateRecorded, t.OrderNo, t.ArticleNo, "
mySql = mySql & "t.Description, t.Process, t.Defects, t.RefNo, "
mySql = mySql & "t.PostedBy, t.Status, t.Attachment, t.Engineer, "
mySql = mySql & "t.Remarks, t.ReviewClose, t.ScrapNo, t.HoldbackNo, "
mySql = mySql & "t.ProductionRemarks, t.HoldbackQuantity, t.HoldbackNum INTO "
mySql = mySql & "frmQryOnHold FROM tblDisposition As t;"

''Action query, so execute it aginst a connection
''This will fail if the table already exists, so it would be 
''much better to use DELETE FROM ... and INSERT INTO ...
''which would also cut down or bloat. However, the very best
''solution would be to just use a query on tblDisposition 
cnn.Execute mySql

''You now have created the table frmQryOnHold and can use it as @SoupyC 
''shows, or, if you wish to use recordset, as you seem to imply, then you want:

   With rst
     Set .ActiveConnection = cnn
     ''Something like this
     .Source = "select * from frmQryOnHold"
     .LockType = adLockOptimistic
     .CursorType = adOpenKeyset
     .CursorLocation = adUseClient
     .Open
  End With

'set form frmOnHold record source to form frmQryOnHold
Set Forms![frmOnHold].Recordset = rst


'set form frmOnHold record source to form frmQryOnHold
Forms![frmOnHold].RecordSource = frmQryOnHold
End Sub

If you are just assigning a table to a recordsource, pure DAO is the way to go.

于 2013-03-20T12:28:46.937 回答
0

您需要像这样在 RecordSource 周围加上引号:

Forms![frmOnHold].RecordSource = "frmQryOnHold"
于 2013-03-19T09:53:19.053 回答