Type Mismatch

B

Brian

Can anyone tell what's wrong with this code, I have used this method in lots
of forms but for some reason, this just won't work, I get a Type Mismatch
error on the last line.

Private Sub cmdGo_Click()
Dim Db As Database
Dim RsResults As Recordset

Set Db = CurrentDb()
Set RsResults = Db.OpenRecordset("tblResults", dbOpenDynaset)

end sub

any ideas ???
Brian
 
A

Andy Cole

Brian

Your code uses the DAO syntax. You are using A2k or A2K2 where the default
is to use ADO. The solution is to set a reference to DAO (Microsoft DAO 3.x
Object Library). If you're *not* using ADO, you should remove the reference
to it (Microsoft ActiveX Data Objects 2.x Library).

If you need (or want) both, then you must dis-ambiguate variable
declarations such "x As Recordset". Your code declarations would be;

Dim Db As DAO.Database
Dim RsResults As DAO.Recordset

when this is done, you will be able to compile and run without errors

HTH

Andy
 
A

Andy Cole

Brian

Your code uses the DAO syntax. You are using A2k or A2K2 where the default
is to use ADO. The solution is to set a reference to DAO (Microsoft DAO 3.x
Object Library). If you're *not* using ADO, you should remove the reference
to it (Microsoft ActiveX Data Objects 2.x Library).

If you need (or want) both, then you must dis-ambiguate variable
declarations such "x As Recordset". Your code declarations would be;

Dim Db As DAO.Database
Dim RsResults As DAO.Recordset

when this is done, you will be able to compile and run without errors

HTH

Andy
 
M

Maria from Sweden

Hi Brian!

I can't tell you what's wrong but I had the same problem
before...I solved it by using this code instead (it just
wouldn't work with the way I/you used):

Dim MyDB As Database
Dim MyQ As QueryDef

Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set MyQ = MyDB.CreateQueryDef()
MyQ.Name = " a name "
MyQ.SQL = " your sql-question"
MyDB.QueryDefs.Append MyQ
MyQ.Execute
MyQ.Close
MyDB.QueryDefs.Delete " the name you wrote above"

It takes the database you're using at the moment, creates
and runs the sql-question, closes and deletes the question.

Hope this could help you!

Maybe you could help me - I wrote a post before (sender
Maria from Sweden)?

Have a nice 4th of July if you're american!

Cheers
// Maria
 
M

Maria from Sweden

Hi Brian!

I can't tell you what's wrong but I had the same problem
before...I solved it by using this code instead (it just
wouldn't work with the way I/you used):

Dim MyDB As Database
Dim MyQ As QueryDef

Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set MyQ = MyDB.CreateQueryDef()
MyQ.Name = " a name "
MyQ.SQL = " your sql-question"
MyDB.QueryDefs.Append MyQ
MyQ.Execute
MyQ.Close
MyDB.QueryDefs.Delete " the name you wrote above"

It takes the database you're using at the moment, creates
and runs the sql-question, closes and deletes the question.

Hope this could help you!

Maybe you could help me - I wrote a post before (sender
Maria from Sweden)?

Have a nice 4th of July if you're american!

Cheers
// Maria
 
B

Brian

Thanks Andy,
You were correct, simply being explicit and adding the DAO prefix, fixed it.
Thanks again,
Brian
 

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