Using ADO in VBA to connect to PostgreSQL

前端 未结 4 1667
深忆病人
深忆病人 2021-01-24 13:10

I am having trouble finding clear and reliable examples of connecting to a PostgreSQL database from Excel using VBA ADO. Admittedly, I am new to VBA and most examples and tutori

相关标签:
4条回答
  • 2021-01-24 13:21

    Set cmd = New ADODB.Command cmd.ActiveConnection = cnn

    0 讨论(0)
  • 2021-01-24 13:32

    Not sure about the details of the actual DB connection, but there is a simple although common mistake with your statement: you need to use 'set' when working with objects:

    set cmd.ActiveConnection = cnn
    
    0 讨论(0)
  • 2021-01-24 13:36

    In the original Code, "PostgreSQL35W" is a DSN name which included the default host and port. When you changed to "PostgreSQL Unicode", it is a driver and your connection string is lacking the value for the port. Remember to access PostgreSQL directly from driver, you need at least 5 parameters:

    • host
    • port
    • userid
    • password
    • database

    If you are using DSN, some parameters may be defined as default.

    0 讨论(0)
  • 2021-01-24 13:42

    I wan't using a DSN as I am using an ODBC driver as opposed to OLE DB. By referencing a DSN, the above code works with very few changes.

    See this question for how I found the answer once I began to suspect OLE DB/ODBC to the issue. Does ADO work with ODBC drivers or only OLE DB providers?

    New Code here:

    Sub GetCustomers()
    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 = Sheets("CONFIG").Range("B4").Value
    ' Password:
    strPassword = Sheets("CONFIG").Range("B5").Value
    ' Server Address:
    strServerAddress = Sheets("CONFIG").Range("B6").Value
    ' Database
    strDatabase = Sheets("CONFIG").Range("B3").Value
    
    
    oConn.Open "DSN=my_system_dsn;" & _
        "Database=" & strDatabase & ";" & _
        "Uid=" & strUsername & ";" & _
        "Pwd=" & strPassword
    
    Set xlSheet = Sheets("CUSTOMERS")
    xlSheet.Activate
    Range("A3").Activate
    Selection.CurrentRegion.Select
    Selection.ClearContents
    Range("A1").Select
    
    Dim strSQL As String
    strSQL = "SELECT * FROM customers"
    
    cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
    cmd.ActiveConnection = oConn
    cmd.CommandText = strSQL
    
    Set rs = New ADODB.Recordset
    Set rs = cmd.Execute
    
    For i = 1 To rs.Fields.Count
        ActiveSheet.Cells(3, i).Value = rs.Fields(i - 1).Name
    Next i
    
    xlSheet.Range(xlSheet.Cells(3, 1), _
        xlSheet.Cells(3, rs.Fields.Count)).Font.Bold = True
    
    ActiveSheet.Range("A4").CopyFromRecordset rs
    
    xlSheet.Select
    Range("A3").Select
    Selection.CurrentRegion.Select
    Selection.Columns.AutoFit
    Range("A1").Select
    
    rs.Close
    oConn.Close
    
    Set cmd = Nothing
    Set param = Nothing
    Set rs = Nothing
    Set cnn = Nothing
    Set xlSheet = Nothing
    End Sub
    

    The System DSN is configured to use the PostgreSQL Unicode driver. I chose not to use OLE DB even though there is a provider available. If you look at PGFoundry, you will see it has many problems and has not been updated in several years.

    0 讨论(0)
提交回复
热议问题