Error msg when adding reocrds

G

Gman

I am attempting to add records to a table using the code below. I get a type
mismtch error using DAO and an Object variable not assigned error using ADO.
eventually I will be taking data from a form and adding it to an added
records log table. I tookthe code directly from the reference books, But Im
stuck on the basics somewhere . Any Ideas? Thank you

DAO code
Sub AutoEnterStudents()
Dim CurDB As Database
Dim MyRecordSet As Recordset

Set CurDB = CurrentDb
Set MyRecordSet = CurDB.OpenRecordset("tblStudents")' This is where I get
the error

With MyRecordSet
.AddNew
!LName = "Scott"
.Update
End With

MyRecordSet.Close
End Sub

ADO Code
Sub AutoEdit2()


Dim CurConn As ADODB.Connection
Dim MyTable As ADODB.Recordset

Set CurConn = CurrentProject.Connection
' This is where I get the error


MyTable.CursorType = adOpenKeyset
MyTable.LockType = adLockOptimistic
MyTable.Open "tblStudents", CurConn, , , adCmdTable

With MyTable
.AddNew
!LName = "Scott"
.Update
End With

MyTable.Close
Set MyTable = Nothing
CurConn.Close
Set CurConn = Nothing
 
J

John Vinson

I am attempting to add records to a table using the code below. I get a type
mismtch error using DAO and an Object variable not assigned error using ADO.

If you're using DAO code, select Tools... References and make sure
that Microsoft DAO 3.6 Object Library is checked, and (unless you're
using ADO code elsewhere) that Microsoft ActiveX Data Objects is
*unchecked*.

It's also wise to be explicit in your Dim:

Dim rs As DAO.Recordset

Both ADO and DAO have recordset objects but they are DIFFERENT
objects. What's happening is that you're getting an ADO recordset and
Access objects because you're trying to assign a DAO recordset value
to the ADO recordset object: type mismatch!

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top