Question

I'm new to VBA and I'm having problems using createQueryDef method with a connection object.

I'm just trying to create a query from a table that is in a SQL server database, simply to make the data appear in Access. I opened the connection and then tried to use createQueryDef method, but it gives me a runtime error:

"Arguments are of the wrong type, are out of acceptable rangel, or are in confilct with one another"

Here is the code:

Sub connect()

Dim conn As ADODB.Connection
Set conn = CreateObject("ADODB.Connection")

conn.ConnectionString = "Provider=SQLOLEDB;" & _
"Data Source=MyDataSource;" & _
"User Id=MyUserID;" & _
"Password=MyPassword;" & _
"initial catalog=MyDatabase;"

conn.Properties("Prompt") = adPromptAlways

conn.Open

Dim qd As QueryDef

Set qd = conn.CreateQueryDef("test", "SELECT * FROM Mytable")

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "test", "C\MyFolder\test.xls"
rs.Close
conn.Close
Set conn = Nothing

End Sub

I'm getting the error on:

Set qd = conn.CreateQueryDef("test", "SELECT * FROM Mytable")

If you have another way to display the data from in an Access Table that works, I also appreciate your help, because that is just what I want

No correct solution

OTHER TIPS

CreateQueryDef is a method of a DAO.Database object. It is not an available method of an ADO connection object.

If your goal is to create an Access query object and if you're doing this with VBA from within an Access session, you can leave ADO out of the picture and use a DAO.Database object to create the query with the CreateQueryDef method.

Dim db As DAO.Database
Dim qd As QueryDef

Set db = CurrentDb
Set qd = db.CreateQueryDef("test", "SELECT * FROM Mytable")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top