Go Back   Access World Forums > Microsoft Access Discussion > Forms

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 06-13-2019, 06:05 AM   #1
IgnoranceIsBliss
Newly Registered User
 
Join Date: Jun 2019
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
IgnoranceIsBliss is on a distinguished road
Validation For Form

Hi - I have a form that has the below controls
2 combo boxes
1 listbox
2 text boxes
1 button

What is the best way to ensure that when the button is pressed all controls have input/selection made?

IgnoranceIsBliss is offline   Reply With Quote
Old 06-13-2019, 06:09 AM   #2
theDBguy
I’m here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 3,040
Thanks: 36
Thanked 722 Times in 705 Posts
theDBguy will become famous soon enough theDBguy will become famous soon enough
Re: Validation For Form

Hi. The recommended approach is to use the form’s BeforeUpdate event for validating records.
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
Old 06-13-2019, 06:10 AM   #3
IgnoranceIsBliss
Newly Registered User
 
Join Date: Jun 2019
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
IgnoranceIsBliss is on a distinguished road
Re: Validation For Form

Quote:
Originally Posted by theDBguy View Post
Hi. The recommended approach is to use the form’s BeforeUpdate event for validating records.
When does the before update event fire? Would that go for each control or for the button?

IgnoranceIsBliss is offline   Reply With Quote
Old 06-13-2019, 06:17 AM   #4
Micron
Newly Registered User
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 720
Thanks: 3
Thanked 148 Times in 142 Posts
Micron will become famous soon enough Micron will become famous soon enough
Re: Validation For Form

Depends. Bascialy you can write a bunch of If statements or a loop through those controls. If there are or will be more of them in the future, they could be included in the loop but that could be either good or bad. If they are bound, then you have to worry about when the check is made and what has to happen on pass or fail. Then does that happen every time it finds a problem (so that could be 5 times for you) or just once?

I see that responses appear in advance view - I never saw that before.
Oh well, as I was composing an answer for how, not when, I'll leave it at this.
__________________
Sometimes I just roll my eyes out loud...
Windows 10; Office 365 (Access 2016)
Micron is offline   Reply With Quote
Old 06-13-2019, 06:19 AM   #5
jdraw
Super Moderator
 
jdraw's Avatar
 
Join Date: Jan 2006
Location: Ottawa, Ontario, Canada;West Palm Beach, Florida
Posts: 11,940
Thanks: 74
Thanked 1,969 Times in 1,917 Posts
jdraw is just really nice jdraw is just really nice jdraw is just really nice jdraw is just really nice jdraw is just really nice
Re: Validation For Form

Here is a reference link re Form Events sequence.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
jdraw is offline   Reply With Quote
Old 06-13-2019, 06:20 AM   #6
IgnoranceIsBliss
Newly Registered User
 
Join Date: Jun 2019
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
IgnoranceIsBliss is on a distinguished road
Re: Validation For Form

Quote:
Originally Posted by Micron View Post
Depends. Bascialy you can write a bunch of If statements or a loop through those controls. If there are or will be more of them in the future, they could be included in the loop but that could be either good or bad. If they are bound, then you have to worry about when the check is made and what has to happen on pass or fail. Then does that happen every time it finds a problem (so that could be 5 times for you) or just once?

I see that responses appear in advance view - I never saw that before.
Oh well, as I was composing an answer for how, not when, I'll leave it at this.
No controls will be added. 3 of the controls are bound

I was originally thinking of writing IF statements in the button press event and basically if validation fails show a message to the user as why it failed and stop code execution for the button press.
IgnoranceIsBliss is offline   Reply With Quote
Old 06-13-2019, 06:58 AM   #7
theDBguy
I’m here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 3,040
Thanks: 36
Thanked 722 Times in 705 Posts
theDBguy will become famous soon enough theDBguy will become famous soon enough
Re: Validation For Form

Quote:
Originally Posted by IgnoranceIsBliss View Post
When does the before update event fire? Would that go for each control or for the button?
No, none of them. This event fires whenever a record on a form needs to be saved to the bound table. So, it will/should catch all the controls that were changed.

__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
Old 06-13-2019, 07:13 AM   #8
Micron
Newly Registered User
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 720
Thanks: 3
Thanked 148 Times in 142 Posts
Micron will become famous soon enough Micron will become famous soon enough
Re: Validation For Form

Then on form update event something like
Code:
Dim ctl As Control

For Each ctl in me.Controls
  If ctl.ControlType = acTextbox or ctl.ControlType = acListbox Or ctl.ControlType = acCombobox Then
    If Nz(ctl,0) = 0 Then
      msgbox "You forgot one"
      Cancel = True
      Exit Sub
    End If
  End if
Next
This can get much more elaborate, such as
- reporting all at one time in a list
- using attached label captions rather than sometimes cryptic control names (the above makes no mention of which are missing)
- skipping some controls and not others
Micron is offline   Reply With Quote
Old 06-13-2019, 08:24 AM   #9
IgnoranceIsBliss
Newly Registered User
 
