Excel VBA to SQL Server 2016 Connection String Error

前端 未结 1 2132
我在风中等你
我在风中等你 2020-12-22 12:46

We´re trying to get VBA access to our native server database with the plugin ADODB in Excel. However, we´re running into the same error message no matter what connection str

相关标签:
1条回答
  • 2020-12-22 12:58

    Consider first checking if SQLOLEDB is among installed OLEDB providers with below PowerShell script, borrowed from this @josepmv's blog page, also available on his Gitub repo.

    PowerShell (run .ps1 script inside Powershell ISE for x86 (32-bit) and non-x86 (64-bit))

    function Get-OledbRegistered
    {
        [CmdletBinding()]
        [OutputType([System.Collections.Generic.List[PSObject]])]
        param ()
    
        Process
        {
            $list = New-Object ([System.Collections.Generic.List[PSObject]])
    
            foreach ($provider in [System.Data.OleDb.OleDbEnumerator]::GetRootEnumerator())
            {
                $v = New-Object PSObject        
                for ($i = 0; $i -lt $provider.FieldCount; $i++) 
                {
                    Add-Member -in $v NoteProperty $provider.GetName($i) $provider.GetValue($i)
                }
                $list.Add($v)
            }
            return $list
        }
    }
    
    $list = Get-OledbRegistered
    $list | ?{ $_.SOURCES_NAME.IndexOf('SQL') -ge 0 }
    

    Script above searches for any source name containing SQL. Simply, run list at end to show all OLEDB providers. Also, the available provider must match the bit-version of your MS Office. For example, MS Office 32-bit version may not be able to run OLEDB 64-bit providers and vice versa. Similarly your SQL Server bit-version must align as well to the provider.

    Also, do note you can download the latest OLEDB driver which raises an important message (emphasis added):

    The previous Microsoft OLE DB Provider for SQL Server (SQLOLEDB) and SQL Server Native Client OLE DB provider (SQLNCLI) remains deprecated and it is not recommended to use either for new development work.
    ...
    The new OLE DB provider is called the Microsoft OLE DB Driver for SQL Server (MSOLEDBSQL). The new provider will be updated with the most recent server features going forward.


    From there, adjust connection strings as needed according to availability on machine. Below are typical, untested examples for illustration.

    VBA

    Sub test()
    
        Set Conn = New ADODB.Connection
        ConnString = "Provider=MSOLEDBSQL;Server=myServer\myInstance;Database=myDatabase;Trusted_Connection=yes;"
        Conn.Open
    
        ConnString = "Provider=SQLOLEDB;Data Source=myServer\myInstance;Initial Catalog=myDatabase;Integrated Security=SSPI;"
        Conn.Open
    
        ConnString = "Provider=SQLNCLI;Server=myServer\myInstance;Database=myDatabase;UID=myUser;PWD=mypwd"
        Conn.Open
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题