Error message if incomplete form

  • Thread starter Thread starter Todd
  • Start date Start date
T

Todd

I'm a new user, so please bear with me on what is probably
a simple question.

I have a data entry form where the user inputs 7 or 8
items which are saved in a status history table.

What I'm finding is that sometimes they forget to enter
all the info, so what I'd like to do is make it so that
when the record that they are creating is updated, there
is a check done and if any of the fields are null, they
get prompted to "check for missing fields" or something
like that.

Is this possible? Is it a matter of putting some code in
the "before update" event? What would the format of the
code look like?

Thanks for the help.
 
If you make the fields 'Required, not null' in the underlying table, then
they wont be able to close the form or move to a new record until the fields
are filled. No code needed at all.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Thanks.
However, in a few instances null may be acceptable, and
I'd like to prompt the user so that they have to think
about whether they really want to enter null.

Also, there are a few conditional instances where null may
be acceptable. eg if field 1 is x then field 2 can be
null, if field 1 is y then field 2 cannot be null.

Can someone show me the code to do this? I think it would
go in the before update event property for the form? Right?
 
In each controls BeforeUpdate event, where you want to check another field
for a value, just put

if me.field1 = "value" then

msgbox "You must enter a value in field2"

end if

Sustitute your field names and "value" text for your application. Likewise
you can customise the message box message, or write the text to an unbound
text box on the form itself, if that is more convenient - saves the user
having to click on the message box to make it go away.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
I'm almost there!
I tried what you put below, my only problem is that this
code does pop-up the message box, but it doesn't stop the
record from being updated, or stop the user from moving to
the next field.

What am I missing?
 
Put a me.undo in the code after the message box open, you can even select
whether you want to save or discard, based on what the message box returns -
ie whether the user hit Ok or Cancel.

Something like

if me.field1 = "value" then

userreply = msgbox ("You must enter a value in field2",vbOkCancel)

if userreply = vbOk then

me.undo

endif



--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
One more question for you. What you've given me works
great. The only thing I'm wondering is if there is a
different command other than me.undo to use so that all
the fields on the form aren't blanked out.
I tried what you suggested and it does eliminate the
record being saved incorrectly, however, it blanks out all
the fields that the user has just entered on the form, so
they have to re-enter them. I'd like for the message to
be displayed that says "you must enter a value..." then
when they click OK, they should just have to enter the
missing value, not re-enter all the fields.

Thanks.
 
Just do a me.textbox.setfocus to put the cursor back into the textbox where
you want editing to continue. You can select which action you want the user
to take based on the return value from the message box. In fact there are
several 'styles' of the standard message box, with buttons like Yes/No,
Cancel/OK, which allow a bit of customisation. If these are not enough,
just design a popup modal form and use for your own messages. Personally I
dislike popups for this type of use, because the user has to mouse click to
take some action, and take fingers off the keyboard, so I tend just to use
messages displayed in a status bar on the entry form, but its your choice.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
One more question.
the me.textbox.setfocus command works if the user has
tried to create a new record. However, if the user has
chosen to exit the form (via a command button that closes
the form and opens a different form), although the msgbox
still appears, when they say OK, they are still moved to
the next form, and the record is saved with the error in
it. Whats the command to abort the exit and set the focus
back to the appropriate field in the current record on the
form.

Thanks again.
 
So you have to put some code in the command button event to check for a
valid entry in the fields you need, then if the validation fails, decide
whether to do a me.undo or set the focus back to the field where you want
the correct entry, all before you close this form and open the new one.

Psuedocode:

CommandButton_Click()

.... do validation checks...

If validation fails:
Popup message box
if response is "Cancel"
do me.undo to cancel all entries
else
set focus to textbox where validation failed
end if
Exit Sub - exiting here will prevent the form closing
else - validation passed
Open next form
close this form

End if

End Sub

Note that you can write psuedo code like this, and add plenty of comments,
describing how you want the logic to work, then when you have the logic how
you want it, and the code flow following the logic, then you can translate
the psuedocode into real code, adding the correct names for variables and
controls, fixing the syntax, etc. But get the logic correct first, while
you dont have to concentrate on all the little details.
Makes coding much simpler, by breaking the task down into little pieces.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Back
Top