Join Date: Jun 2019
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
IgnoranceIsBliss is on a distinguished road
Smile Re: Validation For Form

Quote:
Originally Posted by theDBguy View Post
No, none of them. This event fires whenever a record on a form needs to be saved to the bound table. So, it will/should catch all the controls that were changed.
Okay, I misunderstood your question. None of the information is being saved to a table. However a few controls pull information from a table.

On the button press a report is created that uses data from the form, which is why I want to ensure nothing is null/empty. So the report has all data that it should.
IgnoranceIsBliss is offline   Reply With Quote
Old 06-13-2019, 08:25 AM   #10
IgnoranceIsBliss
Newly Registered User
 
Join Date: Jun 2019
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
IgnoranceIsBliss is on a distinguished road
Re: Validation For Form

Quote:
Originally Posted by Micron View Post
Then on form update event something like
Code:
Dim ctl As Control

For Each ctl in me.Controls
  If ctl.ControlType = acTextbox or ctl.ControlType = acListbox Or ctl.ControlType = acCombobox Then
    If Nz(ctl,0) = 0 Then
      msgbox "You forgot one"
      Cancel = True
      Exit Sub
    End If
  End if
Next
This can get much more elaborate, such as
- reporting all at one time in a list
- using attached label captions rather than sometimes cryptic control names (the above makes no mention of which are missing)
- skipping some controls and not others
That is short and sweet. I would like to notify the user of the specific control tho. Is there a way to provide that? And if multiple are blank just providing the first null/empty control is fine.
IgnoranceIsBliss is offline   Reply With Quote
Old 06-13-2019, 08:29 AM   #11
theDBguy
I’m here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 3,040
Thanks: 36
Thanked 722 Times in 705 Posts
theDBguy will become famous soon enough theDBguy will become famous soon enough
Re: Validation For Form

Quote:
Originally Posted by IgnoranceIsBliss View Post
Okay, I misunderstood your question. None of the information is being saved to a table. However a few controls pull information from a table.

On the button press a report is created that uses data from the form, which is why I want to ensure nothing is null/empty. So the report has all data that it should.
Well, if the data is not being saved to a table, then the BeforeUpdate event won't help you. And since you simply don't want to open/print the report if there's incomplete data, then the "best" place, I think, to put your validation is in your code where you initiate opening/printing the report. For example, if you have a button to print/view the report, then you can put the validation routine there. Cheers!
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
Old 06-13-2019, 10:11 AM   #12
IgnoranceIsBliss
Newly Registered User
 
Join Date: Jun 2019
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
IgnoranceIsBliss is on a distinguished road
Re: Validation For Form

Quote:
Originally Posted by theDBguy View Post
Well, if the data is not being saved to a table, then the BeforeUpdate event won't help you. And since you simply don't want to open/print the report if there's incomplete data, then the "best" place, I think, to put your validation is in your code where you initiate opening/printing the report. For example, if you have a button to print/view the report, then you can put the validation routine there. Cheers!
Would this validation for each control be advisable?

Code:
If Me.combo0.ListIndex = “-1” Then
  MsgBox “Please select from combo box”
  Me.combo0.SetFocus
  Exit Sub
End If
IgnoranceIsBliss is offline   Reply With Quote
Old 06-13-2019, 10:19 AM   #13
theDBguy
I’m here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 3,040
Thanks: 36
Thanked 722 Times in 705 Posts
theDBguy will become famous soon enough theDBguy will become famous soon enough
Re: Validation For Form

Quote:
Originally Posted by IgnoranceIsBliss View Post
Would this validation for each control be advisable?

Code:
If Me.combo0.ListIndex = “-1” Then
  MsgBox “Please select from combo box”
  Me.combo0.SetFocus
  Exit Sub
End If
It all depends on your goal. The above example should work except you could just simply check if a Combobox is Null. For example:
Code:
If IsNull(Me.ComboName) Then
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
Old 06-13-2019, 12:52 PM   #14
Micron
Newly Registered User
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 720
Thanks: 3
Thanked 148 Times in 142 Posts
Micron will become famous soon enough Micron will become famous soon enough
Re: Validation For Form

How did we go from "3 controls are bound" to none are bound?

Unbound controls still have a Before Update event, yes? So with some bound and some not, either event could be used, I think.

This is getting confusing. Maybe a db copy would keep this from getting too much longer.

Micron is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
validation value in form? cikwan82 Forms 6 12-19-2013 12:07 AM
Form validation tezread Forms 9 07-28-2011 07:18 PM
form validation kris2011 Forms 2 06-28-2011 10:51 AM
Validation on a form RickR Forms 4 11-20-2004 06:41 PM
[SOLVED] Validation on a form rayn Forms 3 10-11-2003 11:27 PM




All times are GMT -8. The time now is 02:38 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World