VBA - working with data videos | Excel VBA Part 32 - Executing SQL Commands with ADO

Posted by Andrew Gould on 02 April 2014

How to construct and manipulate command objects in ADO to talk to databases using VBA

You can download any files that you need to follow the video here.

You can increase the size of the video:

Full screen mode for YouTube

You can view the video in full screen mode as shown on the left, using the icon at the bottom right of the frame.

You can also increase the quality of the video:

Changing resolution

You can improve the resolution of the video using another icon at the bottom right of the frame. This will slow down the connection speed, but increase the display and sound quality. This icon only becomes visible when you start playing the video.

Finally, if nothing happens when you play the video, check that you're not using IE in compatibility view.

This page has 1 thread Add post
02 Jun 19 at 11:49

Dear Andrew,

I'm having a problem with this lesson

I'm using SQL Server 2016 (installed by your instractions) and in this video you're using SQL Server 2012.

The problem is ConnectionString. Examples from "https://www.connectionstrings.com/sql-server-2016/" doesn't work at all

Using ConnectionString from Excel data Connection does do some work but useless to add anydata to database. 

Here is my current ConnectionString "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=ma\new_sql2016;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=MA;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=Movies"

Would you please excuse my ignorance and drop a small hint how to fix this problem.

Sincerely yours

Alexander

 

03 Jun 19 at 07:33

Hi there, here's some basic code which will work with the new version of the Movies database:

Const SQLConStr As String = _
    "Provider=SQLNCLI11;Server=.\SQL2016;DataBase=Movies;Trusted_Connection=yes"
 
Sub ConnectToDB()
 
    Dim MoviesConn As ADODB.Connection
    Dim MoviesData As ADODB.Recordset
    Dim r As Range
    
    Set MoviesConn = New ADODB.Connection
    Set MoviesData = New ADODB.Recordset
    
    MoviesConn.ConnectionString = SQLConStr
    MoviesConn.Open
    
    On Error GoTo CloseConnection
    
    With MoviesData
       .ActiveConnection = MoviesConn
       .Source = "Film"
       .LockType = adLockOptimistic
       .CursorType = adOpenKeyset
       .Open
               
        On Error GoTo CloseRecordset
        
        Sheet1.Activate
        For Each r In Range("A3", Range("A3").End(xlDown))
        
            .AddNew
            .Fields("Title").Value = r.Offset(0, 1).Value
            .Fields("ReleaseDate").Value = r.Offset(0, 2).Value
            .Fields("RunTimeMinutes").Value = r.Offset(0, 3).Value
            .Update
            
        Next r
    End With
    
CloseRecordset:
    MoviesData.CancelUpdate
    MoviesData.Close
    
CloseConnection:
    MoviesConn.Close
    
    Set MoviesData = Nothing
    Set MoviesConn = Nothing
    
End Sub
 

I hope that helps!