ODBC using QueryTable

P

Phillip Hall

Hi all,

I have and ODBC query which returns data from a PostgreSQL database which
works correctly when I use the Get ExternaL Data & MS Query approach.

I want to use the QueryTable object in VBA to so the same. This is so I can
parameterise my select statement

The following macro


Dim thisSheet As Worksheet
Set thisSheet = ThisWorkbook.Worksheets(1)

Dim sql
sql = "SELECT * FROM books"

Dim connString
connString = "ODBC;DSN=Booktown"

Dim thisQT As QueryTable
Set thisQT = thisSheet.QueryTables.Add(Connection:=connString,
Destination:=Range("a1"))

thisQT.BackgroundQuery = False
thisQT.sql = sql
thisQT.Refresh

Causes this error

Run-time error'1004'
Method 'Refresh' of object '_QueryTable' failed.

Does anyone know what might be wrong with my macro. I think it might be the
connString that needs work.

-Phillip
 

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