Solved Stuck with "the value you entered isn't valid for this field" (1 Viewer)

Romio_1968

Member
Local time
Today, 08:33
Joined
Jan 11, 2023
Messages
126
I have an unbound textbox, with format set to 'General Number', no decimals.
When a non numerical key is entered, the default error message pops up: "the value you entered isn't valid for this field"
1676295832251.png


To prohibit user for entering such values, i set an On Change event for the control
Code:
Private Sub PrintYearStart_Change()

  If Not IsNumeric(Me.PrintYearStart.Value) Then
    MsgBox "Valoare incorecta!" & vbNewLine & vbNewLine & _
           "Anul este un numar format din 4 cifre" & vbNewLine & _
           "(Exemplu: 1968)", vbExclamation, "Atentie"
    Me.PrintYearStart.Value = MinPrintYear
  End If
 
End Sub
Instead of executing the MsgBox and do the roll back, the default Access error message pops up, and no rollback is executed, but a Cancel seems to be done (the non numerical value stays, highlighted)

What am I doing wrong?

RESOLUTION
___________________________________________________________________
Instead of testing Me.PrintYearStart.Value, i chaned it to Me.PrintYearStart.Text
The custom behavior is now triggered

(thank you, arnelgp)
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:33
Joined
May 7, 2009
Messages
19,245
use BeforeUpdate event to validate.
both you can't assign any value on that event except to Undo it.
 

Romio_1968

Member
Local time
Today, 08:33
Joined
Jan 11, 2023
Messages
126
use BeforeUpdate event to validate.
both you can't assign any value on that event except to Undo it.
Tried... no change
The very first key is entered, the default error handler is triggered and any other action is canceled.
On form level is not set an errorhandler
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:33
Joined
May 7, 2009
Messages
19,245
PrintYearStart
this textbox does not have (or still has the original value) on the Change event.
you should check it's .Text property and not it's .Value.
 

Josef P.

Well-known member
Local time
Today, 07:33
Joined
Feb 2, 2023
Messages
827
You could also respond to the form error.
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 2113 Then 'please check error number
        with Me.ActiveControl
            if .Name = "PrintYearStart" Then
                msgbox "your message"
                .Undo
                Response = acDataErrContinue
            end if
        End with
    End If
End Sub
 

Romio_1968

Member
Local time
Today, 08:33
Joined
Jan 11, 2023
Messages
126
Tried... no change
The very first key is entered, the default error handler is triggered and any other action is canceled.
On form level is not set an errorhandler
Yes... that was it.
Checked for Me.Control.Textx and the custom behavior comes up.
Thank You
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:33
Joined
Feb 19, 2002
Messages
43,293
1. The change event runs once for EACH character typed. You have to check specifically for 0-9 to limit to numeric characters.
2. The IsNumeric() function allows non-numeric characters because it supports scientific notation.
 

Users who are viewing this thread

Top Bottom