SQL代码在Excel VBA中不起作用

时间:2022-11-22 21:28:43

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

我正在使用Excel 2010 VBA并从PGSQL数据库查询3个表

The following SQL code works perfect in MS ACCESS:

以下SQL代码在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:

但似乎没有转移到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(

我得到一个运行时错误:... FROM之后的预期表源,得到(

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.

用户表使用user.entity_id = recall.master_id链接到召回表,并显示用户的所有召回记录。然后我需要匹配用户的电话号码。并非所有人都有电话号码,但无论他们是否有电话号码我都需要召回。

What is wrong with the SQL code in VBA?

VBA中的SQL代码有什么问题?

2 个解决方案

#1


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

#2


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.

是的,对于不同的数据库,它可能是不同的SQL。通常,SQL非常相似,但不同数据库之间总是存在细微差别。

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.

如果你有时间,请看下面链接中的这个小工具。这对于将SQL转换为VBA非常有用。试一试,看看你是如何相处的。

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

#1


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

#2


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.

是的,对于不同的数据库,它可能是不同的SQL。通常,SQL非常相似,但不同数据库之间总是存在细微差别。

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.

如果你有时间,请看下面链接中的这个小工具。这对于将SQL转换为VBA非常有用。试一试,看看你是如何相处的。

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