Problem with button on form

T

Tom

Hi

I have been working with Access for a number of years but grope in the dark
as far as VBA is concerned (ie, don't know a lot).

I am developing a small application for reading barcode data for our factory
shop floor. The problem I am having is with one of the forms.

When the form is opened, focus is set on a text box called [OPERATOR_ID].
This is where the user scans their ID card resulting in their ID card number
and staff number being stored here. So far, so good.

On the 'On Exit' control, I have set some code to check to see if the
[OPERATOR_ID] and [CARD_STATUS] is valid. This also works fine.

The problem I have is that I want the user to be able to click on one of two
other buttons on the form: one to 'reset' the form so they can start again;
one to 'exit' the form. These buttons are called 'New_Entry_button' and
'exit' respectively. Without the code control on the [OPERATOR_ID] text box,
these also work fine. However, with the code in place, the user cannot get
past the [OPERATOR_ID] box: ie, unless they have entered a valid operator
number, they cannot click on the 'New_Entry_button' or the 'exit' button as
they just get the error message from exiting the [OPERATOR_ID] box and focus
is returned there. The coding I have used on the [OPERATOR_ID] box's 'On
Exit' control is as follows.

============
Private Sub OPERATOR_ID_Exit(Cancel As Integer)
If IsNull(Me!OPERATOR_ID) Then
MsgBox "You Cannot Proceed Without Entering An Operator Number"
Me!OPERATOR_ID.SetFocus
DoCmd.CancelEvent
ElseIf Me!CARD_STATUS <> "A" Or IsNull(Me!CARD_STATUS) Then
MsgBox "Illegal Card or Card Status Inactive - Contact Administrator"
Me!OPERATOR_ID.SetFocus
Me!OPERATOR_ID = ""
DoCmd.CancelEvent
End If
End Sub
============

I was thinking of trying to test to see if one of the buttons has been
clicked but this is beyond my knowledge of VBA. I guessed this could be done
on the 'lost focus' control on [OPERATOR_ID].

Could someone help me with this please?

Regards

Tom
 
N

Nigel Davison

Hi Tom,

maybe you should create a function called checkFrmValid that is used when
ever you want to exit the form and save the data.
Therefore any cmd buttons where you want to check the data is valid before
saving you call this function.
Otherwise, on other exits such as your "Exit" you should do a me.undo and
then close the form without doing any checks.

generally on a form you should have:

cmd actions
OK check frm, save frm, close frm
Cancel undo frm, close frm
Add New check frm, save frm, move to add frm
Undo Changes undo frm

hope that makes sense, good luck

Nigel
 
N

Nigel Davison

just rereading my post, to clarify, you should take the check valid event off
the txt box/combo box/whatever and put in the function.

hope that amkes even more sense ;-)

Nigel Davison said:
Hi Tom,

maybe you should create a function called checkFrmValid that is used when
ever you want to exit the form and save the data.
Therefore any cmd buttons where you want to check the data is valid before
saving you call this function.
Otherwise, on other exits such as your "Exit" you should do a me.undo and
then close the form without doing any checks.

generally on a form you should have:

cmd actions
OK check frm, save frm, close frm
Cancel undo frm, close frm
Add New check frm, save frm, move to add frm
Undo Changes undo frm

hope that makes sense, good luck

Nigel


Tom said:
Hi

I have been working with Access for a number of years but grope in the dark
as far as VBA is concerned (ie, don't know a lot).

I am developing a small application for reading barcode data for our factory
shop floor. The problem I am having is with one of the forms.

When the form is opened, focus is set on a text box called [OPERATOR_ID].
This is where the user scans their ID card resulting in their ID card number
and staff number being stored here. So far, so good.

On the 'On Exit' control, I have set some code to check to see if the
[OPERATOR_ID] and [CARD_STATUS] is valid. This also works fine.

The problem I have is that I want the user to be able to click on one of two
other buttons on the form: one to 'reset' the form so they can start again;
one to 'exit' the form. These buttons are called 'New_Entry_button' and
'exit' respectively. Without the code control on the [OPERATOR_ID] text box,
these also work fine. However, with the code in place, the user cannot get
past the [OPERATOR_ID] box: ie, unless they have entered a valid operator
number, they cannot click on the 'New_Entry_button' or the 'exit' button as
they just get the error message from exiting the [OPERATOR_ID] box and focus
is returned there. The coding I have used on the [OPERATOR_ID] box's 'On
Exit' control is as follows.

============
Private Sub OPERATOR_ID_Exit(Cancel As Integer)
If IsNull(Me!OPERATOR_ID) Then
MsgBox "You Cannot Proceed Without Entering An Operator Number"
Me!OPERATOR_ID.SetFocus
DoCmd.CancelEvent
ElseIf Me!CARD_STATUS <> "A" Or IsNull(Me!CARD_STATUS) Then
MsgBox "Illegal Card or Card Status Inactive - Contact Administrator"
Me!OPERATOR_ID.SetFocus
Me!OPERATOR_ID = ""
DoCmd.CancelEvent
End If
End Sub
============

I was thinking of trying to test to see if one of the buttons has been
clicked but this is beyond my knowledge of VBA. I guessed this could be done
on the 'lost focus' control on [OPERATOR_ID].

Could someone help me with this please?

Regards

Tom
 
Top