如何从VBA代码执行PostgreSQL函数?

时间:2010-10-05 12:14:41

标签: ms-access postgresql odbc

如何从VBA代码执行存储在PostgreSQL中的名为Test1的函数?

例如,我们有一个函数定义如下:

CREATE OR REPLACE FUNCTION "public"."Test1" (
)
RETURNS bit AS
$body$
BEGIN
    INSERT INTO test ("name") VALUES ('1');
RETURN 1;

END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;

现在我正试图以这种方式执行此功能:

Function TestCall()
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset

Dim strSQl As String

strSQl = "SELECT * FROM Test1();" 

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQl, dbOpenDynaset, dbSeeChanges)
'this doesnt work as well: syntax error'
dbs.Execute strSQl 


If Not (rst.BOF And rst.EOF) Then
    do some work here
End If

End Function

但我得到了Syntax Error near FROM。我不知道如何执行此操作,如何才能完成?

3 个答案:

答案 0 :(得分:2)

您可以对PostGreSQL函数使用Access“传递”查询。

我在PostGreSQL服务器上创建了你的函数和表。然后在Access中创建了一个新的传递查询。我在查询的SQL属性中使用PostGreSQL语句。

SELECT Test1();

(我不需要FROM子句。)

在查询属性表上,我分配了ODBC连接字符串,并为“返回记录”属性选择了“是”。然后,可以从Access用户界面运行查询,或者使用VBA代码根据该查询打开DAO记录集。

答案 1 :(得分:1)

失败是因为您已将dbs设置为当前数据库。当你执行这个语句时,访问将查找表“Test1()”并在它找不到时抛出一个混合物。

我不确定如何使用postgre执行此操作,但这就是我如何使用SQL服务器执行类似操作,因此我假设它将是相同的但具有不同的连接字符串

Dim dbCon as new ADODB.Connection
Dim rst as new ADODB.Recordset

Dbcon.connectionstring=”Your connection string goes here!”
Dbcon.open

Rst.open strsql

等等

答案 2 :(得分:0)

老版本的帖子尽管如此,我来到这里是通过谷歌最终设法组建了一个适合我的功能,它可以帮助其他人有同样的问题:在MS Access的公共模块中,保存以下

Option Compare Database
Public Function ProcessSTP_Fn(PostgresFnString As String)
    Dim qdf As DAO.QueryDef, rst As DAO.Recordset
''Server Settings
   On Error GoTo ErrHandler
    myServerName = "127.0.0.1"
    myDatabaseName = "my_db"
    myUserName = "my_user"
    myPassword = "my_pass"

''Connection String Components
    sqlConnString = ""
    sqlConnString = sqlConnString & "ODBC;Driver={PostgreSQL ANSI};"
    sqlConnString = sqlConnString & "Server=" & myServerName & ";"
    sqlConnString = sqlConnString & "Port=5432;"
    sqlConnString = sqlConnString & "Database=" & myDatabaseName & ";"
    sqlConnString = sqlConnString & "Uid=" & myUserName & ";"
    sqlConnString = sqlConnString & "Pwd=" & myPassword & ";"

''Create QueryDef and run the Postgres Function
    Set qdf = CurrentDb.CreateQueryDef("")
        qdf.Connect = sqlConnString
        qdf.SQL = PostgresFnString ''From the parameters
        qdf.ReturnsRecords = True
        Set rst = qdf.OpenRecordset
            ''Return any results expected
        rst.Close
        Set rst = Nothing
    Set qdf = Nothing
    Debug.Print "Query: " & PostgresFnString & " Passed Sucessfully At " & Now()
   Exit Function
ExitHandler:
   On Error GoTo 0
   ErrorState = True
   Exit Function
ErrHandler:
    Dim MyError As Error
    MsgBox Errors.Count
    For Each MyError In DBEngine.Errors
      With MyError
        MsgBox .Number & " " & .Description
      End With
    Next MyError
    GoTo ExitHandler
End Function

Public Sub TestPostgresFn()
    PostgresFnString = "SELECT * FROM Test1();"
    Call ProcessSTP_Fn(PostgresFnString)
End Sub