473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VBA Code to verify data

106 64KB
I have 2 fields I need to check data for:
Field 1) is yes/no box called "Closed"
Field 2) is a combo box named "QualityClosedBy"

I need code that says and executes on closing the form:
if Closed = yes then "QualityClosedby" must not be empty and if it is empty to set focus to that field to be filled in.
This way it will force a record into a log that is controlled by code already in place. I need this as someone has been closing things out without it triggering the code for the log.
Apr 21 '16 #1

✓ answered by Seth Schrock

You actually almost have it. In the form's On_Unload event, put in the following code:
Expand|Select|Wrap|Line Numbers
  1. If Me.Closed = True Then
  2.     If Me.QualityClosedBy & "" = "" Then
  3.         Cancel = True
  4.         MsgBox "You must enter a value in QualityClosedBy."
  5.         Me.QualityClosedBy.SetFocus
  6.     End If
  7. End If

13 1532
Seth Schrock
2,965 Expert 2GB
You actually almost have it. In the form's On_Unload event, put in the following code:
Expand|Select|Wrap|Line Numbers
  1. If Me.Closed = True Then
  2.     If Me.QualityClosedBy & "" = "" Then
  3.         Cancel = True
  4.         MsgBox "You must enter a value in QualityClosedBy."
  5.         Me.QualityClosedBy.SetFocus
  6.     End If
  7. End If
Apr 21 '16 #2
NeoPa
32,556 Expert Mod 16PB
I would suggest that the proper place to do that, and what it's designed for, is the table properties.

When in Design View of a table hit Alt-Enter (or press the properties button) and look at the Validation Rule.
Apr 21 '16 #3
DJRhino
106 64KB
This worked Good. The only issue was when I compiled it, I needed to remove the extra end if.
Apr 22 '16 #4
DJRhino
106 64KB
NeoPa

Your right that the Validation rule would be good, but I needed it on the form as someone was closing issues out with out entering data to back it up, so I put in a log file to see who was doing it and it happened again but there was nothing in the log file. So this should hopefully stop this from happening again.
Apr 22 '16 #5
NeoPa
32,556 Expert Mod 16PB
It depends on what you want to do. If you want to name & shame someone then you'll need to log it with some code.

OTOH, designing the table so as to disallow it in the first place ensures it never happens, either directly or via a form.

Remember, as the designer, most of the shame would come back on you anyway as it's your responsibility to design it to work correctly. That's part of the job. I'd give that some thought.
Apr 22 '16 #6
DJRhino
106 64KB
Seth,

The code works good, but when:
"Closed" = True and "QualityClosedBy" = True
it still pops up the message. I dont want the message when both are true.

Neo,

I agree with what your saying, but I only require the Closed by part when you close it "Officially" and someone has been going in and just clicking on close. I'm just trying to prevent that.
Apr 22 '16 #7
Seth Schrock
2,965 Expert 2GB
You must have entered something incorrectly, because my code doesn't produce the error message unless the combo box QualityClosedBy doesn't contain anything.
Apr 22 '16 #8
DJRhino
106 64KB
Here is what I have
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(CANCEL As Integer)
  2. 'Help stop unauthorized closure.
  3. If Me.Closed = True Then
  4.     CANCEL = True
  5.     MsgBox "You must enter your name in Quality Closed By, If your name is not in the list, then leave this form as you are not authorized to make changes. To leave uncheck close and click the close button. Goto the read only version."
  6.     Me.QualityClosedBy.SetFocus
  7.     End If
  8. End Sub
Apr 22 '16 #9
Seth Schrock
2,965 Expert 2GB
That would explain your comment about the extra End If. You missed line 2 completely. You need to add it and the End If to go with it.
Apr 22 '16 #10
DJRhino
106 64KB
Good catch, As soon as I put that in all problems gone.

Thanks for all your help.
Apr 22 '16 #11
Seth Schrock
2,965 Expert 2GB
No problem. Just as a side note, it would probably be a good idea to also put in the field validation in your table as well to block the attempts that might go around your form. As NeoPa says, correct data is always better than catching fixing it later. I personally like doing my validation in code because it allows me to make nicer error messages, but there are ways around that, so doing both gives you the best of both worlds. I have never regretted following NeoPa's advice. A quick Google search for Conditional Field Validation should help you create the validation rule that will only allow the proper combination of values.
Apr 22 '16 #12
PhilOfWalton
1,430 Expert 1GB
Sorry to come in so late in the discussion, but I would have thought (please correct me if I'm wrong) that you equally have an error condition if Closed = No and there is a valid QualityClosedBy name.
I believe one of the best ways of handling this situation is to remove the Close button on the form and instead add a command button to close the form. That way, on pressing the Close Command button, you can run whatever checks you like before actually closing the form.
On all of my database, I go a bit further. Once the main menu is loaded, it is impossible to close the database on the normal Access close button. It has to be closed using the Close Command button on the main menu.
This allows various "housekeeping" activities like checking no other forms or reports are open and checking when the last backup was done before allowing the database to close.

Phil
Apr 22 '16 #13
DJRhino
106 64KB
I use the closed - Yes/No box for sorting.

Also the other reason I did not do the validation in the table, is we use a form to create a data entry and then once that issue is dealt with, they use another form for closing the issue out.
Apr 27 '16 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key...
2
by: Greg Strong | last post by:
Hello All, I've written code in a test database with test data. Everything seems to be working except compact database in VB code per http://www.mvps.org/access/general/gen0041.htm. The reason I...
8
by: SBC News Groups | last post by:
I have a field on a form that when a user enters a number, I want to verify that the number is already in another table. For example: If a user enters 100 in the control Bidder, I want to check...
6
by: Sandro997 | last post by:
Yes, I know there must be a guide out there somewhere that answers this question quickly, but then again most of them answer a question slightly different than the one I want. Basically, I have a...
3
by: kchang77 | last post by:
Good morning. I have two tables (Batches) and (Archive). (Batches) is a linked table that contains all of the existing data and the other (Archive) I am keying data into, through a form. I want...
2
by: gviteri | last post by:
This is my first coding attempt at VBA (6.3) and I'm very ill-rehearsed. I was hoping that someone with more experience would be able to help me out here. This is what I have: Sub Macro1() On...
4
by: soule | last post by:
Hi, Everyone, I'm trying to adapt code for an .accdb form button in the main form class module that moves a piece of data from a field in one table to the same named field in another table. Not...
1
by: PreethiGowri | last post by:
I have to code for data mining, predicting on what the customer would prefer to buy on checking his previous product selection. I'm new to this concept and i'm in a state of confusion that, where to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.