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.
You actually almost have it. In the form's On_Unload event, put in the following code: - If Me.Closed = True Then
-
If Me.QualityClosedBy & "" = "" Then
-
Cancel = True
-
MsgBox "You must enter a value in QualityClosedBy."
-
Me.QualityClosedBy.SetFocus
-
End If
-
End If
13 1532
You actually almost have it. In the form's On_Unload event, put in the following code: - If Me.Closed = True Then
-
If Me.QualityClosedBy & "" = "" Then
-
Cancel = True
-
MsgBox "You must enter a value in QualityClosedBy."
-
Me.QualityClosedBy.SetFocus
-
End If
-
End If
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.
This worked Good. The only issue was when I compiled it, I needed to remove the extra end if.
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.
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.
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.
You must have entered something incorrectly, because my code doesn't produce the error message unless the combo box QualityClosedBy doesn't contain anything.
Here is what I have - Private Sub Form_Unload(CANCEL As Integer)
-
'Help stop unauthorized closure.
-
If Me.Closed = True Then
-
CANCEL = True
-
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."
-
Me.QualityClosedBy.SetFocus
-
End If
-
End Sub
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.
Good catch, As soon as I put that in all problems gone.
Thanks for all your help.
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.
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |