All fields are mandatory ! Issue

Krays23

Registered User.
Local time
Today, 22:49
Joined
Jun 18, 2015
Messages
46
Hi Guys

Seen a few posts on this and yet still cant make it work for me

I need in my form which has 10 different combo boxes or txtboxes to be filled out before the will allow you to save it !

Ive been close with this module but all it does when I hit save is shut form down and saves even if there are still blank boxes

To be honest im not 100% sure of what areas to change of this code to suit my db

the required tag do you write required in the properties box of other?

Code tags added by UG
Code:
 Public Function validateform(myform As Form) As Boolean
'returns true if all required fields have data, or false if not. 
'It will also create a popup message explaining which fields need data

Dim boolresponse As Boolean
Dim strError As Variant
Dim ctl As Control
boolresponse = True
strError = Null
With myform
    For Each ctl In .Controls
      With ctl
        If .Tag = "required" Then
           If .Value & "" = "" Then
                boolresponse = False
                strError = (strError + ", ") & .Name
            End If
        End If
      End With
    Next ctl
End With
If strError & ""  "" Then MsgBox "The following information must be entered first: " & strError, vbInformation
validateform = boolresponse
End Function

Any help would be very welcome

Dan
 
Last edited by a moderator:
Have you put the word "required" in the tag property of the controls that you want to check?
 
What code do you've in the close event of the form or where do you call the function?
 
Yes to no avail I have just found out that in the fields you can change that to required = yes? that seems to work but gives a horrible user message on screen is there a way to edit that?
 
Sorry to confuse you all. in the design view of the tbl I am using its self in the properties box you can change required setting to "yes" then when editing the form it flags up a message it says "you must enter a value in the field tblmanualRewardIncident.roadmaproute." field its the right thing I want but I want to be able to have a personal message there not this one that's built into access
 
I don't understand? Your original question said that it was saving with blank entries?

Are you now saying that it is working OK and your original question is answered?
 
I stopped using that code completely that I showed at the start because it didn't actually appear to do any thing. It closed down the form and saved entries in my table regardless if a box had data or not.

This required field I just found does the job I want but shows a message box for the error that a blank box exisits that I don't like I want to be able to edit the text in that box which I doubt I can right?

to refresh

I have a form that staff fill out but they keep forgetting certain key boxes and leave blank what I want is some code to run on the click of a save button at the bottom of the form which will prevent staff from saving by displaying a message (of my text) upon the click of ok in that message box it will just return them to the form to fix the missing data
 
Krays23,

It is not easy to "hit" a moving target.

What is latest status -- please be specific with messages, forms, text boxes etc?
Did you explicitly put data in the .Tag property?

You might want to research the Form Before Update Event as the place to check all required fields before leaving the record.
 
one simple way of doing this is to set the fields in the table as

"required= yes"
"allow zero length strin" = no
and no default value

you will then get a standard access message when you try to save an "illegal" record.

alternatively you can explicitly test for values as you are doing.

either way closing the form with a partly edited record will give you fairly unhelpful rejection messages. Changing the message is a different thing to managing the data.
 
one simple way of doing this is to set the fields in the table as

"required= yes"
"allow zero length strin" = no
and no default value

you will then get a standard access message when you try to save an "illegal" record.
If you want a simple but yet effective method, then go with this. Validation will be done on the server rather than the client and I would imagine that you don't have that many users to worry about continuously using the server for validation. This also ensures that, in the event where your db admin needs to manually make some changes in the table directly, that validation is still in place to ensure data integrity.

The standard error message can be caught and re-formatted in the form's On Error event.

With regards your current setup, there are two things:
1. In the code below, you're missing "=":
Code:
If strError & "" [COLOR="blue"]=[/COLOR] "" Then
2. Like jdraw mentioned, you need the Before Update event of the form, and in your case you need to test the function for True or False in this fashion:
Code:
If validateform(Me) = False Then
[COLOR="blue"]    Cancel = True[/COLOR]
End If
That's the crucial line.
 
I am going to throw in my two cents worth because I like my strategy.

On opening the form turn the background of the required fields yellow.

In the after update event of each field, check the value for something other than null, if met, background turns white, otherwise background turns yellow. No message yet about it being empty or full. After all, the person has not asked to save it yet, they are still working on the form. Wait till they finish and ask to save it.

Then in the on click event of the save/close button check all the required fields again. I call the procedure CheckRequired and this gives you the option to customize the error message, like the first name field is empty, rather than a required field is empty. And you can set the focus to that field.

I also use a global variable called Problem. When the save event starts I set it to zero. The next line says Call CheckRequired. The next line checks to see if Problem is zero and then it saves the record. You see, if any of the required fields are empty, Problem is set to a number, where the background is turned to yellow, and the entire process stops.

Anyway, that's my strategy and I use it in forms where I am editing a record and creating a new one. And to give you another tip, the values from CheckRequired are assigned to public variables on that form. Then if Problem equals zero, I run CheckDupes. This procedure checks the table for duplicate records with a criteria string generated using the variables. And if both of those procedures have executed without a problem, meaning Problem still equals zero, then the record is saved.
 
On opening the form turn the background of the required fields yellow.
I used to do a similar thing on some applications but rather than changing fore/background colour I put a red asterisk next to the control with a control tip text describing the field.

In some applications, I change the border to red if it's required. There are so many ways of signifying which fields are required.
 
Yeah, that might be better as it matches the printed forms people are used to filling out. Where did you get the asterisk?
I will tell you though, the yellow background really pops in user's faces and they get the message, particularly when they edit a record and wipe out a required field.
I hope my comments help the guy, he is going down a dark hole using tags and required fields.
 
I'm sure it gives them a fright ;)

The asterisk is just a label, it moves around as and when it's required. And it helps if you build a Class around it so that it can be used in multiple forms.
 
Great stuff thanks a lot guys for your help !!! love this forum
 

Users who are viewing this thread

Back
Top Bottom