AVISAR DE O CLIENTE E DUPLICADO
Mais uma vez estou postando, pois não achei nada parecendo pra esse exemplo abaixo
Quando inserir novo registro avisar se já tem
meu banco de dados é o access
No meu txtNome.text
Quando incluir um novo cliente e digitar Ex: [ Paulo Siva]
Msg = “ Já Existe um Cliente com esse Nome Cadastrado “
Grato
t+
Quando inserir novo registro avisar se já tem
meu banco de dados é o access
No meu txtNome.text
Quando incluir um novo cliente e digitar Ex: [ Paulo Siva]
Msg = “ Já Existe um Cliente com esse Nome Cadastrado “
Grato
t+
Citação:
Vamos la, antes de você fazer o INSERT vc tem que fazer um SELECT para saber se ja existe o nome digitado.
exemplo:
antes do insert vc vai colocar um codigo parecido com esse....
dim Verifica as String
SQL = [Ô]SELECT nome FROM tabela WHERE nome=[ô][Ô] & txtNome.Text & [Ô][ô][Ô][Ô]
rst.Open SQL, cnn1, adOpenDynamic, adLockOptimistic
Do While Not rst.EOF
Verifica = rst([Ô]nome[Ô])
rst.movenext
loop
if Verifica <> [Ô][Ô] then
Msg = “ Já Existe um Cliente com esse Nome Cadastrado “
exit sub
end if
Bom acho que é isso, qualquer coisa posta parte do seu codigo...
espero ter ajudado
exemplo:
antes do insert vc vai colocar um codigo parecido com esse....
dim Verifica as String
SQL = [Ô]SELECT nome FROM tabela WHERE nome=[ô][Ô] & txtNome.Text & [Ô][ô][Ô][Ô]
rst.Open SQL, cnn1, adOpenDynamic, adLockOptimistic
Do While Not rst.EOF
Verifica = rst([Ô]nome[Ô])
rst.movenext
loop
if Verifica <> [Ô][Ô] then
Msg = “ Já Existe um Cliente com esse Nome Cadastrado “
exit sub
end if
Bom acho que é isso, qualquer coisa posta parte do seu codigo...
espero ter ajudado
no caso usaria ??
Private Function Valida() As Boolean
[ô]No campo de entrada estu usando asim
Private Sub txtNome_GotFocus()
SelecionaTexto txtNome
End Sub
depois tenho este:
Public Sub Verifica()
Dim Verifica As String
Dim SQL As String
SQL = [Ô]Select * From CadPaciente Where Nome =[ô][Ô] & txtNome.Text & [Ô][ô][Ô]
RS.Open SQL, CnSql, adOpenDynamic, adLockOptimistic
Do While Not RS.EOF
Verifica = RS([Ô]Nome[Ô])
RS.MoveNext
Loop
If Verifica <> [Ô][Ô] Then
MsgBox [Ô]Já Existe um Cliente com esse Nome Cadastrado [Ô], vbExclamation, [Ô] Sisnews Informações[Ô]
Exit Sub
End If
End Sub
mas da um erro!!!!
Private Sub txtNome_GotFocus()
SelecionaTexto txtNome
End Sub
depois tenho este:
Public Sub Verifica()
Dim Verifica As String
Dim SQL As String
SQL = [Ô]Select * From CadPaciente Where Nome =[ô][Ô] & txtNome.Text & [Ô][ô][Ô]
RS.Open SQL, CnSql, adOpenDynamic, adLockOptimistic
Do While Not RS.EOF
Verifica = RS([Ô]Nome[Ô])
RS.MoveNext
Loop
If Verifica <> [Ô][Ô] Then
MsgBox [Ô]Já Existe um Cliente com esse Nome Cadastrado [Ô], vbExclamation, [Ô] Sisnews Informações[Ô]
Exit Sub
End If
End Sub
mas da um erro!!!!
perdao
é asim
Private Sub txtNome_GotFocus()
SelecionaTexto txtNome
Verifica
End Sub
é asim
Private Sub txtNome_GotFocus()
SelecionaTexto txtNome
Verifica
End Sub
Isso ocorre por você está abrindo um rs que já está aberto. Para corrigir, deve fecha-lo e depois abrir.
Coloque isso no lostFocus da TxtNome:
Coloque isso no lostFocus da TxtNome:
Private Sub TxtNome_LostFocus()
If Rs.State = 1 Then Rs.Close
SQL = [Ô]Select Nome From CadPaciente Where Nome =[ô][Ô] & TxtNome.Text & [Ô][ô][Ô]
Rs.Open SQL, CnSql, adOpenKeyset, adLockPessimistic
If Rs.RecordCount > 0 Then
MsgBox [Ô]Já Existe um Cliente com esse Nome Cadastrado [Ô], vbExclamation, [Ô] Sisnews Informações[Ô]
End If
End Sub
Está faltando instanciar o Recordset.
Colca esta linha antes do rs.Open: Set rs = New ADODB.Recordset.
Colca esta linha antes do rs.Open: Set rs = New ADODB.Recordset.
Outra coisa, evite select com asterisco, pois deixa sua consulta mais lenta.
O ideal seria o seguinte:
Private Sub TxtNome_LostFocus()
Dim sSql as String
Dim Rs as ADODB.Recordset
sSql= [Ô]SELECT NOME FROM CADPACIENTE WHERE NOME =[ô][Ô] & Trim$(TxtNome.Text) & [Ô][ô][Ô]
Set Rs = new ADODB.Recorset
Rs.Open sSql, conexao, adOpenKeyset, adLockPessimistic
If Rs.EOF = True Then
MsgBox [Ô]Já existe um cliente cadastrado com esse Nome![Ô], vbExclamation, NomeSistema
End If
If Rs.State = adStateOpen then
Rs.Close
Set Rs = Nothing
End if
End Sub
O ideal seria o seguinte:
Private Sub TxtNome_LostFocus()
Dim sSql as String
Dim Rs as ADODB.Recordset
sSql= [Ô]SELECT NOME FROM CADPACIENTE WHERE NOME =[ô][Ô] & Trim$(TxtNome.Text) & [Ô][ô][Ô]
Set Rs = new ADODB.Recorset
Rs.Open sSql, conexao, adOpenKeyset, adLockPessimistic
If Rs.EOF = True Then
MsgBox [Ô]Já existe um cliente cadastrado com esse Nome![Ô], vbExclamation, NomeSistema
End If
If Rs.State = adStateOpen then
Rs.Close
Set Rs = Nothing
End if
End Sub
Optimus, se o record está aberto é porque já foi instanciando na parte do código que não foi mostrado. A questão do asterisco tambem tratei no meu post acima
Valeu mesmo ROBIU
Deu certinho... que aliviu Huuf..
estou usando assim
Private Sub txtNome_LostFocus()
If RS.State = 1 Then RS.Close
Sql = [Ô]Select Nome From CadPaciente Where Nome =[ô][Ô] & txtNome.Text & [Ô][ô][Ô]
RS.Open Sql, CnSql, adOpenKeyset, adLockPessimistic
If RS.RecordCount > 0 Then
MsgBox [Ô]Já Existe um Cliente com esse Nome Cadastrado [Ô], vbExclamation, [Ô] Sisnews Informações[Ô]
txtNome.Text = [Ô][Ô]
txtNome.SetFocus
End If
RS.Close
End Sub
[ô]**************************************************
Abusado da sua boa vontade e experiência
Tem como quando avisar que já existe o nome e jogar os dados no meu FORM.
Grato novamente
Deu certinho... que aliviu Huuf..
estou usando assim
Private Sub txtNome_LostFocus()
If RS.State = 1 Then RS.Close
Sql = [Ô]Select Nome From CadPaciente Where Nome =[ô][Ô] & txtNome.Text & [Ô][ô][Ô]
RS.Open Sql, CnSql, adOpenKeyset, adLockPessimistic
If RS.RecordCount > 0 Then
MsgBox [Ô]Já Existe um Cliente com esse Nome Cadastrado [Ô], vbExclamation, [Ô] Sisnews Informações[Ô]
txtNome.Text = [Ô][Ô]
txtNome.SetFocus
End If
RS.Close
End Sub
[ô]**************************************************
Abusado da sua boa vontade e experiência
Tem como quando avisar que já existe o nome e jogar os dados no meu FORM.
Grato novamente

