x_soldier Posted July 26, 2013 at 05:46 PM Report #520128 Posted July 26, 2013 at 05:46 PM Boa Tarde Pessoal, Alguém pode dar uma ajudinha no seguinte: Tenho uma listagem em Excel que quero inserir atraves de uma macro numa Tabela de uma BD Access. Já andei a tentar uns Insert Into mas não está a funcionar. Alguém tem um exemplo para que eu possa ver como é a sintax. Obrigado
x_soldier Posted July 29, 2013 at 02:08 PM Author Report #520320 Posted July 29, 2013 at 02:08 PM Deixo o código para alguém que esteja enrrascado! Sub InsertPositions() Dim conn As ADODB.Connection 'Variável para a Base Dados Dim rs As ADODB.Recordset 'Variável para a Tabela Dim r As Long 'Variável para o número da linha da Sheet Set conn = New ADODB.Connection Set rs = New ADODB.Recordset 'Conectando á Base de Dados Access conn.ConnectionString = "Provider=MSDASQL.1;Password=VanFor;Persist Security Info=True;Data Source=VanFor_Teste" conn.Open rs.ActiveConnection = conn rs.Open "Agregados", conn, adOpenKeyset, adLockOptimistic, adCmdTable 'Todos os registros da tabela 'Número da linha que irá começar na Sheet r = 6 'Repete enquanto a coluna "A" for maior que Zero Do While Len(Range("A" & r).Formula) > 0 With rs 'Insere novo registo na Base de Dados .AddNew 'Adiciona valores para cada campo na tabela .Fields("CD") = Range("A" & r).Value .Fields("CI") = Range("B" & r).Value .Fields("CodigoISIN") = Range("C" & r).Value '.Fields("NumOrdAgregado") = Range("D" & r).Value .Fields("NombreISIN") = Range("E" & r).Value .Fields("Remitente") = Range("F" & r).Value .Fields("Declarante") = Range("G" & r).Value .Fields("Periodo") = Range("H" & r).Value .Fields("Prueba") = Range("I" & r).Value .Fields("Epigrafe") = Range("J" & r).Value .Fields("Titular") = Range("K" & r).Value .Fields("Pais") = Range("L" & r).Value .Fields("Emisor") = Range("M" & r).Value .Fields("SaldoInicialNValores") = Range("N" & r).Value .Fields("SaldoInicialImporte") = Range("O" & r).Value .Fields("EntradasSinPrecioNValores") = Range("P" & r).Value .Fields("SalidasSinPrecioNValores") = Range("Q" & r).Value .Fields("EntradasNValores") = Range("R" & r).Value .Fields("EntradasImporteEfectivo") = Range("S" & r).Value .Fields("SalidasNValores") = Range("T" & r).Value .Fields("SalidasImporteEfectivo") = Range("U" & r).Value .Fields("CuponDivNValores") = Range("V" & r).Value .Fields("CuponDivImporteEfectivo") = Range("W" & r).Value .Fields("SaldoFinalNValores") = Range("X" & r).Value .Fields("SaldoFinalImporte") = Range("Y" & r).Value .Fields("Desglose") = Range("Z" & r).Value .Fields("NumDesglose") = Range("AA" & r).Value .Fields("IdDeclarante") = Range("AB" & r).Value 'Actualiza o novo registro .Update Cells(r, 30) = "OK" End With 'Próxima linha da Sheet para a Base Dados r = r + 1 Loop 'Fecha a tabela rs.Close Set rs = Nothing 'Fecha a Base de Dados conn.Close Set conn = Nothing End Sub
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now