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