How to force data entry

V

Valerie Wong

Hi,

I have a few data input forms, on these forms some of the fields are
required and I want to force user to input values, otherwise will not be able
to proceed to the next Field. How do I do this?

Cheers,
Valerie
 
B

Brendan Reynolds

Valerie Wong said:
Hi,

I have a few data input forms, on these forms some of the fields are
required and I want to force user to input values, otherwise will not be
able
to proceed to the next Field. How do I do this?

Cheers,
Valerie

It's not an approach to user interface design that I would generally
recommend, but if you really want to do it that way, you can do it by
setting the Enabled property of all controls on the form except the first
input control to False in design view. Then in the AfterUpdate event of the
first control, check to see if the user entered a valid value. If they did,
enable the next control. Here's an example ...

Private Sub TestText_AfterUpdate()

If Not IsNull(Me.TestText) Then
Me.TestNum.Enabled = True
End If

End Sub
 
M

Mr B

Valerie,

While I agree with Brendan, you might want to provide your users with a
little more flexable approach.

Normally you will have a command button on your form that will save the data
entered or cancel the creation of the entire record. If you set that button
to be disabled when the record is started or displayed. Then as the user
enters data in controls, you can check all required fields to be sure that
your user has made an entry in all required fields. When your criteria for
data entry has been met, you enable the command button.

You can even create a user defined function that does the checking for data
entry in the required fields and then just call the function from the
AfterUpdate event of each appropriate control.

Here is some air code to give you an idea:

Function ChkReqData()
If not isnull(me.NameOfSomeControl) _
and not isnull(me.NameOfSomeOtherControl) _
and me.NameOfAnotherControl > 0 then
Me.NameOfCommandButton.Enabled = true
else
Me.NameOfCommandButton.Enabled = false
endif
End Function

Place this statement in the AfterUpdate event of your controls to call the
funciton like this:

ChkReqData

You will find that you will need to call this function from each control
where users can change the data, even though the field linked to a control
may not be required, if all other required fields have appropriate data
entries and the user makes a change to any non-required field you would want
to enable the command button so they have the option to save the entry.
 
M

Mr. B

Chegu Tom,

The way that I do it is to add code like the following to the OnClick event
of the command button.

'Code Starts here
'declare variables
dim strMsg as string
Dim vbResponse
If Me.Dirty then
strMsg = "Do you want to save your changes?"
vbResponse = MsgBox("Some text", vbYesNo + vbQuestion + _
vbDefaultButton1, "Save Changes?")
if vbResponse = vbYes then
'save the conditions
me.dirty = false
EndIf
EndIf
'close the form
DoCmd.Close acForm, "YourFormName"
'end of code
 
C

Chegu Tom

Thanks Mr. B

How do I prevent the data to be written to the table when the user moves to
the next record

Tom


Mr. B said:
Chegu Tom,

The way that I do it is to add code like the following to the OnClick
event
of the command button.

'Code Starts here
'declare variables
dim strMsg as string
Dim vbResponse
If Me.Dirty then
strMsg = "Do you want to save your changes?"
vbResponse = MsgBox("Some text", vbYesNo + vbQuestion + _
vbDefaultButton1, "Save Changes?")
if vbResponse = vbYes then
'save the conditions
me.dirty = false
EndIf
EndIf
'close the form
DoCmd.Close acForm, "YourFormName"
'end of code
 
M

Mr B

Chegu Tom,

Use the BeforeUpdate event of your form and use essentially the same code
except you will need to have code that will Undo the changes that have been
made to the current record if the user indicates that they do not want to
save the changes.

'declare variables
dim strMsg as string
Dim vbResponse
If Me.Dirty then
strMsg = "Do you want to save your changes?"
vbResponse = MsgBox("Some text", vbYesNo + vbQuestion + _
vbDefaultButton1, "Save Changes?")
if vbResponse = vbYes then
'save the conditions
me.dirty = false
Else
'If you are using Access 2003 or prior, you use
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
'If you are using Access 2007, comment out the line above and
'remove the comment out for the line below to use:
'Me.Undo
EndIf
EndIf
'close the form
DoCmd.Close acForm, "YourFormName"
'end of code
 
M

Mr B

Chegu Tom,

I forgot to take out the line that closes the form in my last post, but you
will not want that line.

Sorry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top