Vídeo recomendado
https://youtu.be/diWPPPhW-9E

[RESOLVIDO]Ajuda em salvar dados

Discussões sobre a integração do Excel com o Banco de Dados Access

Moderador: joseA

grsn
Acabou de chegar
Acabou de chegar
Mensagens: 9
Registrado em: Qua Mar 18, 2020 7:01 am

[RESOLVIDO]Ajuda em salvar dados

Mensagem por grsn »

Estou salvando dados de uma planilha em um banco de dados access usando o seguinte código

Código: Selecionar todos

Sub ConectaDB()

Dim cnn         As New ADODB.Connection
Dim bncDados    As String
Dim wbk         As Workbook
Dim Sql         As String
Dim rs          As New Recordset
Dim lin         As Long
Dim ultLin      As Long
Dim Cod      As String

Set wbk = WbDataBase
lin = 2

ultLin = Application.WorksheetFunction.CountA(Planilha2.Range("A:A"))

bncDados = wbk.Path & "\DB.accdb"

If cnn.State = 0 Then

    cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                            "Data Source=" & bncDados & ";" & _
                            "Jet OLEDB:Database Password="

    cnn.Open

End If

With Planilha2

    For lin = 2 To ultLin
        Cod = .Cells(lin, 2)
                Turma = .Cells(1, 1)
                 Sql = "Select * From  Produtos   Where RM Like '" & Cod & "' And Classe like '" & Turma & "'"
                    rs.Open Sql, cnn, adOpenKeyset, adLockOptimistic
                    
                    If rs.RecordCount > 0 Then
                        rs!Nome = .Cells(lin, 1)
                        rs!RM = .Cells(lin, 2)
                        rs!Classe = .Cells(1, 1)
                        rs!Disc1 = .Cells(lin, 8)
                        rs!Disc2 = .Cells(lin, 9)
                        rs!Disc3 = .Cells(lin, 10)
                        rs!Disc4 = .Cells(lin, 11)
                        rs!Disc5 = .Cells(lin, 12)
                        rs!Disc6 = .Cells(lin, 13)
                        
                        rs.Update
                        
                    Else

                        rs.AddNew
                
                        rs!Nome = .Cells(lin, 1)
                        rs!RM = .Cells(lin, 2)
                        rs!Classe = .Cells(1, 1)
                        rs!Disc1 = .Cells(lin, 8)
                        rs!Disc2 = .Cells(lin, 9)
                        rs!Disc3 = .Cells(lin, 10)
                        rs!Disc4 = .Cells(lin, 11)
                        rs!Disc5 = .Cells(lin, 12)
                        rs!Disc6 = .Cells(lin, 13)
                        rs.Update
                        
                   End If
            rs.Close
            
        Next lin

End With

cnn.Close

End Sub
Como faria para que uma linha que tenha um determinado valor, no caso -- não seja salva.
Desde já agradeço
Anexos
Captura de tela 2022-05-01 210654.png
Captura de tela 2022-05-01 210654.png (34.7 KiB) Exibido 1440 vezes
Editado pela última vez por grsn em Qui Mai 05, 2022 7:17 pm, em um total de 1 vez.


Disable adblock

This site is supported by ads and donations.
If you see this text you are blocking our ads.
Please consider a Donation to support the site.


Avatar do usuário
webmaster
Administrador
Mensagens: 3114
Registrado em: Sex Jul 24, 2009 2:44 pm
Contato:

Re: Ajuda em salvar dados

Mensagem por webmaster »

If Cells(lin, 7).Value == "--" Then...


grsn
Acabou de chegar
Acabou de chegar
Mensagens: 9
Registrado em: Qua Mar 18, 2020 7:01 am

Re: [RESOLVIDO]Ajuda em salvar dados

Mensagem por grsn »

If .Cells(lin, 7).Value <> "--" Then
Deu certo vlw
Tinha colocado no lugar errado junto com o if já existente

If rs.RecordCount > 0 and .Cells(lin, 8).Value <> "--" Then


Disable adblock

This site is supported by ads and donations.
If you see this text you are blocking our ads.
Please consider a Donation to support the site.


Responder