Excel VBA INSERT INTO statement using variables
I've just started learning a bit of VBA, I've managed to import data from a DB created in SQL Server just fine, now I'm trying to export data from Excel into the same DB.
The following works just fine:
Dim cnState As ADODB.Connection Dim sSQL As String Set cnState = New Connection With cnState .Provider = "SQLOLEDB" .ConnectionString = "server=.\SQLEXPRESS; Trusted_Connection=Yes; database=SIM_PROJ" .Open End With sSQL = "Insert Into Alunos Values ('3', 'Tiago Aleixo', 'Rua dos Pincéis' , '1990-05-26' , 'M' , 'Multimédia' , 'D')" cnState.Execute sSQL End Sub Private Sub btn_goMenu_Click() Sheet1.Select
The only issue is, I've only managed to figure out how to insert actual values through the code, I want to be able to read a whole line in Excel and insert it into my DB.
Something like this:
sSQL = "Insert Into Alunos Values (Range(A4).Value, Range(B4).Value, Range(C4).Value, Range(D4).Value, Range(E4).Value, Range(F4).Value, Range(G4).Value)"
IE, I only want values from line 4 to be inserted.