alex.mattos
24-03-2015 13:23:05
Bom dia,
Sou iniciante em VBA e novo aqui no forúm, estou com algumas dúvidas básicas. Estou adaptando o formulário de cadastro de clientes do macoratti.net, para fazer um banco de dados de empregado onde trabalho. O formulário ainda não está com todos os campos que pretendo inserir mais a princípio será um banco de dados para consulta, alteração de dados cadastrados.
Por agora, para fazer funcionar preciso de três coisas:
1 - Um botão que me permita alterar os dados do cliente já cadastrado no banco de dados já pesquisado.
2 - Inserir a funcionalidade de apertar o "enter" após digitar a a matrícula e já aparecer os dados do empregado.
3 - Colocar o botão para abrir o formulário em uma aba que não seja a do próprio banco de dados.
Desde já agradeço a atenção!
Segue abaixo o código e em anexo meu projeto.
Private Sub cboEstado_Change()
If cboEstado.Value = "BA" Then
cboCidade.RowSource = "Cidades!A2:A5"
ElseIf cboEstado.Value = "PR" Then
cboCidade.RowSource = "Cidades!B3:B5"
ElseIf cboEstado.Value = "SC" Then
cboCidade.RowSource = "Cidades!C3:C6"
ElseIf cboEstado.Value = "SP" Then
cboCidade.RowSource = "Cidades!D3:D8"
ElseIf cboEstado.Value = "GO" Then
cboCidade.RowSource = "Cidades!E3:E6"
End If
End Sub
Private Sub cmdExcluir_Click()
'Declarar a variável Resp para receber uma resposta
Dim Resp As Integer
'Fazer a busca do registro digitado pelo usuário
With Worksheets("Consulta").Range("A:A")
Set c = .Find(txtCPF.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
Resp = MsgBox("Tem certeza que deseja excluir o registro?", vbYesNo, "Confirmação")
If Resp = vbYes Then
c.Select
Selection.EntireRow.Delete
'Limpar as caixas de texto
txtCPF.Value = Empty
txtNome.Value = Empty
txtEndereco.Value = Empty
cboEstado.Value = Empty
cboCidade.Value = Empty
txtTelefone.Value = Empty
txtEmail.Value = Empty
txtNascimento.Value = Empty
OptionButton1.Value = False
OptionButton2.Value = False
'Colocar o foco na primeira caixa de texto
txtCPF.SetFocus
Else
MsgBox "O registro não será excluído!"
End If
Else
MsgBox "Cliente não encontrado!"
End If
End With
Exit Sub
End Sub
Private Sub cmdFechar_Click()
Dados.Hide
End Sub
Private Sub cmdGravar_Click()
'Ativar a primeira planilha
ThisWorkbook.Worksheets("Consulta").Activate
'Selecionar a célula A3
Range("A3").Select
'Procurar a primeira célula vazia
Do
If Not (IsEmpty(ActiveCell)) Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
'Carregar os dados digitados nas caixas de texto para a planilha
ActiveCell.Value = txtCPF.Value
ActiveCell.Offset(0, 1).Value = txtNome.Value
ActiveCell.Offset(0, 2).Value = txtEndereco.Value
ActiveCell.Offset(0, 3).Value = cboEstado.Value
ActiveCell.Offset(0, 4).Value = cboCidade.Value
ActiveCell.Offset(0, 5).Value = txtTelefone.Value
ActiveCell.Offset(0, 6).Value = txtEmail.Value
ActiveCell.Offset(0, 7).Value = txtNascimento.Value
'Carregar o sexo do cliente dos botões de opção
If OptionButton1.Value = True Then
ActiveCell.Offset(0, 8).Value = "Masculino"
Else
ActiveCell.Offset(0, 8).Value = "Feminino"
End If
'Limpar as caixas de texto
txtCPF.Value = Empty
txtNome.Value = Empty
txtEndereco.Value = Empty
txtTelefone.Value = Empty
txtEmail.Value = Empty
txtNascimento.Value = Empty
'Limpar as caixas de combinação
cboEstado.Value = Empty
cboCidade.Value = Empty
'Limpar os botões OptionButton
OptionButton1.Value = False
OptionButton2.Value = False
'Colocar o foco na primeira caixa de texto
txtCPF.SetFocus
End Sub
Private Sub cmdLimpar_Click()
'Limpar as caixas de texto
txtCPF.Value = Empty
txtNome.Value = Empty
txtEndereco.Value = Empty
'Limpar as caixas de combinação
cboEstado.Value = Empty
cboCidade.Value = Empty
txtTelefone.Value = Empty
txtEmail.Value = Empty
txtNascimento.Value = Empty
'Limpar os botões CheckBox e OptionButton
OptionButton1.Value = False
OptionButton2.Value = False
'Colocar o foco na primeira caixa de texto
txtCPF.SetFocus
End Sub
Private Sub cmdPequisar_Click()
'Verificar se foi digitado um nome na primeira caixa de texto
If txtCPF.Text = "" Then
MsgBox "Digite a matricula do empregado"
txtCPF.SetFocus
GoTo Linha1
End If
With Worksheets("Consulta").Range("A:A")
Set c = .Find(txtCPF.Value, LookIn:=xlValues, LookAt:=xlPart)
If Not c Is Nothing Then
c.Activate
txtCPF.Value = c.Value
txtNome.Value = c.Offset(0, 1).Value
txtEndereco.Value = c.Offset(0, 2).Value
cboEstado.Value = c.Offset(0, 3).Value
cboCidade.Value = c.Offset(0, 4).Value
txtTelefone.Value = c.Offset(0, 5).Value
txtEmail.Value = c.Offset(0, 6).Value
txtNascimento.Value = c.Offset(0, 7).Value
'Carregando o botão de opção
If c.Offset(0, 8) = "Masculino" Then
OptionButton1.Value = True
Else
OptionButton2.Value = True
End If
Else
MsgBox "Empregado Não encontrado!"
End If
End With
Linha1:
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub txtCPF_Change()
End Sub
Módulo:
Sub OpenCourseBookingForm()
Dados.Show
End Sub
Sou iniciante em VBA e novo aqui no forúm, estou com algumas dúvidas básicas. Estou adaptando o formulário de cadastro de clientes do macoratti.net, para fazer um banco de dados de empregado onde trabalho. O formulário ainda não está com todos os campos que pretendo inserir mais a princípio será um banco de dados para consulta, alteração de dados cadastrados.
Por agora, para fazer funcionar preciso de três coisas:
1 - Um botão que me permita alterar os dados do cliente já cadastrado no banco de dados já pesquisado.
2 - Inserir a funcionalidade de apertar o "enter" após digitar a a matrícula e já aparecer os dados do empregado.
3 - Colocar o botão para abrir o formulário em uma aba que não seja a do próprio banco de dados.
Desde já agradeço a atenção!
Segue abaixo o código e em anexo meu projeto.
Private Sub cboEstado_Change()
If cboEstado.Value = "BA" Then
cboCidade.RowSource = "Cidades!A2:A5"
ElseIf cboEstado.Value = "PR" Then
cboCidade.RowSource = "Cidades!B3:B5"
ElseIf cboEstado.Value = "SC" Then
cboCidade.RowSource = "Cidades!C3:C6"
ElseIf cboEstado.Value = "SP" Then
cboCidade.RowSource = "Cidades!D3:D8"
ElseIf cboEstado.Value = "GO" Then
cboCidade.RowSource = "Cidades!E3:E6"
End If
End Sub
Private Sub cmdExcluir_Click()
'Declarar a variável Resp para receber uma resposta
Dim Resp As Integer
'Fazer a busca do registro digitado pelo usuário
With Worksheets("Consulta").Range("A:A")
Set c = .Find(txtCPF.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
Resp = MsgBox("Tem certeza que deseja excluir o registro?", vbYesNo, "Confirmação")
If Resp = vbYes Then
c.Select
Selection.EntireRow.Delete
'Limpar as caixas de texto
txtCPF.Value = Empty
txtNome.Value = Empty
txtEndereco.Value = Empty
cboEstado.Value = Empty
cboCidade.Value = Empty
txtTelefone.Value = Empty
txtEmail.Value = Empty
txtNascimento.Value = Empty
OptionButton1.Value = False
OptionButton2.Value = False
'Colocar o foco na primeira caixa de texto
txtCPF.SetFocus
Else
MsgBox "O registro não será excluído!"
End If
Else
MsgBox "Cliente não encontrado!"
End If
End With
Exit Sub
End Sub
Private Sub cmdFechar_Click()
Dados.Hide
End Sub
Private Sub cmdGravar_Click()
'Ativar a primeira planilha
ThisWorkbook.Worksheets("Consulta").Activate
'Selecionar a célula A3
Range("A3").Select
'Procurar a primeira célula vazia
Do
If Not (IsEmpty(ActiveCell)) Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
'Carregar os dados digitados nas caixas de texto para a planilha
ActiveCell.Value = txtCPF.Value
ActiveCell.Offset(0, 1).Value = txtNome.Value
ActiveCell.Offset(0, 2).Value = txtEndereco.Value
ActiveCell.Offset(0, 3).Value = cboEstado.Value
ActiveCell.Offset(0, 4).Value = cboCidade.Value
ActiveCell.Offset(0, 5).Value = txtTelefone.Value
ActiveCell.Offset(0, 6).Value = txtEmail.Value
ActiveCell.Offset(0, 7).Value = txtNascimento.Value
'Carregar o sexo do cliente dos botões de opção
If OptionButton1.Value = True Then
ActiveCell.Offset(0, 8).Value = "Masculino"
Else
ActiveCell.Offset(0, 8).Value = "Feminino"
End If
'Limpar as caixas de texto
txtCPF.Value = Empty
txtNome.Value = Empty
txtEndereco.Value = Empty
txtTelefone.Value = Empty
txtEmail.Value = Empty
txtNascimento.Value = Empty
'Limpar as caixas de combinação
cboEstado.Value = Empty
cboCidade.Value = Empty
'Limpar os botões OptionButton
OptionButton1.Value = False
OptionButton2.Value = False
'Colocar o foco na primeira caixa de texto
txtCPF.SetFocus
End Sub
Private Sub cmdLimpar_Click()
'Limpar as caixas de texto
txtCPF.Value = Empty
txtNome.Value = Empty
txtEndereco.Value = Empty
'Limpar as caixas de combinação
cboEstado.Value = Empty
cboCidade.Value = Empty
txtTelefone.Value = Empty
txtEmail.Value = Empty
txtNascimento.Value = Empty
'Limpar os botões CheckBox e OptionButton
OptionButton1.Value = False
OptionButton2.Value = False
'Colocar o foco na primeira caixa de texto
txtCPF.SetFocus
End Sub
Private Sub cmdPequisar_Click()
'Verificar se foi digitado um nome na primeira caixa de texto
If txtCPF.Text = "" Then
MsgBox "Digite a matricula do empregado"
txtCPF.SetFocus
GoTo Linha1
End If
With Worksheets("Consulta").Range("A:A")
Set c = .Find(txtCPF.Value, LookIn:=xlValues, LookAt:=xlPart)
If Not c Is Nothing Then
c.Activate
txtCPF.Value = c.Value
txtNome.Value = c.Offset(0, 1).Value
txtEndereco.Value = c.Offset(0, 2).Value
cboEstado.Value = c.Offset(0, 3).Value
cboCidade.Value = c.Offset(0, 4).Value
txtTelefone.Value = c.Offset(0, 5).Value
txtEmail.Value = c.Offset(0, 6).Value
txtNascimento.Value = c.Offset(0, 7).Value
'Carregando o botão de opção
If c.Offset(0, 8) = "Masculino" Then
OptionButton1.Value = True
Else
OptionButton2.Value = True
End If
Else
MsgBox "Empregado Não encontrado!"
End If
End With
Linha1:
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub txtCPF_Change()
End Sub
Módulo:
Sub OpenCourseBookingForm()
Dados.Show
End Sub