Command Button (1 Viewer)

kitty77

Registered User.
Local time
Yesterday, 22:20
Joined
May 27, 2019
Messages
712
What is the vba code for: afterupdate, goto a command button and click it?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:20
Joined
Oct 29, 2018
Messages
21,473
Just call the click event. For example:

ButtonName_Click
 

kitty77

Registered User.
Local time
Yesterday, 22:20
Joined
May 27, 2019
Messages
712
When I click the button manually, it works. It opens another form. When I use Command5_Click I get an error.
 

kitty77

Registered User.
Local time
Yesterday, 22:20
Joined
May 27, 2019
Messages
712
1645560384629.png
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:20
Joined
Oct 29, 2018
Messages
21,473
When I click the button manually, it works. It opens another form. When I use Command5_Click I get an error.
Can you show us the code in the Click event of the button please? Thanks.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:20
Joined
Feb 28, 2001
Messages
27,185
Since there is no parameter inherent in a Click event try this:

Call Command5_Click

Then put a breakpoint on that line of code. When it breaks, single-step (using Step-Into, which you can find on the VBA code-page menu). One line at a time see what it does.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 03:20
Joined
Jul 9, 2003
Messages
16,282
Just call the click event.

Not considered good practice.

It's better it you put the command button code in its own routine and call that from the command button and elsewhere....
 

kitty77

Registered User.
Local time
Yesterday, 22:20
Joined
May 27, 2019
Messages
712
Here is what's in the command button.
1645569525943.png
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:20
Joined
Oct 29, 2018
Messages
21,473
Here is what's in the command button.
View attachment 98482
Ah, that's an embedded macro. Not sure you can execute it another way aside from clicking on the button. If you must use macros, you might use a named macro instead. Otherwise, you could repeat/duplicate the macro in your other code. Just a thought...
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:20
Joined
Feb 19, 2002
Messages
43,275
I think it probably depends on which event you want to call whether or not it is dangerous. I also tend to make procedures when I want to reuse code just for neatness and to make it clear that the code is being reused.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 03:20
Joined
Jul 9, 2003
Messages
16,282

It surprises me how many times I see this done. I was taught it was bad practice in my early days of MS Access programming, by my mentors. I didn't rightly know why at the time, but as you start to develop more sophisticated code you start to see the sense in it.

If the code is only applicable to the command button, then leave it there. If you need to call it from elsewhere, then you should refactor it into its own method.

Code called from more than one place should not be tied to a particular control. If you rename your command button, the code that is calling the command button event handler will fail.

If you update the code within the command button event handler, you might not be aware that it is being called from elsewhere and introduce a bug.

It's better coding style to have a standalone procedure with a name that indicates what it does.

It's a more elegant solution and is much easier to maintain.

It makes it easy to reuse the code when it is separate from the command button event handler. It is also easier to test and maintain.
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:20
Joined
Oct 29, 2018
Messages
21,473
Given that the OP posted the code behind the button in question, does anybody still think it belongs in a Standard Module? Just curious...
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 03:20
Joined
Jul 9, 2003
Messages
16,282
does anybody still think it belongs in a Standard Module? Just curious...

Ha! Its a macro!

It should immediately be converted into VBA and the perpetrator of the offence should be banned from (AWF) Access World Forums!
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:20
Joined
Oct 29, 2018
Messages
21,473
Ha! Its a macro!

It should immediately be converted into VBA and the perpetrator of the offence should be banned from (AWF) Access World Forums!
But I was referring to its content (the code inside the macro). Even if you convert it into VBA, do you really think it should be in a Standard Module. I was just curious...
 

isladogs

MVP / VIP
Local time
Today, 03:20
Joined
Jan 14, 2017
Messages
18,221
I totally agree that code repeated more than once should be transferred to a separate procedure, either within the form module or, where appropriate, in a standard module. I do that all the time for exactly the reasons Tony described.

However, in this case where the button click code is simple and used in just one other place, I see no reason to create a separate procedure just for the sake of it. However, I would definitely replace that embedded macro with VBA
 

Users who are viewing this thread

Top Bottom