check field value before closing

klynn

New Member
Joined
Jun 9, 2003
Messages
11
I have a form that contains two text boxes and one combo box that must have data selected or entered before closing out the form. I would like to place a code in the close button to check the three fields for value before closing. If any of the three are null, have focus set on the first field found. If more than one field is null repeat the close process to determine. Allowing the form to close will only be allowed if all three fields have a value.
Any help would be greatly appreciated. Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
It depends on how you want to handle it. You could drop code into the forms OnClose event OR you could turn off the Close Button and add your own and use the OnClick Event to customize.

I'd recommend the latter as it gives you a lot more flexibility in approach.

From there it depends on how you want to handle things.
Would you want to have the code validate all three then step the user through editing them? Would you want to return an error message and then flag the fields visibly on the form? Would you just want to have it push you into the first detected invalid value and have the user hit close again (only to fail and send the user to the second control)

First things you need to understand is how to refer to your controls from within the form and from within a regular VBA module not directly attached to the form. From there it's just regular VBA.

Me.controlName.propertytomodify is the format to go after for the field value. You can use something like:

If len(Me.textbox1.Value)=0 Then
DoCmd.GotoControl (Me.textbox1.Name)
Exit Sub ' Use this if you want it to exit the routine
End If
If (test value) Then
End If
...

Alternatively, there are a variety of IsXXXXX (IsNumeric, IsDate - check VBA help within Access) things you can also test for directly.

Also, alternatively, your routines could include throw up basic inputboxes asking the user to manually type the value and you can push that value directly into the fields, such as:

strName = InputBox("What Value to Add?", "BoxTitle in Quotes")
Me.textbox1.Value = strName

Always remember though, you can only use 'Me' to refer to the same form you are currently on. It's equivalent to using functions elsewhere within VBA like ActiveCell, ActiveForm, etc. You can use it to drop into a subform or up into a parent object but it always starts with *this* form you are on now.

I know this doesn't give you a lot of specific answers. No "here is precisely how to do this" but it should show you the basic commands you need to use to do a little self-help.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,569
Messages
6,160,557
Members
451,656
Latest member
SBulinski1975

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top