Click cmd button only once?

C

CW

I have a button that when clicked, saves the form (the header of an invoice)
and allocates the next number. My code is as follows:

Private Sub cmdSaveInvHeader_Click()
On Error GoTo Err_cmdSaveInvHeader_Click

Me.InvNo = Nz(DMax("[InvNo]", "[tblInvoices]")) + 1

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_cmdSaveInvHeader_Click:
Exit Sub

Err_cmdSaveInvHeader_Click:
MsgBox Err.Description
Resume Exit_cmdSaveInvHeader_Click

End Sub

Trouble is, if the button is clicked twice, naturally it will allocate two
numbers and one of the forms then has to be cancelled and we end up with void
invoices in our sequence.
Is there some way to suppress a second click, or rather the consequence of a
second click, so that only one number at a time can be allocated/saved?
(I suppose I could make the firing action a little more deliberate and less
prone to accidental activation, such as a double-click, but I would prefer to
exclude the possibility altogether, if I can).
Many thanks
CW
 
R

Ron2006

I have a button that when clicked, saves the form (the header of an invoice)
and allocates the next number. My code is as follows:

Private Sub cmdSaveInvHeader_Click()
On Error GoTo Err_cmdSaveInvHeader_Click

Me.InvNo = Nz(DMax("[InvNo]", "[tblInvoices]")) + 1

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_cmdSaveInvHeader_Click:
Exit Sub

Err_cmdSaveInvHeader_Click:
MsgBox Err.Description
Resume Exit_cmdSaveInvHeader_Click

End Sub

Trouble is, if the button is clicked twice, naturally it will allocate two
numbers and one of the forms then has to be cancelled and we end up with void
invoices in our sequence.
Is there some way to suppress a second click, or rather the consequence of a
second click, so that only one number at a time can be allocated/saved?
(I suppose I could make the firing action a little more deliberate and less
prone to accidental activation, such as a double-click, but I would prefer to
exclude the possibility altogether, if I can).
Many thanks
CW

As part of the click steps, change the focus to another field, and
then disable the button.

re-enabling it from then on would be either to close and open the form
or some other button to reset it.

Ron
 
A

Allen Browne

Do you *really* need the command button?

The invoice header will need other things, such as the client and invoice
date. Once the user has created these, you want to assign the invoice number
at the last possible moment before the record saves (to diminish the chance
2 users get assigned the same number.) The last chance you get is the
BeforeUpdate event of the form:

Private Sub Form_BeforeUpdate(Cancel As Integer)
With Me.InvNo
If IsNull(.Value) Then
.Value = = Nz(DMax("[InvNo]", "[tblInvoices]")) + 1
End If
End With
End Sub

Now if you must have a command button as well, all it needs to do is save
the record. This fires Form_BeforeUpdate, and so the invoice number gets
assigned. Therefore the command button just needs:
If Me.Dirty Then Me.Dirty = False
 
C

CW

Allen -
You're quite right (of course) - selection of the client, confirmation of
the date and a couple of other controls have already been edited before the
save button is used, and the number allocation and the saving of the record
are indeed the very last things that happen before the user moves on to the
second half of the invoice (another form on which the charge details and the
values are entered).
I like the idea of using the Before Update event of the form; I'm sure it
would handle the situation very well and I will give that a try.
As always, many thanks for your valued advice
CW


Allen Browne said:
Do you *really* need the command button?

The invoice header will need other things, such as the client and invoice
date. Once the user has created these, you want to assign the invoice number
at the last possible moment before the record saves (to diminish the chance
2 users get assigned the same number.) The last chance you get is the
BeforeUpdate event of the form:

Private Sub Form_BeforeUpdate(Cancel As Integer)
With Me.InvNo
If IsNull(.Value) Then
.Value = = Nz(DMax("[InvNo]", "[tblInvoices]")) + 1
End If
End With
End Sub

Now if you must have a command button as well, all it needs to do is save
the record. This fires Form_BeforeUpdate, and so the invoice number gets
assigned. Therefore the command button just needs:
If Me.Dirty Then Me.Dirty = False

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

CW said:
I have a button that when clicked, saves the form (the header of an
invoice)
and allocates the next number. My code is as follows:

Private Sub cmdSaveInvHeader_Click()
On Error GoTo Err_cmdSaveInvHeader_Click

Me.InvNo = Nz(DMax("[InvNo]", "[tblInvoices]")) + 1

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_cmdSaveInvHeader_Click:
Exit Sub

Err_cmdSaveInvHeader_Click:
MsgBox Err.Description
Resume Exit_cmdSaveInvHeader_Click

End Sub

Trouble is, if the button is clicked twice, naturally it will allocate two
numbers and one of the forms then has to be cancelled and we end up with
void
invoices in our sequence.
Is there some way to suppress a second click, or rather the consequence of
a
second click, so that only one number at a time can be allocated/saved?
(I suppose I could make the firing action a little more deliberate and
less
prone to accidental activation, such as a double-click, but I would prefer
to
exclude the possibility altogether, if I can).
Many thanks
CW
 

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