I am new to ADO. I tried to copy/paste from what I found in another forum, but I got nowhere. Here is my set-up. I host the SQL Server database. I set it up for Active Directory authentication. Another company sends me excel spreadsheets by email. I want to basically insert the data from the Excel spreadsheet into my database. Here is the general idea:
When an email comes, I have an Outlook script grab the attachment. If it is an excel file, it opens it up. Once it is open, it figures out how many rows the spreadsheet has. Then, it goes row-by-row creating a SQL string to insert data into my database. Once it finishes, it opens a connection to my database and runs the SQL string I just created. Next, I determine what was deleted from the last update, and I delete my master data table. Then, I determine what was added from the last update, and I add it to the master data table. Finally, I remove the old data, copy the new data to the old data, and delete the new data. This will set it up for the next time that the spreadsheet arrives.
Here is my code:
I have no clue how it works so far, as I cannot even get it to connect to the database. I do not understand the connection strings at all.'Initialize New Variables Dim iMaxRow, iRowsToUpdate As Integer Dim strInsert As String Dim dbSQL As ADODB.Connection 'Figure out how many rows there are With xlWorkbook.Worksheets(1) .Activate .Cells(1, 1).Select End With While Selection.Row < 65536 iMaxRow = Selection.Row - 1 Selection.End(xlDown).Select Wend 'Start building the INSERT statement strInsert = "INSERT INTO dbo.NewData (Field1, Field2, Field3, Field4)" & vbCrLf & "VALUES" & vbCrLf iRowsToUpdate = 0 If iMaxRow > 1 Then For iRow = 2 To iMaxRow With xlWorkbook.Worksheets(1) 'Figure out if you want to add the row If <true row is useful> Then iRowsToUpdate = iRowsToUpdate + 1 'After first row to add, need a comma If iRowsToUpdate > 1 Then strInsert = strInsert & "," & vbCrLf End If 'Reformat the data strInsert = strInsert & "(" & _ .Cells(iRow, 1).Value & "," & _ 'Call FormatValue2 function FormatValue2(.Cells(iRow, 3).Value) & "," & _ 'Call FormatValue3 function FormatValue3(.Cells(iRow, 4).Value) & "," & _ .Cells(iRow, 5).Value & ")" End If End With Next End If If iRowsToUpdate > 1 Then 'Add the semicolon to complete the SQL statement strInsert = strInsert & ";" 'Open up a connection Set dbSQL = New ADODB.Connection 'Set the ConnectionString dbSQL.ConnectionString = "driver={SQL Server};" & _"server=localhost;database=MyDatabase"
'Open the connection to the database dbSQL.Open dbSQL.Execute (strInsert) 'Figure out which rows were removed from the previous update and delete them from the MasterData table strSQL = "DELETE FROM dbo.MasterData" & vbCrLf & _"WHERE (Field1, Field2, Field3, Field4) IN " & vbCrLf & _"(SELECT Field1, Field2, Field3, Field4" & vbCrLf & _"FROM dbo.OldData" & vbCrLf & _"WHERE (Field1, Field2, Field3, Field4) NOT IN " & vbCrLf & _"(SELECT Field1, Field2, Field3, Field4" & vbCrLf & _"FROM dbo.NewData))" dbSQL.Execute (strSQL) 'Figure out which rows were not in the previous update and add them to the MasterData table strSQL = "INSERT INTO dbo.MasterData (Field1, Field2, Field3, Field4)" & vbCrLf & _"SELECT Field1, Field2, Field3, Field4" & vbCrLf & _"FROM dbo.NewData" & vbCrLf & _"WHERE (Field1, Field2, Field3, Field4) NOT IN " & vbCrLf & _"(SELECT Field1, Field2, Field3, Field4" & vbCrLf & _"FROM dbo.OldData)" dbSQL.Execute (strSQL) 'Clear all data from the OldData table strSQL = "DELETE * FROM dbo.OldData" dbSQL.Execute (strSQL) 'Move the data from the NewData table to the OldData table strSQL = "INSERT INTO dbo.OldData (Field1, Field2, Field3, Field4)" & vbCrLf & _"SELECT Field1, Field2, Field3, Field4" & vbCrLf & _"FROM dbo.NewData" dbSQL.Execute (strSQL) 'Clear the NewData table to get it ready for the next spreadsheet that arrives strSQL = "DELETE * FROM dbo.NewData" dbSQL.Execute (strSQL) dbSQL.CommitTrans dbSQL.Close Set dbSQL = Nothing End If