NUNCA MAIS PASSE RAIVA POR NÃO CONSEGUIR RESOLVER UM PROBLEMA COM O EXCEL - GARANTIDO!
UNIVERSIDADE DO VBA - Domine o VBA no Excel Criando Sistemas Completos - Passo a Passo - CLIQUE AQUI
Objetivo:
Continuar o desenvolvimento dosistema, criando neste tutorial o bancode dados para armazenar registros etambém codificar o formulárioDados_empresa, tudo de forma simples e direta.
Pré-requisito:
Para você poder acompanhar odesenvolvimento deste tutorial, será necessário ter conhecimento no mínimo do “CursoBásico de Excel e os Tutoriais Utilizando Editor do Visual Basic do Excel Parte1 e Parte2” Parte 1, Parte 2 eParte 3 deste tutorial.
Utilizaremos a primeira planilhado excel, modifique o nome Plan1 para a “EMPRESA”, depois iremos inserir osnomes dos campos abaixo.
COD, NOME, IE, CNPJ, ENDERECO, BAIRRO, CIDADE, ESTADO, CEPe TELRESIDENCIAL.
Tela 001
Insira cor cinza,negrito e centralizado os campos.
Tela 002
Devemos fazer agora a formatação dos campos conformedescrição abaixo.
COD, IE, e CNPJcolocar na categoria “número”.
Tela 003
Os demais campos você deveseguir as formatações abaixo:
NOME, ENDERECO, BAIRRO, CIDADEe ESTADO inserir na categoria “texto”.
CEP inserir na categoria “especial”.
TELCOMERCIAL inserir na categoria “especial”.
Terminado o banco de dados,iremos codificar o sistema inserindo o código abaixo dentro do evento initialize do formulário.
Label_n =Application.WorksheetFunction.CountA(Plan1.Columns(1)) – 1
Tela 004
Agora vamos inseriros códigos nos botões, sempre dentro doevento clic.
BotãoCadastrar:
Dim bd As Database
Dim Rs As Recordset
Set bd =OpenDatabase(ThisWorkbook.Path & "\" & ThisWorkbook.Name,False, False, "excel 8.0")
Set Rs =bd.OpenRecordset("EMPRESA$", dbOpenDynaset)
If Me.text_cod >Me.Label_n Then
If Me.text_nome = "" Then
Me.text_nome = "-"
End If
If Me.Text_IE = "" Then
Me.Text_IE.Text = "-"
End If
If Me.Text_cnpj = "" Then
Me.Text_cnpj.Text ="-"
End If
If Me.Text_endereco = ""Then
Me.Text_endereco.Text ="-"
End If
If Me.text_bairro = ""Then
Me.text_bairro.Text ="-"
End If
If Me.Combo_cidade = ""Then
Me.Combo_cidade.Text ="-"
End If
If Me.Combo_estado = ""Then
Me.Combo_estado.Text ="-"
End If
If Me.text_cep = "" Then
Me.text_cep.Text ="-"
End If
If Me.Text_telcomercial ="" Then
Me.Text_telcomercial.Text ="-"
End If
Dim CADASTRO(1 To 11)
CADASTRO(1) = UCase(Me.text_cod)
CADASTRO(2) = UCase(Me.text_nome)
CADASTRO(3)= UCase(Me.Text_IE)
CADASTRO(4) = UCase(Me.Text_cnpj)
CADASTRO(5) = UCase(Me.Text_endereco)
CADASTRO(6) = UCase(Me.text_bairro)
CADASTRO(7) = UCase(Me.Combo_cidade)
CADASTRO(8) = UCase(Me.Combo_estado)
CADASTRO(9) = UCase(Me.text_cep)
CADASTRO(10) = UCase(Me.Text_telcomercial)
CADASTRO(11) = UCase(Me.text_cod.Value)
Dim VT As Object
Dim L, i
Set VT = Plan1.Cells(1,1).CurrentRegion
L = VT.Rows.Count + 1
If Len(Me.text_cod) = 0 Then
MsgBox "VOCÊ NÃODIGITOU NENHUM NOME PARA INCLUSÃO", vbCritical, "CADASTRO DECLIENTES"
Else
For i = 1 To 11
Plan1.Cells(L, i).Value = Trim(CADASTRO(i))
Next i
Me.text_cod.Text = ""
Me.text_nome.Text =""
Me.Text_IE.Text = ""
Me.Text_cnpj.Text =""
Me.Text_endereco.Text =""
Me.text_bairro.Text =""
Me.Combo_cidade.Text = ""
Me.Combo_estado.Text = ""
Me.text_cep.Text = ""
Me.Text_telcomercial.Text = ""
MsgBox"CADASTRADO", vbInformation, "EFETUADO COM SUCESSO"
ThisWorkbook.Save
End If
Exit Sub
Else
MsgBox "No campo COD digite um número maior do que háno campo Total Registro para casdastrar."
End If
Universidade do VBA - Curso Completo, com Certificado e com Atualizações Semanais
Domine, sem Dificuldades, a Programação VBA no Excel, Através da Criação de Sistemas Profissionais Completos - Com Exemplos Práticos e Explicações Detalhadas - Passo a Passo - Tela a Tela - Comando a Comando!
Aplica-se ao Excel 2019, 2016, 2013 e 2010!
Para todos os detalhes, acesse:
Tela 005
BotãoPesquisar:
Dim bd As Database
Dim Rs As Recordset
Dim LIN
Set bd =OpenDatabase(ThisWorkbook.Path & "\" & ThisWorkbook.Name,False, False, "excel 8.0")
Set Rs = bd.OpenRecordset("EMPRESA$",dbOpenDynaset)
LIN = 2
Do Until Rs.EOF
If Rs("NOME") =Me.text_nome.Text Then
Me.text_cod =Rs.Fields("COD")
Me.Text_IE =Rs.Fields("IE")
Me.Text_cnpj =Rs.Fields("CNPJ")
Me.Text_endereco = Rs.Fields("ENDERECO")
Me.text_bairro = Rs.Fields("BAIRRO")
Me.Combo_cidade = Rs.Fields("CIDADE")
Me.Combo_estado = Rs.Fields("ESTADO")
Me.text_cep =Rs.Fields("CEP")
Me.Text_telcomercial =Rs.Fields("TELCOMERCIAL")
Me.TextBox_codf.Text = LIN
End If
LIN = LIN + 1
Rs.MoveNext
Loop
Tela 006
Botão Editar:
Dim bd As Database
Dim Rs As Recordset
Set bd =OpenDatabase(ThisWorkbook.Path & "\" & ThisWorkbook.Name,False, False, "excel 8.0")
Set Rs =bd.OpenRecordset("EMPRESA$", dbOpenDynaset)
Rs.Edit
Rs("NOME") = Me.text_nome
Rs("IE") = Me.Text_IE
Rs("CNPJ") = Me.Text_cnpj
Rs("ENDERECO") = Me.Text_endereco
Rs("BAIRRO") = Me.text_bairro
Rs("CEP") = Me.text_cep
Rs("TELCOMERCIAL") = Me.Text_telcomercial
Rs.Update
MsgBox"DADOS ALTERADOS COM SUCESSO", vbInformation, "BANCO DEDADOS"
ThisWorkbook.Save
Tela 007
Botão Apagar:
Dim VT
VT =MsgBox("DESEJA REALMENTE EXCLUIR O CLIENTE?", vbYesNo + vbQuestion,"BANCO DE DADOS")
If VT = vbYes Then
Plan1.Cells(Me.TextBox_codf,1).EntireRow.Delete
Me.text_cod.Text = ""
Me.text_nome.Text =""
Me.Text_IE.Text = ""
Me.Text_cnpj.Text =""
Me.Text_endereco.Text =""
Me.text_bairro.Text =""
Me.Combo_cidade.Text =""
Me.Combo_estado.Text = ""
Me.text_cep.Text = ""
Me.Text_telcomercial.Text =""
MsgBox "REGISTROS EXCLUÍDOCOM SUCESSO."
ThisWorkbook.Save
End If
Tela 008
Botão Voltar:
Me.hide
Tela 009
Desenvolvemos o banco de dados, fazendo as formataçõesnecessárias para cada campo, codificamos a interface “DADOS_EMPRESA”, tudo deforma simples e objetiva mostradoatravés de telas. Na próxima parte do tutorial daremos continuidade nodesenlvovimento do sistema. Bons estudos e até a próxima parte.
Contato: Telefone: (51) 3717-3796 | E-mail: webmaster@juliobattisti.com.br | Whatsapp: (51) 99627-3434
Júlio Battisti Livros e Cursos Ltda | CNPJ: 08.916.484/0001-25 | Rua Vereador Ivo Cláudio Weigel, 537 - Universitário, Santa Cruz do Sul/RS, CEP: 96816-208
Todos os direitos reservados, Júlio Battisti 2001-2025 ®
LIVRO: MACROS E PROGRAMAÇÃO VBA NO EXCEL 2016 - CURSO COMPLETO E PRÁTICO
DOMINE A PROGRAMAÇÃO VBA NO EXCEL - 878 PÁGINAS - CLIQUE AQUI