Coding one command button with two different commands or events.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone,
I know this question has probably been asked before, but I just can't seem
to get it right. How do I code a single command button with two entirely
different events. I'm using Access 2002. I don't know if this is the
correct process or not but here are the two different events I would like the
one command button to call:

Private Sub Command596_Click()
On Error GoTo Err_Command596_Click


DoCmd.GoToRecord , , acNewRec

Exit_Command596_Click:
Exit Sub

Err_Command596_Click:
MsgBox Err.Description
Resume Exit_Command596_Click

End Sub
_________________________________________________________________

Private Sub NEWSNAPSHOT_Click()
SNAPSHOT2 = Nz(DMax("SNAPSHOT2", "SNAPSHOT"), 0) + 1
End Sub

As you can probably tell, right now I have two different command buttons.
Just for my own purposes, I would like to combine these two separate events
and command buttons into one command button with two different events. Your
help is greatly appreciated. Thank You.
 
Hi Kurtis,

firstly, you should give your controls a good Name BEFORE your wirte
code for them... "Command596" is not very descriptive...

get familar with the PROPERTIES you can set for each control (Name being
the most important one for referencing in code so it should be meaningful)

your first routine is just one statement, to go to a new record, and it
has an error handler. your second routine is also just one statement,
to assign a value to a control.

your second routine looks like it should be called in two places:

1. form BeforeInsert
2. form BeforeUpdate

it should also be expanded to this:

'~~~~~~~~~~~~~~~~~
if me.NewRecord then
SNAPSHOT2 = Nz(DMax("SNAPSHOT2", "SNAPSHOT"), 0) + 1
end if
'~~~~~~~~~~~~~~~~~

you do not want to assign this value if it is not a new record

the BeforeInsert event will cause it to fill as soon as you start typing
the first character on a new record. In the event that you have
multiple users, it will possibly get reassigned the moment before the
record is saved (BeforeUpdate event) so that, if multiple people get the
same number assinged, the chances of multiple numbers being saved is
minimized. There are more complicated ways, but this is the easiest and
mostly safe.

your first routine goes to a new record -- this is sufficient -- if the
user goes to a new record but decides not to fill it out, you do not
want Access to actually make that record by making an assignment



Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 
Hello everyone,
I know this question has probably been asked before, but I just can't seem
to get it right. How do I code a single command button with two entirely
different events. I'm using Access 2002. I don't know if this is the
correct process or not but here are the two different events I would like the
one command button to call:

Private Sub Command596_Click()
On Error GoTo Err_Command596_Click

DoCmd.GoToRecord , , acNewRec

Exit_Command596_Click:
Exit Sub

Err_Command596_Click:
MsgBox Err.Description
Resume Exit_Command596_Click

End Sub
_________________________________________________________________

Private Sub NEWSNAPSHOT_Click()
SNAPSHOT2 = Nz(DMax("SNAPSHOT2", "SNAPSHOT"), 0) + 1
End Sub

As you can probably tell, right now I have two different command buttons.
Just for my own purposes, I would like to combine these two separate events
and command buttons into one command button with two different events. Your
help is greatly appreciated. Thank You.

Which operation did you wish to do first?

Private Sub Command596_Click()
On Error GoTo Err_Command596_Click

DoCmd.GoToRecord , , acNewRec
SNAPSHOT2 = Nz(DMax("SNAPSHOT2", "SNAPSHOT"), 0) + 1

Exit_Command596_Click:
Exit Sub

Err_Command596_Click:
MsgBox Err.Description
Resume Exit_Command596_Click

End Sub
 
you're welcome, Kurtis Lee ;) happy to help

"...I am aware ..."

please forgive me if I give too much information; this newsgroup is read
by thousands who simply study what others have written, so I try to
respond not only to the question, but also to those who may be reading
to learn

Warm Regards,
Crystal
*
(: have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 

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

Back
Top