Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VALIDATE DATA BEFORE CONTINUING 3

Status
Not open for further replies.

Woody666

Technical User
Oct 22, 2001
17
AU
I have a form/subform set up that sets initial focus to a text field but I need a text field to:

1 Contain a value and not be left blank
2 Contain a valid value

before continuing to enter data anywhere else on the form.

Ive tried the AfterUpdate event but this only works if a value is entered and doesnt trap the Null situation. Ive tried the LostFocus event but I can only issue a warning that the field was left blank and cant reset the focus back to the original field from this event.

Is the only way to handle this to code each fields Gotfocus event to check the above and then reset the focus back to the text field if is doesnt satisfy the above conditions? Thanks
Woody
[sadeyes]
 
Doing all the GotFocus checks is one way, but you could also set all the other controls disabled in the form design, and only enable them after you get a valid value in the text box. That will keep the focus from wandering, and it also gives visual feedback to the user that they must enter this field first.

If you're allowing multiple records to be added, you'll also need to disable the other controls in the Current event, if Me.NewRec is true. Rick Sprague
 
Thanks Rick,

I have tried to disable the subform as a whole before but am unable to enable it afterwards through the program, do I have to disable all the objects on the subform and enable them all for what you have suggested or is there a way to disable the whole subform. Thanks
Woody
[sadeyes]
 
Interesting! I did some experimenting, and I see what you mean. However, you really can enable the subform in code--it's just that, apparently, there's a bug and the subform is leaving its label dimmed. However, I was able to click on controls in the subform, and even use the keyboard shortcut on the label to focus the subform.

If you don't need a keyboard shortcut on the label, I suggest you just detach it from the subform. (Cut it to the clipboard, then select the form's Detail section and paste it, then move it back to the correct position.) Then it won't become dimmed in design mode, and will stay black when the form is opened.

However, when you disable a subform control, though it does block any of the subform's controls from receiving focus, it doesn't dim them to show they're disabled. That might be confusing to the user. If it were me, I'd go ahead and enable/disable the controls on the subform.

Here's a tip for easier maintenance: Create a Public procedure in the subform's module that enables or disables the desired controls based on a parameter. Then you could call that from the main form, using this syntax:
Me![subform control].Form.EnableControls(False)
(Use False to disable or True to enable them). The benefit of this is that you don't have to keep switching between the forms to check the control names, nor, if you add a field to the subform, do you have to fix code in the main form. The Public procedure simply becomes a new, custom method of the form.

BTW, that should have been "Me.NewRecord", not "Me.NewRec", in my prior post, in case you hadn't noticed. Rick Sprague
 
Thanks again Rick,

I like the last idea, I think Ill just have to do that! Thanks
Woody
[sadeyes]
 
Use the On Exit event for the control that needs to be validated before anything else happens on the forms. This creates a Cancel parameter which if set to true somewhere in your code will not allow the control to lose focus even if you have clicked somewhere else or used the navigation buttons.

Have a great day!
 
Good catch, SBendBuckeye! I personally never use the Enter or Exit events, because I don't like to validate until all the data is entered. (It's better for head-down data entry.) So I tend to forget that they exist. But you're right; the Exit event is probably the best way. Rick Sprague
 
Thanks SBendBuckeye,

I appreciate the response and it now seems I'll be able to continue on my merry way. Thanks
Woody
[sadeyes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top