|
|
|
|
|
Dicas
|
|
Visual Basic (Validações)
|
|
|
Título da Dica: Validar datas no formato : DD/MM/AAAA em um Textbox
|
|
|
|
Postada em 11/4/2002 por Alan
---- NO EVENTO CHANGE DO txtData CampoData_Change txtData
---- NO EVENTO VALIDATE OU LOST_FOCUS If Len(txtData) > 0 Then If Not ChecaData(txtData) Then txtData.SetFocus txtData.SelStart = 0 txtData.SelLength = 10 Exit Sub End If End If
---- NO MODULO Public Sub CampoData_Change(ByRef oControle As Object) Dim X As Byte Dim AuxStart As Byte Dim AuxData As String Dim flgData As Boolean AuxStart = oControle.SelStart AuxData = "" For X = 1 To Len(Trim(oControle.Text)) If Mid(oControle.Text, X, 1) <> "/" Then AuxData = AuxData & Mid(oControle.Text, X, 1) End If Next X Select Case Len(Trim(AuxData)) Case Is < 3: oControle.Text = AuxData Case 3, 4: flgData = False oControle.Text = Mid(AuxData, 1, 2) & "/" & Mid(AuxData, 3, 2) Case Is > 4: flgData = False oControle.Text = Mid(AuxData, 1, 2) & "/" & Mid(AuxData, 3, 2) & "/" & Mid(AuxData, 5, 4) End Select Select Case AuxStart Case 3: oControle.SelStart = 4 Case 6: oControle.SelStart = 7 Case Else: oControle.SelStart = AuxStart End Select flgData = True End Sub
Function ChecaData(Data As String) As Boolean ''Function ChecaData(ByRef oControle As Object, NomeCampo As String, Sistema As String) As Boolean 'FUNÇÃO PARA CHECAR SE DATA É VÁLIDA 'FORMATO DE ENTRADA DA DATA: DD/MM/AA OU DD/MM/AAAA 'SAÍDA: ChecaData = TRUE - DATA VÁLIDA ' ChecaData = FALSE - DATA INVÁLIDA
ChecaData = True If Len(Data) <> 8 And Len(Data) <> 10 Then ChecaData = False Exit Function End If Dim X As Integer Dim Erro As Boolean Erro = False For X = 1 To Len(Data) Select Case X Case 1, 2, 4, 5, 7, 8, 9, 10: If Not IsNumeric(Mid(Data, X, 1)) Then Erro = True Exit For End If Case 3, 6: If Mid(Data, X, 1) <> "/" Then Erro = True Exit For End If End Select Next If Erro Then ChecaData = False Exit Function End If Dim Dia As Integer Dim Mes As Integer Dia = Mid(Data, 1, 2) Mes = Mid(Data, 4, 2) If Dia < 1 Or Dia > 31 Then ChecaData = False Exit Function End If If Mes < 1 Or Mes > 12 Then ChecaData = False Exit Function End If If Not IsDate(Data) Then ChecaData = False Exit Function End If End Function
|
|
|
|
|