Sie sind auf Seite 1von 2

Behind the scenes How is it done The Switchboard Manager in Access is a useful tool and you do not need

to know a nything about it in order to be able to build your own system of switchboards. I t does however show up some useful techniques in Access and in VBA program code. If you know how the system works then you can modify it to extend its capabilit ies and perhaps use some of these programming techniques elsewhere. Switchboard table The system is based on a table and a form, both named "Switchboard". The table d efines the structure of the switchboards, the form defines their appearance. If you open the Switchboard table you'll see that it has a simple structure: SwitchboardID ItemNumber ItemText Command Argument 1 0 Main Switchboard 0 Default 1 1 Clients 3 fClients 1 2 Enquiries 3 fEnquiry 1 3 People 3 fPeople 1 4 Reports 3 fInvoice 1 5 Exit 6 This table has five columns: SwitchboardID is the same for all rows because this system only has one swit chboard and all the entries belong to that switchboard. More complex systems can have subsidiary switchboards with entries of their own. ItemNumber is the row number of this button on the switchboard. Together wit h the SwitchboardID it forms the primary key for the table. You can rearrange th e order of the buttons on the form if you alter these values. The first row has an ItemNumber of zero because it is the switchboard itself. ItemText is the text that will appear alongside the button. For a switchboar d it is the caption of the form. Command is a code that identifies one of the commands available. This sample shows that 3 must mean "Open FOrm" and 6 must mean "Exit Application". The othe rs are documented in the VBA code of function HandleButtonClick of the Switchboa rd form. Argument is the name of the form, report or switchboard to run. The final ro w has no argument because the command "Exit Application" does not need any more information. Switchboard form The key feature on the switchboard form is the column of eight buttons. VBA func tions on the form determine the appearance of these buttons at runtime and respo nd to the users' clicking the buttons. [Default appearance of the Switchboard Manager in Access XP.] Make the buttons visible When the form loads, the FillOptions method creates a recordset from the Switchb oard table and loops through it to decide which buttons should be displayed: For intOption = 2 To conNumButtons Me("Option" & intOption).Visible = False Me("OptionLabel" & intOption).Visible = False Next intOption The loop starts at 2 because the first button must always be visible. A second l oop then sets the caption associated with each button by reading the ItemText fi eld from the recordset: Me("OptionLabel" & rst![ItemNumber]).Caption _ = rst![ItemText]

Deciding what action to take Each button calls the HandleButtonClick method in its Click event and each passe s its identifying number into that function. Inside the function there's some VB A code using DAO to find the record in the Switchboard table: rst.FindFirst "[SwitchboardID]=" & _ Me![SwitchboardID] & " AND [ItemNumber]=" & intBtn It retrieves the contents of the Command field from that row and uses that numbe r in a Case structure: ... ' Open a form in Add mode. Case conCmdOpenFormAdd DoCmd.OpenForm rst![Argument], , , , acAdd ' Open a form. Case conCmdOpenFormBrowse DoCmd.OpenForm rst![Argument] ' Open a report. Case conCmdOpenReport DoCmd.OpenReport rst![Argument], acPreview ... In each of these cases, the Argument holds the name of a form or a report in the database and the methods of the DoCmd object open the selected form or run the selected report as appropriate. The constants such as conCmdOpenFormAdd are all defined at the top of the Handle ButtonClick function. If you need to extend the range of actions available to th e switchboard you could add a new constant, for example one to run an export int o an Excel spreadsheet: Const conCmdExportToXL = 9 and extend the VBA case structure with a call to DoCmd.TransferSpreadsheet(...). Final warning I can't guarantee that anything I've said on this page will work with your datab ase.The usual warnings apply if you decide to bypass any wizard: Make sure you've got a good backup before you start. Read the details in Help. Make small changes and test often.

Das könnte Ihnen auch gefallen