tem sim... joga as variaveis no declarations para poder usar em outros procedures'
Private Sub txtNome_LostFocus()
If RS.State = 1 Then RS.Close
Sql = [Ô]Select Nome From CadPaciente Where Nome =[ô][Ô] & txtNome.Text & [Ô][ô][Ô]
RS.Open Sql, CnSql, adOpenKeyset, adLockPessimistic
If RS.RecordCount > 0 Then
Dim msg, Style, Title, Help, Ctxt, Response, MyString
msg = [Ô]Deseja Cadastrar outro Cliente ?[Ô] [ô] Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 [ô] Define buttons.
Title = [Ô]Cliente já cadastrado![Ô] [ô] Define title.
Help = [Ô]DEMO.HLP[Ô] [ô] Define Help file.
Ctxt = 1000 [ô] Define topic
Response = MsgBox(msg, Style, Title, Help, Ctxt)
If Response = vbYes Then [ô] User chose Yes.
MyString = [Ô]Yes[Ô] [ô] Perform some action.
txtNome.Text = [Ô][Ô]
txtNome.SetFocus
RS.Close
Else [ô] User chose No.
MyString = [Ô]No[Ô] [ô] Perform some action.
carregadados [ô] CHAMA UM PROCEDURE Q USA a mesma string Sql para carregr os dados
End If
End Sub
Private Sub txtNome_LostFocus()
If RS.State = 1 Then RS.Close
Sql = [Ô]Select Nome From CadPaciente Where Nome =[ô][Ô] & txtNome.Text & [Ô][ô][Ô]
RS.Open Sql, CnSql, adOpenKeyset, adLockPessimistic
If RS.RecordCount > 0 Then
Dim msg, Style, Title, Help, Ctxt, Response, MyString
msg = [Ô]Deseja Cadastrar outro Cliente ?[Ô] [ô] Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 [ô] Define buttons.
Title = [Ô]Cliente já cadastrado![Ô] [ô] Define title.
Help = [Ô]DEMO.HLP[Ô] [ô] Define Help file.
Ctxt = 1000 [ô] Define topic
Response = MsgBox(msg, Style, Title, Help, Ctxt)
If Response = vbYes Then [ô] User chose Yes.
MyString = [Ô]Yes[Ô] [ô] Perform some action.
txtNome.Text = [Ô][Ô]
txtNome.SetFocus
RS.Close
Else [ô] User chose No.
MyString = [Ô]No[Ô] [ô] Perform some action.
carregadados [ô] CHAMA UM PROCEDURE Q USA a mesma string Sql para carregr os dados
End If
End Sub
Tópico encerrado , respostas não são mais permitidas