Why is my MSOLEDBSQL connection string in Excel not connecting to my SQL Express db?

I’m trying to connect to a new SQL Express Db and can establish the connection with a BAT file, but not in VBA. The name in SQL Server Management Studio is: Data Source=James-desktopSQLEXPRESS.History. I can connect with SMS as well at the BAT. I do have Integrated Security (Windows Authentication) setup, and since it works with the BAT, that presumably can’t be the problem.

I have used each of the connectionStr values shown below and they all produce the same error (the connection string output is different for all, but the rest of the error msg is unchanged).

Error:
Attempting to establish connection to the database…
Connection String: Provider=MSOLEDBSQL;Data Source=James-desktopSQLEXPRESS;Initial Catalog=History;Integrated Security=True;Connect Timeout=30
Error Number: -2147217887
Error Description: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
Failed to establish connection to the database.
Connection failed!


Here’s the VBA, and again, I tried all of those connectionStr statements seperately.:

Sub TestDatabaseConnection()
Dim dbFilePath As String
Dim connectionStr As String
Dim conn As Object

' Set the database file path and connection string
dbFilePath = "C:BeakerMarket DataDatabaseHistory.mdf"


connectionStr = "Provider=MSOLEDBSQL;Data Source=James-desktopSQLEXPRESS;Initial Catalog=History;Integrated Security=True;Connect Timeout=30"
connectionStr = "Provider=SQLOLEDB;Data Source=James-desktopSQLEXPRESS;Initial Catalog=History;Integrated Security=True;Connect Timeout=30"
connectionStr = "Provider=MSOLEDBSQL;Data Source=James-desktopSQLEXPRESS;Initial Catalog=History;Integrated Security=True;Connect Timeout=30"
connectionStr = "Provider=SQLOLEDB;Data Source=(LocalDb)v16.0;Initial Catalog=History;Integrated Security=True;Connect Timeout=30"
connectionStr = "Provider=MSOLEDBSQL;Data Source=James-desktopSQLEXPRESS;AttachDbFilename=" & _
    dbFilePath & ";Integrated Security=True;Connect Timeout=30"

' Print the connection string to the immediate window for debugging
Debug.Print "Attempting to establish connection to the database..."
Debug.Print "Connection String: " & connectionStr

' Open the connection
Set conn = CreateObject("ADODB.Connection")
On Error Resume Next ' Continue execution even if an error occurs
conn.Open connectionStr
If Err.Number <> 0 Then
    ' Print the error details to the immediate window
    Debug.Print "Error Number: " & Err.Number
    Debug.Print "Error Description: " & Err.Description
    Debug.Print "Failed to establish connection to the database."
End If
On Error GoTo 0 ' Reset error handling to default behavior

' Check if the connection was successful
If conn.State = 1 Then
    Debug.Print "Connection successful!"
    ' Close the connection
    conn.Close
Else
    Debug.Print "Connection failed!"
End If

' Cleanup
Set conn = Nothing

End Sub

Here is the BAT file I can use and it connects fine:
set server=James-desktopSQLEXPRESS
set database=History

rem Test connection using sqlcmd with Windows Authentication
sqlcmd -S %server% -d %database% -E -Q “SELECT 1”

rem Check errorlevel to determine success
if %errorlevel% equ 0 (
echo Connection successful!
) else (
echo Connection failed!
)

Any ideas?

New contributor

S. Jagermanjensen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật