Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2010, 08:15 AM
Registered User
 
Join Date: Apr 2010
Posts: 10
Thanks: 1
Thanked 1 Time in 1 Post
Question Is it possible to dynamically update OnClick code for Event?

Hi Everyone!
I think I posted this question in the wrong place yesterday and thought maybe it would better to start a new thread.

I am trying to build an Access 2007 form (a menu system) dynamically from a table. The table looks like the Switchboard items table, but instead of having a number that represents the type of event, I have the actual code in a field.

MenuItem - table
MenuID the specific menu, 1, 2, 3, etc
Menu Option - the option number within the menu
Menu desc - the literal that will appear on the form buttons caption
MenuCode - the actual code such as Do.Cmd that needs to run for this button.


I have the program working so far like this.
Open a recordset using a SQL to grab all of the records for a particular menu such as menu 2 which is inventory management.

Then I load all of the buttons that are on that menu form - changing their captions to what is in the table. The buttons are blank until this happens in the code at Form_Load time. All of this works just fine.

The problem - I know how to use VBA to alter the caption property from a value in a table, but not how to build an event from the code that is also strored in the table.

If I were doing this manually, I would go to the OnClick event for the button, click on the elipsis which would open the code editor and present me with an OnClick Sub and End sub for that button. Then I would put the Do.Cmd line of text between them and save it, and the button would then be able to open a form or report.

I know that the OnClick is a property just like Caption is, but cannot find anything in the book on how I would go about programmatically building the menu so that the OnClick events would show up in the Code Editor, coming from a table. Is this even possible?

My code to change the captions is shown below. It needs to be put into a loop and is just procedural at the moment, but I wanted to figure out how to do this before making the code better with error trapping and looping instead of essentially hard coding each menu option which is how it is now.
If you look at the program below, you can see that I am changing the caption property with the field ItemText from the database table. The question is, how would I take a field called MenuCode and make the Do.Cmd OpenForm code get into the code editor using VBA? Right now I coded all of the _Click events for all of the buttons, but I have the text DoCmd.OpenForm "CustMenu" in the table. I hope my description makes sense and that someone with VBA experience would know if this is even possible to do.
Thanks so very very much for reading this. The assignment for school is to just be able to build the menus and take the captions from the table which I was able to do - but I really want to be able to make totally dynamic menus where everything is stored in a table.
Thanks for any help, ideas or suggestions you might have! :)
Best Regards,
Ann Z in CT


Code:
Option Compare Database

Private Sub btn1_Click()
DoCmd.OpenForm "CustMenu"
End Sub

Private Sub btn2_Click()
DoCmd.OpenForm "InvMenu"
End Sub

Private Sub btn3_Click()
DoCmd.OpenForm "OrderMenu"
End Sub

Private Sub btn4_Click()
DoCmd.OpenForm "ReportsMenu"
End Sub

Private Sub btn5_Click()
DoCmd.Close acForm, "MainMenu", acSaveYes
End Sub

Private Sub Form_Load()
Dim conn1 As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim strSQL As String
    
  ' Format the strSQL string with the fields that I want and the user selected search field as the customer name to search for.
    strSQL = "SELECT * FROM MenuItems WHERE MenuID=1 Order By ItemNumber"
    
  ' Start the connection and process the SQL query on the recordset.
    Set conn1 = CurrentProject.Connection
    
    Set rs = New ADODB.Recordset
    rs.Open strSQL, conn1
    rs.MoveFirst   ' new code added
    btn1.Caption = rs![ItemText]
    rs.MoveNext
    btn2.Caption = rs![ItemText]
    rs.MoveNext
    btn3.Caption = rs![ItemText]
    rs.MoveNext
    btn4.Caption = rs![ItemText]
    rs.MoveNext
    btn5.Caption = rs![ItemText]
End Sub

Last edited by AnninCT; April 16th, 2010 at 08:24 AM.. Reason: added thank you at end.
 
Old April 16th, 2010, 08:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there..

You can attach an event to a new control using object.event = addressof(procedure name) if my heads doesn't fail.

But you can´t (at least not that I remember) build code on the fly...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following User Says Thank You to gbianchi For This Useful Post:
AnninCT (April 16th, 2010)
 
Old April 16th, 2010, 03:24 PM
Registered User
 
Join Date: Apr 2010
Posts: 10
Thanks: 1
Thanked 1 Time in 1 Post
Default Cant build on the fly

Thanks gbianchi very much for taking a look at this. I have been searching for days to find a solution but nothing popped out as being a possible solution to do these on the fly.
Take care and have a great weekend!
Ann





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT - onclick event Condor76 XSLT 1 June 1st, 2007 04:53 PM
changing the onclick property dynamically? nerssi Javascript 8 September 21st, 2004 12:59 AM
onclick event bjackman Access 6 July 15th, 2004 06:54 AM
onclick event pigtail Javascript 1 April 11th, 2004 03:10 PM
onClick Event mateenmohd Javascript 4 December 16th, 2003 01:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.