mat duwe
mat duwe

Reputation: 153

how to use ADO recordset to create new table based on existing table and set the recordset(new table) as form's record source?

i want to use ADO recordset to create new table based on existing table.then i want to set the new table as my form's record source.i know i can create a query and set the query as my form's record source but is it possible if i dont want to use this method? i want the form's record source to exist only when the form load. here's what i've done but still cannot set the form's record source to my recordset.

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

i get this error "operation is not allowed when the object is close", which object this error refer to?

Upvotes: 2

Views: 5441

Answers (2)

Fionnuala
Fionnuala

Reputation: 91356

You seem to be working with two different ideas.

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.

Upvotes: 1

RichardC
RichardC

Reputation: 794

You need to put quotes around the RecordSource like this:

Forms![frmOnHold].RecordSource = "frmQryOnHold"

Upvotes: 0

Related Questions