Check correct input... are in code all possible option?

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Check correct input... are in code all possible option?

Post by sal21 »

Code: Select all

Private Sub CommandButton2_Click()

    Me.TextBox1.SetFocus
    DoEvents
    If Len(Me.TNR.Text) <> 10 Then
        Me.TNR.Text = ""
        Me.LAZIONI1.Caption = "NUMERO ASSEGNO 10 CIFRE!"
        DoEvents
        Sleep (800)
        Me.LAZIONI1.Caption = ""
        Me.TNR.SetFocus
        DoEvents
    Else
        SQL = "SELECT * FROM TOT_ASSEGNI_5000 WHERE TOT_ASSEGNI_5000.NR_ASS='" & Me.TNR.Text & "'"
        Set RS = New ADODB.Recordset
        RS.CursorLocation = adUseClient
        RS.Open SQL, CNN1, adOpenStatic, adLockReadOnly

        If Not RS.EOF Then
            Erase strDBRows()
            strDBRows = RS.GetRows()
            RS.Close
            Set RS = Nothing
        Else
            Me.LAZIONI1.Caption = "NR. ASSEGNO: " & Me.TNR.Text & " NON TROVATO!"
            Me.TNR.Text = ""
            DoEvents
            Sleep (800)
            Me.LAZIONI1.Caption = ""
            Me.TNR.SetFocus
            DoEvents
        End If
    End If

End Sub
note:
- ASSEGNO is a check
- rquired 10 digit before
- only number in textbox
Last edited by HansV on 25 Apr 2018, 11:43, edited 2 times in total.
Reason: to add [code]...[/code] tags

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: heck correct input... are in code all possible option?

Post by HansV »

The code checks that the length of TNR is 10 characters. It does not check that those characters are digits.

You can force the user to enter only digits in the KeyPress event of the text box:

Code: Select all

Private Sub TNR_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case vbKey0 To vbKey9
            ' OK
        Case Else
            ' Not allowed
            KeyAscii = 0
            Beep
    End Select
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: heck correct input... are in code all possible option?

Post by sal21 »

HansV wrote:The code checks that the length of TNR is 10 characters. It does not check that those characters are digits.

You can force the user to enter only digits in the KeyPress event of the text box:

Code: Select all

Private Sub TNR_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case vbKey0 To vbKey9
            ' OK
        Case Else
            ' Not allowed
            KeyAscii = 0
            Beep
    End Select
End Sub
NICE!
But i f i cut and paste, the code dont controll lenght and numeric value.... :sad:

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Check correct input... are in code all possible option?

Post by HansV »

Try this:

Code: Select all

Private Sub CommandButton2_Click()
    Dim i As Long
    Dim f As Boolean
    Me.TextBox1.SetFocus
    DoEvents
    If Len(Me.TNR.Text) <> 10 Then
        Me.TNR.Text = ""
        Me.LAZIONI1.Caption = "NUMERO ASSEGNO 10 CIFRE!"
        DoEvents
        Sleep (800)
        Me.LAZIONI1.Caption = ""
        Me.TNR.SetFocus
        DoEvents
    Else
        For i = 1 To 10
            If Not IsNumeric(Mid(Me.TNR.Text, i, 1)) Then
                f = True
                Exit For
            End If
        Next i
        If f Then
            Me.TNR.Text = ""
            Me.LAZIONI1.Caption = "NUMERO ASSEGNO 10 CIFRE!"
            DoEvents
            Sleep (800)
            Me.LAZIONI1.Caption = ""
            Me.TNR.SetFocus
            DoEvents
        Else
            Sql = "SELECT * FROM TOT_ASSEGNI_5000 WHERE TOT_ASSEGNI_5000.NR_ASS='" & Me.TNR.Text & "'"
            Set RS = New ADODB.Recordset
            RS.CursorLocation = adUseClient
            RS.Open Sql, CNN1, adOpenStatic, adLockReadOnly
            If Not RS.EOF Then
                Erase strDBRows()
                strDBRows = RS.GetRows()
                RS.Close
                Set RS = Nothing
            Else
                Me.LAZIONI1.Caption = "NR. ASSEGNO: " & Me.TNR.Text & " NON TROVATO!"
                Me.TNR.Text = ""
                DoEvents
                Sleep (800)
                Me.LAZIONI1.Caption = ""
                Me.TNR.SetFocus
                DoEvents
            End If
        End If
    End If
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: Check correct input... are in code all possible option?

Post by sal21 »

HansV wrote:Try this:

Code: Select all

Private Sub CommandButton2_Click()
    Dim i As Long
    Dim f As Boolean
    Me.TextBox1.SetFocus
    DoEvents
    If Len(Me.TNR.Text) <> 10 Then
        Me.TNR.Text = ""
        Me.LAZIONI1.Caption = "NUMERO ASSEGNO 10 CIFRE!"
        DoEvents
        Sleep (800)
        Me.LAZIONI1.Caption = ""
        Me.TNR.SetFocus
        DoEvents
    Else
        For i = 1 To 10
            If Not IsNumeric(Mid(Me.TNR.Text, i, 1)) Then
                f = True
                Exit For
            End If
        Next i
        If f Then
            Me.TNR.Text = ""
            Me.LAZIONI1.Caption = "NUMERO ASSEGNO 10 CIFRE!"
            DoEvents
            Sleep (800)
            Me.LAZIONI1.Caption = ""
            Me.TNR.SetFocus
            DoEvents
        Else
            Sql = "SELECT * FROM TOT_ASSEGNI_5000 WHERE TOT_ASSEGNI_5000.NR_ASS='" & Me.TNR.Text & "'"
            Set RS = New ADODB.Recordset
            RS.CursorLocation = adUseClient
            RS.Open Sql, CNN1, adOpenStatic, adLockReadOnly
            If Not RS.EOF Then
                Erase strDBRows()
                strDBRows = RS.GetRows()
                RS.Close
                Set RS = Nothing
            Else
                Me.LAZIONI1.Caption = "NR. ASSEGNO: " & Me.TNR.Text & " NON TROVATO!"
                Me.TNR.Text = ""
                DoEvents
                Sleep (800)
                Me.LAZIONI1.Caption = ""
                Me.TNR.SetFocus
                DoEvents
            End If
        End If
    End If
End Sub
sorry for delay...
Work perfect.
tks