2010-10-05 118 views
1

如何執行從VBA代碼存儲在PostgreSQL中的名爲Test1的函數?如何從VBA代碼執行PostgreSQL函數?

例如,我們有一個函數定義如下:

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。我不知道如何去執行,怎麼辦?

回答

1

由於您已將dbs設置爲當前數據庫,因此失敗。當你執行這個語句時,訪問將查找表「Test1()」,並在找不到它時拋出一個hissy fit。

我不確定應該如何進行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 

等等

+0

你絕對沒錯。這是正確的一點。 – Dariusz 2010-10-05 13:40:33

2

您可以使用Access「傳遞」查詢爲您的PostGreSQL函數。

我在PostGreSQL服務器上創建了你的函數和表。然後在Access中創建一個新的傳遞查詢。我爲查詢的SQL屬性使用了PostGreSQL語句。

SELECT Test1(); 

(我並不需要一個FROM子句。)

在查詢屬性表,我分配在ODBC連接字符串,並選擇是爲「返回記錄」屬性。然後可以從Access用戶界面運行該查詢,或者從VBA代碼中使用該查詢來打開基於該查詢的DAO記錄集。

+0

也是非常有趣的解決方案。 – Dariusz 2010-10-06 14:03:33

0

路舊後nonwithstanding,我來到這裏,通過谷歌,終於成功地放在一起,對我工作的一個功能,它可以幫助別人同樣的問題:在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