validation help

E

EddWood

I have a form with a discount option and need to set it so they maximum
value that can be entered is 100, what is the syntax to enter to ensure
users cannot enter any value greater than 100?

Thanks
 
T

Tore

If the discount is entered into a text field named txtDiscount you could do
it on the after update event of this field.

Private sub txtDiscount_AfterUpdate()
if not isnumeric (me.txtDiscount) then
Msgbox("You must enter a nunmeric value")
exit sub
end if

if me.txtDiscount > 100 then
Msgbox("You cannot enter values over 100")
me.txtDiscount = ""
end if

End sub
 
L

Linq Adams via AccessMonster.com

This type of validation code belongs in the textbox BeforeUpdate event; the
AfterUpdate event is too late, and focus will simply move to the next field
if the code is done there.

If the discount field is numeric, which it usually is, this will do the job:

Private Sub txtDiscount_BeforeUpdate(Cancel As Integer)
If Me.txtDiscount > 100 Then
MsgBox ("You cannot enter values over 100")
Cancel = True
txtDiscount.SelStart = 0
txtDiscount.SelLength = Len(Me.txtDiscount)
End If
End Sub

Notice the

Cancel = True

line. This tells Access to not update the field's value, and returns focus to
the trextbox, so that the correction can be made. The lines

txtDiscount.SelStart = 0
txtDiscount.SelLength = Len(Me.txtDiscount)

hilites the incorrect data, making correction easier on the user.
 
E

EddWood

Hi Tore,

I have tried that code, but it still allows me to enter values greater than
100??

Any other suggestions?

Regards
Edd
 
L

Linq Adams via AccessMonster.com

The code was tested and works as per your stated needs.

What datatype is txtDiscount?

I guess you need to copy your code and post it here.
 
L

Linq Adams via AccessMonster.com

Sorry, two more questions:

Did you remove the code from the AfterUpdate event?

What version of Access are you using?
 
E

EddWood

cool guys, sorted it, my datatype is a SQL 'float' type and as such maximum
value is 1.0, sorry thanks for your help

Edd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top