CALCULO DO CPF EM VB6

USUARIO.EXCLUIDOS 06/06/2007 17:57:26
#220107
existe codigo em vb que possa calcular o CPF e verificar se o CPF é verdadeiro ou falso atravéz de contas matemáticas.Se alquem souber por favor me passe...
USUARIO.EXCLUIDOS 06/06/2007 18:08:38
#220111
hehe, pow Hugo .. vc eh rápido hein cara !

(Desculpem pelo OFF topic, mas nao resisti)
HUGOSSOUZA 06/06/2007 18:23:18
#220116
Citação:

EMERSON_TADEU escreveu:
hehe, pow Hugo .. vc eh rápido hein cara !

(Desculpem pelo OFF topic, mas nao resisti)




hehehe.. acustumado ja.. essa ja ta no Ctrl + V .. hehehe
SILVIO.GARCIA 06/06/2007 20:14:01
#220132
'==>> Módulo


'Validação de CNPJ
Public Function FU_ValidaCGC(cgc As String) As Boolean

Dim Retorno, a, j, i, d1, d2
If Len(cgc) = 8 And Val(cgc) > 0 Then
a = 0
j = 0
d1 = 0
For i = 1 To 7
a = Val(Mid(cgc, i, 1))
If (i Mod 2) <> 0 Then
a = a * 2
End If
If a > 9 Then
j = j + Int(a / 10) + (a Mod 10)
Else
j = j + a
End If
Next i
d1 = IIf((j Mod 10) <> 0, 10 - (j Mod 10), 0)
If d1 = Val(Mid(cgc, 8, 1)) Then
FU_ValidaCGC = True
Else
FU_ValidaCGC = False
End If
Else
If Len(cgc) = 14 And Val(cgc) > 0 Then
a = 0
i = 0
d1 = 0
d2 = 0
j = 5
For i = 1 To 12 Step 1
a = a + (Val(Mid(cgc, i, 1)) * j)
j = IIf(j > 2, j - 1, 9)
Next i
a = a Mod 11
d1 = IIf(a > 1, 11 - a, 0)
a = 0
i = 0
j = 6
For i = 1 To 13 Step 1
a = a + (Val(Mid(cgc, i, 1)) * j)
j = IIf(j > 2, j - 1, 9)
Next i
a = a Mod 11
d2 = IIf(a > 1, 11 - a, 0)
If (d1 = Val(Mid(cgc, 13, 1)) And d2 = Val(Mid(cgc, _
14, 1))) Then
FU_ValidaCGC = True
Else
FU_ValidaCGC = False
End If
Else
FU_ValidaCGC = False
End If
End If
End Function

Public Function FU_ValidaCPF(CPF As String) As Boolean
Dim soma As Integer
Dim Resto As Integer
Dim i As Integer
'Valida argumento
If Len(CPF) <> 11 Then
FU_ValidaCPF = False
Exit Function
End If
soma = 0
For i = 1 To 9
soma = soma + Val(Mid$(CPF, i, 1)) * (11 - i)
Next i
Resto = 11 - (soma - (Int(soma / 11) * 11))
If Resto = 10 Or Resto = 11 Then Resto = 0
If Resto <> Val(Mid$(CPF, 10, 1)) Then
FU_ValidaCPF = False
Exit Function
End If
soma = 0
For i = 1 To 10
soma = soma + Val(Mid$(CPF, i, 1)) * (12 - i)
Next i
Resto = 11 - (soma - (Int(soma / 11) * 11))
If Resto = 10 Or Resto = 11 Then Resto = 0
If Resto <> Val(Mid$(CPF, 11, 1)) Then
FU_ValidaCPF = False
Exit Function
End If
FU_ValidaCPF = True
End Function



'==>> Para Usar meu exemplo, não uso maskedit

Private Sub txtcpf_Change()

If Len(TxtCpf) = 3 Then
TxtCpf = TxtCpf & "."
TxtCpf.SelStart = 5
End If
If Len(TxtCpf) = 7 Then
TxtCpf = TxtCpf & "."
TxtCpf.SelStart = 9
End If
If Len(TxtCpf) = 11 Then
TxtCpf = TxtCpf & "-"
TxtCpf.SelStart = 13
End If

End Sub




CPF = Mid(CPF, 1, 3) & Mid(CPF, 5, 3) & Mid(CPF, 9, 3) & Mid(CPF, 13, 2)
ret = FU_ValidaCPF(CPF)

if ret = false then
Msgbox"CPF inválido
else
'.......
endif











Tópico encerrado , respostas não são mais permitidas