Daniel Slätt
Daniel Slätt

Reputation: 771

Excel VBA to SQL Server 2016 Connection String Error

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 string configuration we´re trying.

Any help would be appreciated! See below image and code.

Sub test()

    Set Conn = New ADODB.Connection
    ConnString = "Provider=SQLOLEDB;Data Source=databaseb006;Database=EIU"
    Conn.Open

End Sub

enter image description here

Upvotes: 0

Views: 5959

Answers (1)

Parfait
Parfait

Reputation: 107567

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

Upvotes: 2

Related Questions