Option Explicit

Sub ConnectToSQLServer()
'MicsosoftActive X Data Objects Library 6.1がearly bindingしてあるものとして

Dim SQLServerConnection As String
Dim MyServerName As String
MyServerName = "MYSURFACE"

Dim myDataBaseName As String
myDataBaseName = "Movies"

SQLServerConnection = "Provider=MSOLEDBSQL;" & _
                                    "Server=" & MyServerName & ";" & _
                                    "Database=" & myDataBaseName & ";" & _

Dim adoConn As ADODB.Connection
Dim adoRecSet As ADODB.Recordset

Set adoConn = New ADODB.Connection
Set adoRecSet = New ADODB.Recordset

  adoConn.Open SQLServerConnection 'SQLSeverを開く"
    Debug.Print "SQLSever Connection Opened"

Dim SQLServerSQLString As String
SQLServerSQLString = "SELECT * FROM dbo.actor;"
With adoRecSet
    .CursorLocation = adUseClient
    .CursorType = adOpenDynamic
    .Open SQLServerSQLString, adoConn
    Debug.Print "Record Count is = " & .RecordCount
        Debug.Print "SQLServer RecordSet Closed"
End With

    Debug.Print "SQLSever Connection Closed"

Set adoRecSet = Nothing
Set adoConn = Nothing

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *