简体   繁体   English

尝试将 VBA 连接到 PostgreSQL(不是 excel)

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

I'm fairly new to PostgreSQL, and I'm trying to connect to a table via VBA from Autodesk Inventor - not MSEXCEL.我是 PostgreSQL 的新手,我正在尝试通过 Autodesk Inventor 的 VBA 连接到表格 - 而不是 MSEXCEL。

I'm using the code below, but I get the error shown below when I try to connect.我正在使用下面的代码,但是当我尝试连接时出现如下所示的错误。 If I try the same code in Excel, it works fine - it only errors in Autodesk Inventor.如果我在 Excel 中尝试相同的代码,它工作正常 - 它只会在 Autodesk Inventor 中出错。

I've checked my project references, and they match.我检查了我的项目参考资料,它们是匹配的。 I'm not sure what else to try.我不确定还能尝试什么。 Searching on that error brings up some permissions-related issues, but they seem to be linked to Excel. Are the methods I'm using here somehow exclusive to Excel?搜索该错误会出现一些与权限相关的问题,但它们似乎与 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
.......

I've omitted the rest of the code since the error occurrs on the oConn.Open line.我省略了代码的 rest,因为错误发生在oConn.Open行。

Any suggestions?有什么建议么?

I made a few changes to the code and my DSN settings then my PC rebooted overnight for a windows update.我对代码和我的 DSN 设置做了一些更改,然后我的 PC 在一夜之间重新启动以进行 windows 更新。 This morning, everything worked.今天早上,一切正常。 I suggest it was probably the reboot that did it because it didn't work before rebooting, but maybe my settings were wrong anyway.我认为这可能是重新启动造成的,因为它在重新启动之前不起作用,但也许我的设置无论如何都是错误的。

I found a Stack question that suggested that the DSN should be set up under 'System DSN' not 'User DSN' - (sorry - I lost the link) -So I made that change too.我发现了一个 Stack 问题,它建议 DSN 应该设置在“系统 DSN”而不是“用户 DSN”下 -(抱歉 - 我丢失了链接) - 所以我也做了那个改变。

Here's the final code:这是最终代码:

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