2013-03-19 73 views
0

我想使用ADO記錄集來創建基於現有表的新表。然後我想設置新表作爲我的表單的記錄source.i知道我可以創建一個查詢並將查詢設置爲我的表單的記錄源,但是它如果我不想使用這種方法可能嗎?我希望表單的記錄源僅在表單加載時才存在。這是我所做的,但仍然無法將表單的記錄源設置爲我的記錄集。如何使用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 

我得到這個錯誤「當對象關閉時不允許操作」,這個錯誤引用哪個對象?

+1

爲什麼選擇ADO? DAO對MS Access更好。 – Fionnuala 2013-03-20 11:50:53

回答

0

你似乎有兩種不同的創作理念。

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 

如果您只是將一張表分配給一個記錄源,那麼純粹的DAO就是要走的路。

0

你需要把引號將記錄來源是這樣的:

Forms![frmOnHold].RecordSource = "frmQryOnHold" 
相關問題