PDA

View Full Version : Custom / own vba code in toolbar button



raytrace
03-08-2010, 11:36 AM
Hi. Need a little help on this (probably novice) issue.

How can i create a toolbar button with my own code. Also, can i covert the buttons i have already created on a form as toolbar buttons?

Imdabaum
03-16-2010, 09:40 AM
What version of Access are you using? I don't know if you can convert the buttons you already have, but if your button is accessing a sub or function then there are ways that you can tie the sub/function to a menu item.

How you do it just depends on whether you're using 2003 custom toolbar, or 2007 custom ribbon.

raytrace
03-17-2010, 12:15 AM
Thank s for the reply.

I am using Access 2003.
Actually what I want to do is to create a toolbar icon on a new toolbar and give it my own vba code.

The reason I asked about the conversion of the buttons to toolbar icons is that I started the form with buttons but eventually warmed up to the the idea of the customized toolbars.

Imdabaum
03-17-2010, 07:43 AM
http://office.microsoft.com/en-us/access/HP051890051033.aspx

That's a pretty vague link to creating your own custom toolbar. Once you have that you'll want to go to the properties and you can set the button to activate a function or sub that you want executed. or you can use the built in functions OpenForm, OpenReport, RunMacro etc and just include the name of the object you want to pass in.

I wish I could give you more detailed instructions. I have 2007 at work and am running 2010 at home. Haven't used 2003 for about a year and a half and I have a poor memory. But if you're talking about converting a previous switchboard that opened forms and reports for you, then it's as simple as putting the same function/sub in the event properties for the toolbar button.

raytrace
03-17-2010, 11:58 PM
Thanks for your reply and the link.

I assumed that there would be a simple method of adding a command button to the your customized toolbar, as it seems only natural since they both are functionally the same thing.

I have reached the point where I can put a module function into a command menu item's control property. But for that I would have to convert all my form-based code to module bases code. I guess that is not so bad if I want to make generic and reusable buttons. In my case, however, I would like to create customized toolbars to suite the requirement of particulars forms.

Do you think the above is a good idea in the light of "good design" ?

Imdabaum
03-18-2010, 10:01 AM
2007 tends to automatically insert macros for the buttons(at least for simple actions) I've modified those macros to run the easy stuff and any extra code I want run on the button click has been put into a module where I can just call the function.

From a design standpoint it's generally bad to duplicate code. If it's duplicated it should be stored in a location where everything that needs it can access it. From a "does it work" standpoint, what are you goals with the project? Is your boss going to fire you for poor design or fire you for having a very small file that does nothing?

Sometimes life is a juggling act. I would opt for better design unless my job is on the line. Once I get it working then I might consider rehashing the code to simplify it.

Sorry if that isn't the best help. But it largely might depend on what you are trying to do with the command bar.

Are you trying to open forms or perform some complicated routine that you have built into several different buttons on several different forms?

raytrace
03-19-2010, 01:00 AM
>>Are you trying to open forms or perform some complicated routine that you have built into several different buttons on several different forms?

For most of the part, I am trying to perform conditional checks before opening a linked report or allow/restrict additions, edits and deletion etc. I know it is possible to that do with macros, to an extent, but I feel easier coding it out. That way I can control the form behavior better.

So, coming back to my initial query; how to simply put (non class-module) code in a customized tool bar button. I have learned how to add the code to a button but it can be only (i think) a function which has been defined in the modules.

What I still am not able to do is
1) Put a private sub code behind a tool bar button.
2) Covert my command buttons to a customized tool bar button.

So, still working on that...