3

I am using Excel 2010 VBA and querying 3 tables from a PGSQL database

The following SQL code works perfect in MS ACCESS:

SELECT user.surname,user.forename,recall.recalldate,telephone.number
FROM (user INNER JOIN recall ON user.entity_id = recall.master_id) 
LEFT JOIN Telephone ON recall.master_id=Telephone.master_id

but doesn't seem to transfer to EXCEL VBA:

Dim RECALL As String
RECALL = "SELECT user.surname,user.forename,recall.recalldate,telephone.number " _
        & "FROM (user INNER JOIN recall ON user.entity_id = recall.master_id) " _
        & "LEFT JOIN Telephone ON recall.master_id=Telephone.master_id " _

Set rs = conn.Execute(RECALL)
With ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=Range("A1"))
  .Refresh
End With

I get a runtime error: ...expected table source after FROM, got(

The user table links to the recall table using user.entity_id = recall.master_id and shows all recall records for users. Then I need the telephone numbers for the matched users. Not all will have a tel number, but I need all recalls regardless of whether they have a tel number.

What is wrong with the SQL code in VBA?

1
  • You are comparing two different SQL dialects: MS Access ACE/Jet SQL and PostgreSQL. One difference is use of parentheses in joins. If PGSQL tables are linked in Access, you will run the ACE/Jet dialect. If using pass-through queries like in Excel, you use the direct connected PGSQL dialect. Had you run a pass-thru query in Access you would receive same error. Also user is a reserved word in both dialects. Either escape or rename. Commented Mar 18, 2016 at 13:23

2 Answers 2

1

Seems you have a () after form try without

 SELECT user.surname,user.forename,recall.recalldate,telephone.number
 FROM  user INNER JOIN recall ON user.entity_id = recall.master_id
 LEFT JOIN Telephone ON recall.master_id=Telephone.master_id
Sign up to request clarification or add additional context in comments.

3 Comments

This now shows runtime error: Preprocessor;relation "Telephone" does not exist; SQLSTATE 42P01;SOURSE_FILE src\backend\parser\parse_relations.c; COURSE_line 984; SOURCE_FUNCTION parserOpenTable
Check for Telephone or telephone you have a lower case in select and upper case in JOIN .. otherwise you don't have a table with this name ..
Yep that's it. Thank you
0

Yeah, it's probably different SQL for different databases. Typically, the SQL is very similar, but there are always slight differences between different databases.

Take a look at this little utility in the link below when you have time. That is very useful for converting SQL to VBA. Give it a try, and see how you get along.

http://allenbrowne.com/ser-71.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.