简体   繁体   中英

VBA connecting to SQL Server Express database issue

I installed a copy of SQL Server Express (latest). I am having trouble connecting to it.

Since I am the admin on this computer, it should be not needing user name password, because I created the database, and everything should fall under Windows authentication. My code is below.

I get an error:

Run-time error '-2147217843 (80040e4d)':
Invalid authorization specification

This error happens on attempting to open the connection.

When I launch a subform from main form, this code runs in initialize:

Option Explicit
Private objMyConn As ADODB.Connection
Private objMyRecordset As ADODB.Recordset

Private Sub UserForm_Initialize()
    'Declare variables'
        Set objMyConn = New ADODB.Connection
        Set objMyRecordset = New ADODB.Recordset
        Dim strSQL As String

        objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Contact;"
        objMyConn.Open

        strSQL = "Select * from Contact where Lastname like " + Chr(39) + "%" + LastSearch + Chr(39) + " And Firstname like " + Chr(39) + "%" + FirstSearch + Chr(39)
        MsgBox strSQL

End Sub

Add Integrated Security=SSPI to your connection string to indicate a trusted connection is desired:

objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Contact;Integrated Security=SSPI"

EDIT

SQL Server Express installs a named instance by default so you may need to change the data source to include the instance name:

objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;Initial Catalog=Contact;Integrated Security=SSPI"

use something like this instead of localhost

HP8300-0792\SQLEXPRESS

where HP8300-0792 is the name of your computer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM