簡體   English   中英

嘗試將 VBA 連接到 PostgreSQL(不是 excel)

[英]Attempting to connect VBA to PostgreSQL (not excel)

我是 PostgreSQL 的新手,我正在嘗試通過 Autodesk Inventor 的 VBA 連接到表格 - 而不是 MSEXCEL。

我正在使用下面的代碼,但是當我嘗試連接時出現如下所示的錯誤。 如果我在 Excel 中嘗試相同的代碼,它工作正常 - 它只會在 Autodesk Inventor 中出錯。

我檢查了我的項目參考資料,它們是匹配的。 我不確定還能嘗試什么。 搜索該錯誤會出現一些與權限相關的問題,但它們似乎與 Excel 相關聯。我在這里使用的方法是否為 Excel 所獨有?

在此處輸入圖像描述

Sub PostGresTest()

Dim oConn As New ADODB.Connection
Dim cmd As New ADODB.Command
' Connection Parameters
Dim strUsername As String
Dim strPassword As String
Dim strServerAddress As String
Dim strDatabase As String
' User:
strUsername = "TEST"
' Password:
strPassword = "password"
' Server Address:
strServerAddress = "localhost"
' Database
strDatabase = "postgres"

oConn.Open "DSN=PostgreSQL35W;" & _
"Database=" & strDatabase & ";" & _
"Uid=" & strUsername & ";" & _
"Pwd=" & strPassword
.......

我省略了代碼的 rest,因為錯誤發生在oConn.Open行。

有什么建議么?

我對代碼和我的 DSN 設置做了一些更改,然后我的 PC 在一夜之間重新啟動以進行 windows 更新。 今天早上,一切正常。 我認為這可能是重新啟動造成的,因為它在重新啟動之前不起作用,但也許我的設置無論如何都是錯誤的。

我發現了一個 Stack 問題,它建議 DSN 應該設置在“系統 DSN”而不是“用戶 DSN”下 -(抱歉 - 我丟失了鏈接) - 所以我也做了那個改變。

這是最終代碼:

Function PostGresTest(Optional StrSQL As String = "")

Dim oConn As New ADODB.Connection
Dim cmd As New ADODB.Command
' Connection Parameters
Dim strUsername As String
Dim strPassword As String
Dim strServerAddress As String
Dim strDatabase As String
' User:
strUsername = "TEST"
' Password:
strPassword = "password"
' Server Address:
strServerAddress = "localhost"
' Database
strDatabase = "postgres"
   
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Driver={PostgreSQL UNICODE};" & _
         "DSN=PostgreSQL35W;" & _
         "Database=" & strDatabase & ";" & _
         "SERVER=" & strServerAddress & ";" & _
         "Uid=" & strUsername & ";" & _
         "Pwd=" & strPassword
 .....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM