x_soldier Posted July 26, 2013 at 03:43 PM Report #520099 Posted July 26, 2013 at 03:43 PM (edited) Boas Amigos, VB não é a minha praia de maneira que estou com uma dificuldade que deverá ser básica, se alguém me pudesse ajudar agradecia 🙂 Tenho o seguinte código que faz update a uma BD Access e o que pretendo é que não só escreva o OK quando faz o processo como quando não faz Update escreve-se o NOK. Sub Button5_Click() Set conn = New ADODB.Connection Set rs = New ADODB.Recordset rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic conn.ConnectionString = "Provider=MSDASQL.1;Password=VanFor;Persist Security Info=True;Data Source=VanFor_Teste" conn.Open rs.ActiveConnection = conn Set c = Sheets("Dividendos_Inserir").Range("B1").End(xlDown) For i = 2 To c.Row connstr = "SELECT * FROM Agregados where Periodo='" & Sheets("Dividendos_Inserir").Cells(i, 1) _ & "' And CodigoISIN='" & Sheets("Dividendos_Inserir").Cells(i, 2) & "'" rs.Open connstr Do While Not rs.EOF rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update rs.MoveNext Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" Loop rs.Close Next i conn.Close End Sub Desde já agradeço Abraço Edited July 27, 2013 at 02:11 PM by thoga31 GeSHi
nelsonr Posted July 26, 2013 at 03:57 PM Report #520103 Posted July 26, 2013 at 03:57 PM O que pretendes é colocar NOK quando por exemplo dá erro ao atualizar os campos? Podes usar um try... catch na area de atualização de campos, e colocar o OK depois do update (que estaria dentro do try) e o NOK no catch. Algo do tipo (nao testado): try rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" catch Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" end try rs.MoveNext
x_soldier Posted July 26, 2013 at 04:32 PM Author Report #520107 Posted July 26, 2013 at 04:32 PM Acho que coloquei o assunto no sitio errado 😕 Sorry
nelsonr Posted July 26, 2013 at 04:50 PM Report #520112 Posted July 26, 2013 at 04:50 PM (edited) Não precisavas criar outro topico. Um moderador logo movia. Em vez do try catch, podes usar o On error goto label. Algo do tipo (não testado): on error goto erro rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" goto continuar erro: Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" continuar: on error goto 0 rs.MoveNext Edited July 26, 2013 at 04:52 PM by nelsonr
x_soldier Posted July 26, 2013 at 05:15 PM Author Report #520119 Posted July 26, 2013 at 05:15 PM Não precisavas criar outro topico. Um moderador logo movia. Em vez do try catch, podes usar o On error goto label. Algo do tipo (não testado): on error goto erro rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" goto continuar erro: Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" continuar: on error goto 0 rs.MoveNext Obrigado 🙂 No caso não é quando dá erro mas sim quando não insere porque a query não devolve nada. Obrigado pela atenção 🙂
nelsonr Posted July 26, 2013 at 05:26 PM Report #520125 Posted July 26, 2013 at 05:26 PM Então podes por de outra forma: if not rs.EOF then Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" Do While Not rs.EOF rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update rs.MoveNext Loop else Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" endif
x_soldier Posted July 26, 2013 at 05:43 PM Author Report #520127 Posted July 26, 2013 at 05:43 PM (edited) Então podes por de outra forma: if not rs.EOF then Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" Do While Not rs.EOF rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update rs.MoveNext Loop else Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" endif És o maior 😄 Muito Obrigado.... Deixo aqui o código para algum nabo como eu!! Sub InsertDividendos() Set conn = New ADODB.Connection Set rs = New ADODB.Recordset rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic conn.ConnectionString = "Provider=MSDASQL.1;Password=VanFor;Persist Security Info=True;Data Source=VanFor_Teste" conn.Open rs.ActiveConnection = conn Set c = Sheets("Dividendos_Inserir").Range("B1").End(xlDown) For i = 2 To c.Row connstr = "SELECT * FROM Agregados where Periodo='" & Sheets("Dividendos_Inserir").Cells(i, 1) _ & "' And CodigoISIN='" & Sheets("Dividendos_Inserir").Cells(i, 2) & "'" rs.Open connstr If Not rs.EOF Then Sheets("Dividendos_Inserir").Cells(i, 5) = "OK" Do While Not rs.EOF rs.Fields("CuponDivNValores").Value = Sheets("Dividendos_Inserir").Cells(i, 3) rs.Fields("CuponDivImporteEfectivo").Value = Sheets("Dividendos_Inserir").Cells(i, 4) rs.Update rs.MoveNext Loop Else Sheets("Dividendos_Inserir").Cells(i, 5) = "NOK" End If rs.Close Next i conn.Close End Sub Edited July 27, 2013 at 02:12 PM by thoga31 GeSHi
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