Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can a procedure be written to automate adding new code?

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I have a "Locations" module that contains procedures for individual locations. When ever the company adds a new site, I have to copy/paste an existing procedure in the "Locations" module. I then change the location_id in the procedure to process the new location and rename the procedure to the new location.

It will always be the exact same code with the exception of the location_id. Can this be automated??? Could a procedure be written that the user passes in a variable(Location_ID) and the program writes it's own code???

Just curious...
 
First of all - why not have one main sub with one or more parameters - the location name and ID, for example, which handles all of the main processing? Each individual location sub would simply call the main sub. Exactly how to automate those individual subs depends on what triggers the sub (a command button? a menu item? a user form?). You may not need a separate sub for each location. But if you do, it's easy enough to programmatically add code (even though I usually shy away from it). If you decrease the amount of code to be added by going to the main sub approach I suggest above, it will make things easier.
Give this a look:

thisworkbook.VBProject.VBComponents("Module1").CodeModule.AddFromString _
"Sub NewLoc()" & vbcrlf & " MainLoc ""Indiana"",""IN""" & vbcrlf & "end sub"

Rob
[flowerface]
 
Rob:

Not sure what I was thinking... I am in the process of re-writing the code to use a generic function that I pass variables into from a Case Select.

Then I only add a new Case Select criteria when adding a new location vs. a new procedure.

Thanks for the wake up call.
 
Not meaning to insult you by stating the obious, but I would make sure to use Case Else to pop a message box reminding me if a new one is being added and I missed it.

Good Luck!
 
SBendBuckeye:

Already have it in... thanks for the reminder.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top