I have the following VBA code and I am getting the error message stated in the title of this question. Can anybody please help? I don’t usually write this sort of code:
Sub Save_To_DB()
'Declare some variables
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String
'Create a new Connection object
Set cnn = New ADODB.Connection
'Set the connection string
cnn.ConnectionString = "Server=server_name;Database=database_name;Trusted_Connection=True;"
'Open the Connection to the database
cnn.Open **<-THE ERROR THROWS HERE**
'Create a new Command object
Set cmd = New ADODB.Command
'Associate the command with the connection
cmd.ActiveConnection = cnn
'Go on to write the SQL statement and execute
EDIT: Ok I solved it, needed to include Driver={SQL Native Client}; in the connection string.
This means that your connection string is wrong, look here for the right one http://www.connectionstrings.com/.
If you are having difficulties finding what will work for you, attach a table using the external data wizard and then look at the connection string that it provides: