how do i create restrictions in textboxes ?

beanbeanbean

Registered User.
Local time
Today, 04:17
Joined
Sep 17, 2008
Messages
124
Hi everyone. i've been trying to figure this out hope you guys can help me.

I'm trying to figure out how to put restrictions into text boxes.

For example:
The textbox has to have 9 figures E.g S8344009G before it can be actually entered into the table

or

The textbox has to have the @ inside as its a textbox to input emails

or

The date has to be input in order like. 01 / 12 / 2008 or it would not go through.


Any gurus willing to help please ?


Thanks.
 
Howzit

Look into the input mask help in access. This should point you in the right direction. You would put the input mask against each relevant control in teh "Data" tab of the control properties.

For instance your input mask for a date could be 00/00/0000;;#
 
i'm sorry i'm kinda a noob on this. i dun quite understand u. may i ask u to rephrase that ? =D sorry..
 
Howzit

In design view of your form, click on the control you want to restrict entry to. Go to the data tab of the control properties (double clicking on the control should open the properties box if not open - alternatively right click Properties). There is an option called input mask. Click on this and hit F1 - this will bring up the help on the types of input mask.

Enter the relevant mask you require.
 
oh my ! thank you so much. that helped alot. may i ask u one more question ? how do i change the error names to my own custom error boxes ? cos the error box that pops out might be hard for the person using the form to understand. god bless u.
 
If you need to create custom error messages for Input Mask Violations below is some sample code you can work with. You will need to put this under the Forms Event Tab in the "On Error" Event.

Code:
'This is to nullify the default M$ Access 'Input Mask Violation' Error.
Dim CurrentControl As Control
Set CurrentControl = Screen.ActiveControl

If DataErr = 2279 And CurrentControl.Name = "Your Control Name" Then
MsgBox "Your Custom Error Message...", vbExclamation, "Your Custom Message Title"
Response = acDataErrContinue

Else:
'Do Nothing
End If

Hope this helps
 
Just to point out that Input Mask is quite useful for specifying a specific format (e.g. make sure that all phone numbers are same formats), normally Validation Rule (just under the Input Mask property) are used to restrict certain data (e.g. no phone number can be 000-00-0000 or something like that) and you can also use Validation Text property to customize the error message displayed.
 
hey thanks for the help. but i'm kinda of stuck now as my events tab does not have the on error box. i've tried using the validation rule and text but it does not cover the input mask error.



My code looks something like this now thanks to gold007eye.


'This is to nullify the default M$ Access 'Input Mask Violation' Error.
Dim CurrentControl As Control
Set CurrentControl = Screen.ActiveControl

If DataErr = 2279 And CurrentControl.name = "customerone" Then
MsgBox Msg & "IC number cannot be less than 9 characters.", _
vbExclamation, "Insufficient Data Entry"
Response = acDataErrContinue

Else:
'Do Nothing
End If



anyone know where i can put it ?


i've tried putting it in the onclick and ondirty events tabs. but they dun seem the work even thought i've selected the [Event Procedure] in their boxes. the code will look something like this


Private Sub customerone_OnDirty(Cancel As Integer)
'This is to nullify the default M$ Access 'Input Mask Violation' Error.

Dim CurrentControl As Control
Set CurrentControl = Screen.ActiveControl

If DataErr = 2279 And CurrentControl.name = "customerone" Then
MsgBox Msg & "IC number cannot be less than 9 characters.", _
vbExclamation, "Insufficient Data Entry"
Response = acDataErrContinue

Else:
'Do Nothing
End If
End Sub




doesnt seem to work. anyone knows why ?
 
I would just use the control's BeforeUpdate event and this code:

Code:
Dim foo As String

foo=Me.MyTextbox

If len(foo)<9 Then
    Msgbox "The data cannot be less than 9 characters"
    Cancel = True
End If
 
@bean

What you need to do it on the Forms "OnError" Event procedure not on the Controls.
 
I would just use the control's BeforeUpdate event and this code:

Code:
Dim foo As String
 
foo=Me.MyTextbox
 
If len(foo)<9 Then
    Msgbox "The data cannot be less than 9 characters"
    Cancel = True
End If



hi banana, i've changed the code and added it in but it doesnt work. The problem now goes like this. i have this textbox and i type in 8 numbers instead of 9. then when i try to navigate away from the textbox, the error pops out saying: The value you entered isn't appropriate for the input mask '>AAAAAAAAA' specified for this field.



Thanks for your help gold007eye. but i'm still not too sure where to put it. may u enlighten me please ?
 
As I pointed out before, Input Mask is useful for enforcing a specific format or supplying the format for the user (e.g. they don't have to enter the ()s and the -s for a phone number in this format (000) 000-0000) rather than validating the data. If you want to have a custom error, it's usually easier to either use Validation Rule/Validation Text property or BeforeUpdate event.

If you clear the input mask, you should now see your custom error. Using Form's Error Event will indeed trap the input mask and allow you to specify a custom messagebox; it's just that I think using either BeforeUpdate event or Validation Rule is easier method of validating the data.

YMMV.
 
oh ! i'm really sorry i think i mis-interpred what u typed to me. your last post was clear to me. i guess i'm just plain old lousy. hahaha. thank you so much !
 
I have attached an image that should be able to walk you through the steps. Let me know if you are still having issues with it after that. ;)
 

Attachments

  • Form - On Error Event.jpg
    Form - On Error Event.jpg
    99.1 KB · Views: 130
ohhh thank you so much brother. i thought it was the property of the textbox. thats so kind of you. thanks =)
 
Not a problem. Glad I was able to help..

I find this method of handling Input mask violations to be much easier since you can set it up in 1 Event Procedure to handle violations for any given text control on your form.
 

Users who are viewing this thread

Back
Top Bottom