I’m just wondering if it is possible to create (in Access) a function whose output is the code for another subroutine in the same project,
and then run the resulting code that has just been created? Or am I going up the wrong path here?
I want to do this because I have a number of very similar routines to run in a batch and it would be more extensible and elegant
to generate the code from a table containing the variable strings – call this MasterTable.
That is, 90% of the code body is the same, only varying in lines like:
rstTemps.Open “Some_table”, cnnCurrent, , , adCmdTable
where the actual table “Some_table” varies from one (instance of the) subroutine to the next.
Then, for example, you could just add another line to the table “MasterTable.” and this would increase
the number of subroutines to be run in the batch by one subroutine.
The alternative would be to keep adding more and more subroutines to the module – I’d rather just add
a record to “MasterTable”.
Hope this doesn’t sound too nutty.
I really hope this makes sense!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Generating subroutine code automatically (Office/Access 2000 SP1A)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Generating subroutine code automatically (Office/Access 2000 SP1A)
- This topic has 42 replies, 8 voices, and was last updated 20 years, 11 months ago.
AuthorTopicWSdavidhit
AskWoody LoungerApril 24, 2004 at 2:07 am #404076Viewing 5 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerApril 24, 2004 at 10:26 am #819320 -
WSdavidhit
AskWoody LoungerApril 25, 2004 at 4:56 am #819522Yes, I had thought of that but for some reason was convinced it wouldn’t work.
The idea is to develop a general routine that will copy certain records from one table to another
for a list of pairs of tables. This is where the string argument method might not work as there will be a variable number of lines like:
rstTemps.AddNew
rstTemps![Field1] = rstTemps2.[Field1a]
rstTemps![Field2] = rstTemps2.[Field2a]
rstTemps![Field3] = rstTemps2.[Field3a]
………………….. (actual number of lines here will vary from one routine to the next)rstTemps![Fieldn] = rstTemps2.[Fieldna]
rstTemps.Update
You could do use some sort of do…loop here to iterate through the columns to be copied but I’m not sure how without making it really messy.
-
WSdavidhit
AskWoody LoungerApril 25, 2004 at 4:56 am #819523Yes, I had thought of that but for some reason was convinced it wouldn’t work.
The idea is to develop a general routine that will copy certain records from one table to another
for a list of pairs of tables. This is where the string argument method might not work as there will be a variable number of lines like:
rstTemps.AddNew
rstTemps![Field1] = rstTemps2.[Field1a]
rstTemps![Field2] = rstTemps2.[Field2a]
rstTemps![Field3] = rstTemps2.[Field3a]
………………….. (actual number of lines here will vary from one routine to the next)rstTemps![Fieldn] = rstTemps2.[Fieldna]
rstTemps.Update
You could do use some sort of do…loop here to iterate through the columns to be copied but I’m not sure how without making it really messy.
-
-
Andrew Lockton
AskWoody_MVP -
Andrew Lockton
AskWoody_MVP -
WSjacksonmacd
AskWoody LoungerApril 25, 2004 at 7:11 pm #819593Depending on the number of situations that you need to program (i.e., is it worth the trouble to do this…) I would look at a table-based solution. Two tables would be required in a one-to-many relationship (this is air code):
SituationID
SituationName
SourceTable
DestinationTableSituationID
SourceFieldName
DestinationFieldNameChoose the situation and pass its ID to a subroutine. The subroutine builds an SQL statement that includes all the sourcefields and destination fields for the particular situation. The SQL statement would be an APPEND query from the source table to the destination table. Then execute the SQL query.
-
WSdavidhit
AskWoody Lounger -
WSdavidhit
AskWoody Lounger
-
-
WSjacksonmacd
AskWoody LoungerApril 25, 2004 at 7:11 pm #819594Depending on the number of situations that you need to program (i.e., is it worth the trouble to do this…) I would look at a table-based solution. Two tables would be required in a one-to-many relationship (this is air code):
SituationID
SituationName
SourceTable
DestinationTableSituationID
SourceFieldName
DestinationFieldNameChoose the situation and pass its ID to a subroutine. The subroutine builds an SQL statement that includes all the sourcefields and destination fields for the particular situation. The SQL statement would be an APPEND query from the source table to the destination table. Then execute the SQL query.
-
WSchrisgreaves
AskWoody LoungerApril 26, 2004 at 8:43 am #819745> output is the code for another subroutine in the same project
I think that you are well on your way to a solution for your immediate problem with the tables approach; I prefer table-driven solutions over code-driven solutions because (1) the tables are easier to maintain and (2) they are closer to the user’s level.
That said, the answer to your question (above) ought to be “yes”. I’ve written VBA code that self-modifes itself, and can see no reason why one shouldn’t be able to write self-modifying code, if the occasion warrants it.
-
WSdavidhit
AskWoody LoungerApril 26, 2004 at 9:14 am #819760Hear hear!
Yes I’m going to use Do..Loops and data in tables to construct the INSERT INTO SQL string to transfer the record to the target table.
But I am still interested in dynamically generated subroutines for possible future applications.
I found this link illuminating, although it is centred on Excel, not access, but the approach is clear.
http://www.cpearson.com/excel/vbe.htm%5B/url%5D
Thanks for your thoughts. -
WSdavidhit
AskWoody LoungerApril 26, 2004 at 9:14 am #819761Hear hear!
Yes I’m going to use Do..Loops and data in tables to construct the INSERT INTO SQL string to transfer the record to the target table.
But I am still interested in dynamically generated subroutines for possible future applications.
I found this link illuminating, although it is centred on Excel, not access, but the approach is clear.
http://www.cpearson.com/excel/vbe.htm%5B/url%5D
Thanks for your thoughts. -
WSdavidhit
AskWoody LoungerApril 27, 2004 at 4:50 am #820161I just thought of another possible situation where you might want self modifying code.
With an Access front end and an Access back end – I believe it would be more efficient
in some circumstances to populate various controls and subforms by using ADO to retrieve
only the data necessary for the purpose. So, instead of using linked tables to one or more back end
databases, you could use an ADO call for each table, fully specifying in the connection string where the table is located.
There might be 1,2 or 10 locations, for example.
But, rather than hard code this in, I have created a table of the back end tables in the FRONT end showing the full network path
to each table. This can be easily updated or modified and you can move tables without having to edit the code.
In this case you may need to open an unspecified number of connections, depending on what tables are used in the routine…
cnn1.Open “blah blah”
cnn2. Open “blah2, blah2”
…… etc for x times
Or is there a better way? …URLS? -
WScharlotte
AskWoody LoungerApril 27, 2004 at 12:45 pm #820336Why would you open multiple connections instead of opening a single connection to the database and using a recordset object to return the records? The single connection could be used by however many recordsets you might need or you could do something like this:
Private Sub GetRecords() Dim strSQL As String 'holds SQL for recordset If [cboCustomer].Column(0) "" Then 'create the SQL for the recordset strSQL = "SELECT * FROM tblOrders AS O " _ & "WHERE O.CustomerID ='" & [cboCustomer].Column(0) & "'" 'close the recordset if open On Error Resume Next mrst.Close 'set the recordset properties With mrst .CursorLocation = adUseClient .LockType = adLockBatchOptimistic .CursorType = adOpenKeyset .Source = strSQL .ActiveConnection = mcnn .Open , , , , adCmdText 'get the record count .MoveLast mlngRecCount = .RecordCount .MoveFirst 'populate the # of # display Call SetRecordNum End With 'mrst 'disconnect the recordset Set mrst.ActiveConnection = Nothing 'fill the form controls 'with the data from the first record Call FillRecord Else '[cboCustomer].Column(0) "" MsgBox "Please select a valid customer" End If '[cboCustomer].Column(0) "" End Sub 'GetRecords()
I have a sample app called NoTables that uses that approach if you’re interested.
-
WSdavidhit
AskWoody LoungerApril 28, 2004 at 5:14 am #820661Thanks Charlotte – I will look into your suggestion.
But to answer your question: the reason I might want several connections is that I have a front end which links to tables
in several back end databases. Ideally I would have all my back end tables in the one database on a server computer
(in fact I do have them like this). But the front end is on a computer used in a retail situation and the problem is that, every so often,
the network connection fails for reasons best know to the vagaries of the windows operating system (for the most part).
To prevent this from being an annoying interruption to business, I would then want the front end to work with “mirror” tables
located on the same client computer in the retail area until such time as the network connection is restored. Then I want the
data recorded in the local mirror tables during the blackout period sent to the server tables to bring them back into sync.
This requires some code that registers entries on pairs of similar tables – one on the server and one local table on the client.
For this reason I would need two ADO connections for each operation.
But it occurred to me that one might want to take this further and join tables in an SQL statement that are on different connections
(ie : in different back end databases). Is such a thing possible? (in ado)
Thanks -
WScharlotte
AskWoody LoungerApril 28, 2004 at 12:25 pm #820771I think you’re pushing Access to the bleeding edge of its capabilities … and beyond, so beware. If you have an iffy server connection, ADO notwithstanding, Access is likely to blow up and I don’t think having a “mirror” will save it. I believe it is possible to do cross-database SQL joins using ADO but I haven’t experimented with it and wouldn’t know where to start.
-
WSdavidhit
AskWoody Lounger -
WScharlotte
AskWoody Lounger -
WSHansV
AskWoody Lounger -
WSHansV
AskWoody Lounger -
WScharlotte
AskWoody Lounger -
WSdavidhit
AskWoody Lounger -
WScharlotte
AskWoody LoungerApril 28, 2004 at 12:25 pm #820772I think you’re pushing Access to the bleeding edge of its capabilities … and beyond, so beware. If you have an iffy server connection, ADO notwithstanding, Access is likely to blow up and I don’t think having a “mirror” will save it. I believe it is possible to do cross-database SQL joins using ADO but I haven’t experimented with it and wouldn’t know where to start.
-
WBell
AskWoody_MVPApril 28, 2004 at 4:30 pm #820921I second Charlotte’s comments – Access databases become pretty fragile when you start having network problems. I would be inclined to look at an MSDE solution with either replication or dual commits to a full SQL Server back-end if you really want to make sure the data doesn’t go away. You wouldn’t get to have near as much fun with VBA in that structure, but that might make your front-end simpler. Your description sounds as if the front-end at least is a single user application – am I correct on that?
-
WSdavidhit
AskWoody LoungerApril 28, 2004 at 9:47 pm #821128Yes, the front end is a single user application at present.
I’m thinking of migrating the whole thing to PHP mySQL – I hope this will avoid many potential pitfalls
that the Access approach apparently has in a network environment including the disastrous possibility of an explosion.
Thanks for your comments. -
WBell
AskWoody_MVPApril 29, 2004 at 11:52 am #821335Based on the fact that I can’t get to http://www.mysql.com[/url%5D this morning, you might want to reconsider
. Seriously, it is an alternative, and is the technology behind lots of what happens here in the Lounge. On the other hand, if you develop a web-based solution, you still have to deal with the front-end issues when the network isn’t available. One alternative to consider is to use an Access front-end with ODBC links to a mySQL back-end. I know a couple of developers who use that approach. I’m not an expert in mySQL, but based on what I’ve seen, I don’t believe the replication capabilities and the dual commit features match those of SQL Server or the MSDE. Just more food for thought.
-
WSdavidhit
AskWoody LoungerApril 29, 2004 at 11:00 pm #821668The trouble is, my current website service provider (Netregistry), and most other ISPs here in Australia,
don’t support ASP. So I seem to be compelled to write my future web applications in PHP which I gather
cannot be used with MS SQL server, or isn’t easy to use with it at any rate.
Surely every database management system meant for web use must have some sort of mirroring feature
(like the dual commit for SQL server) as standard? Otherwise you could never really rest easy in case
your entire business went down the virtual plughole. -
WSPeterNRoth
AskWoody LoungerApril 29, 2004 at 11:46 pm #821676Hmm… u might consider http://www.parcom.net[/url%5D as a new host. Great service, economy, features.
-
WSdavidhit
AskWoody Lounger -
WSdavidhit
AskWoody Lounger -
WSPeterNRoth
AskWoody LoungerApril 29, 2004 at 11:46 pm #821677Hmm… u might consider http://www.parcom.net[/url%5D as a new host. Great service, economy, features.
-
WSdavidhit
AskWoody LoungerApril 29, 2004 at 11:00 pm #821669The trouble is, my current website service provider (Netregistry), and most other ISPs here in Australia,
don’t support ASP. So I seem to be compelled to write my future web applications in PHP which I gather
cannot be used with MS SQL server, or isn’t easy to use with it at any rate.
Surely every database management system meant for web use must have some sort of mirroring feature
(like the dual commit for SQL server) as standard? Otherwise you could never really rest easy in case
your entire business went down the virtual plughole. -
WBell
AskWoody_MVPApril 29, 2004 at 11:52 am #821336Based on the fact that I can’t get to http://www.mysql.com[/url%5D this morning, you might want to reconsider
. Seriously, it is an alternative, and is the technology behind lots of what happens here in the Lounge. On the other hand, if you develop a web-based solution, you still have to deal with the front-end issues when the network isn’t available. One alternative to consider is to use an Access front-end with ODBC links to a mySQL back-end. I know a couple of developers who use that approach. I’m not an expert in mySQL, but based on what I’ve seen, I don’t believe the replication capabilities and the dual commit features match those of SQL Server or the MSDE. Just more food for thought.
-
WSdavidhit
AskWoody LoungerApril 28, 2004 at 9:47 pm #821129Yes, the front end is a single user application at present.
I’m thinking of migrating the whole thing to PHP mySQL – I hope this will avoid many potential pitfalls
that the Access approach apparently has in a network environment including the disastrous possibility of an explosion.
Thanks for your comments. -
WBell
AskWoody_MVPApril 28, 2004 at 4:30 pm #820922I second Charlotte’s comments – Access databases become pretty fragile when you start having network problems. I would be inclined to look at an MSDE solution with either replication or dual commits to a full SQL Server back-end if you really want to make sure the data doesn’t go away. You wouldn’t get to have near as much fun with VBA in that structure, but that might make your front-end simpler. Your description sounds as if the front-end at least is a single user application – am I correct on that?
-
WSjacksonmacd
AskWoody LoungerApril 29, 2004 at 12:58 am #821159Another option would be to use local database exclusively, and then replicate to the server. If/when the network connection goes down, the replicaiton is just delayed for a while, but the local application continues to run as normal. Of course, replication is not without its own challenges.
-
WSjacksonmacd
AskWoody LoungerApril 29, 2004 at 12:58 am #821160Another option would be to use local database exclusively, and then replicate to the server. If/when the network connection goes down, the replicaiton is just delayed for a while, but the local application continues to run as normal. Of course, replication is not without its own challenges.
-
-
WSdavidhit
AskWoody LoungerApril 28, 2004 at 5:14 am #820662Thanks Charlotte – I will look into your suggestion.
But to answer your question: the reason I might want several connections is that I have a front end which links to tables
in several back end databases. Ideally I would have all my back end tables in the one database on a server computer
(in fact I do have them like this). But the front end is on a computer used in a retail situation and the problem is that, every so often,
the network connection fails for reasons best know to the vagaries of the windows operating system (for the most part).
To prevent this from being an annoying interruption to business, I would then want the front end to work with “mirror” tables
located on the same client computer in the retail area until such time as the network connection is restored. Then I want the
data recorded in the local mirror tables during the blackout period sent to the server tables to bring them back into sync.
This requires some code that registers entries on pairs of similar tables – one on the server and one local table on the client.
For this reason I would need two ADO connections for each operation.
But it occurred to me that one might want to take this further and join tables in an SQL statement that are on different connections
(ie : in different back end databases). Is such a thing possible? (in ado)
Thanks
-
-
WScharlotte
AskWoody LoungerApril 27, 2004 at 12:45 pm #820337Why would you open multiple connections instead of opening a single connection to the database and using a recordset object to return the records? The single connection could be used by however many recordsets you might need or you could do something like this:
Private Sub GetRecords() Dim strSQL As String 'holds SQL for recordset If [cboCustomer].Column(0) "" Then 'create the SQL for the recordset strSQL = "SELECT * FROM tblOrders AS O " _ & "WHERE O.CustomerID ='" & [cboCustomer].Column(0) & "'" 'close the recordset if open On Error Resume Next mrst.Close 'set the recordset properties With mrst .CursorLocation = adUseClient .LockType = adLockBatchOptimistic .CursorType = adOpenKeyset .Source = strSQL .ActiveConnection = mcnn .Open , , , , adCmdText 'get the record count .MoveLast mlngRecCount = .RecordCount .MoveFirst 'populate the # of # display Call SetRecordNum End With 'mrst 'disconnect the recordset Set mrst.ActiveConnection = Nothing 'fill the form controls 'with the data from the first record Call FillRecord Else '[cboCustomer].Column(0) "" MsgBox "Please select a valid customer" End If '[cboCustomer].Column(0) "" End Sub 'GetRecords()
I have a sample app called NoTables that uses that approach if you’re interested.
-
-
WSdavidhit
AskWoody LoungerApril 27, 2004 at 4:50 am #820162I just thought of another possible situation where you might want self modifying code.
With an Access front end and an Access back end – I believe it would be more efficient
in some circumstances to populate various controls and subforms by using ADO to retrieve
only the data necessary for the purpose. So, instead of using linked tables to one or more back end
databases, you could use an ADO call for each table, fully specifying in the connection string where the table is located.
There might be 1,2 or 10 locations, for example.
But, rather than hard code this in, I have created a table of the back end tables in the FRONT end showing the full network path
to each table. This can be easily updated or modified and you can move tables without having to edit the code.
In this case you may need to open an unspecified number of connections, depending on what tables are used in the routine…
cnn1.Open “blah blah”
cnn2. Open “blah2, blah2”
…… etc for x times
Or is there a better way? …URLS?
-
Viewing 5 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
TotalAV safety warning popup
by
Theodore Nicholson
3 hours, 57 minutes ago -
two pages side by side land scape
by
marc
23 hours, 10 minutes ago -
Deleting obsolete OneNote notebooks
by
afillat
1 day, 1 hour ago -
Word/Outlook 2024 vs Dragon Professional 16
by
Kathy Stevens
4 hours, 7 minutes ago -
Security Essentials or Defender?
by
MalcolmP
6 hours, 52 minutes ago -
April 2025 updates out
by
Susan Bradley
1 hour, 12 minutes ago -
Framework to stop selling some PCs in the US due to new tariffs
by
Alex5723
13 minutes ago -
WARNING about Nvidia driver version 572.83 and 4000/5000 series cards
by
Bob99
7 hours, 20 minutes ago -
Creating an Index in Word 365
by
CWBillow
16 hours, 49 minutes ago -
Coming at Word 365 and Table of Contents
by
CWBillow
8 hours, 18 minutes ago -
Windows 11 Insider Preview Build 22635.5170 (23H2) released to BETA
by
joep517
1 day, 20 hours ago -
Has the Microsoft Account Sharing Problem Been Fixed?
by
jknauth
1 day, 23 hours ago -
W11 24H2 – Susan Bradley
by
G Pickerell
2 days, 1 hour ago -
7 tips to get the most out of Windows 11
by
Alex5723
1 day, 23 hours ago -
Using Office apps with non-Microsoft cloud services
by
Peter Deegan
1 day, 16 hours ago -
I installed Windows 11 24H2
by
Will Fastie
2 hours, 12 minutes ago -
NotifyIcons — Put that System tray to work!
by
Deanna McElveen
2 days, 5 hours ago -
Decisions to be made before moving to Windows 11
by
Susan Bradley
1 minute ago -
Port of Seattle says ransomware breach impacts 90,000 people
by
Nibbled To Death By Ducks
2 days, 13 hours ago -
Looking for personal finance software with budgeting capabilities
by
cellsee6
1 day, 21 hours ago -
ATT/Yahoo Secure Mail Key
by
Lil88reb
1 day, 21 hours ago -
Devices with apps using sprotect.sys driver might stop responding
by
Alex5723
3 days, 6 hours ago -
Neowin – 20 times computers embarrassed themselves with public BSODs and goofups
by
EP
3 days, 14 hours ago -
Slow Down in Windows 10 performance after March 2025 updates ??
by
arbrich
2 days, 17 hours ago -
Mail from certain domains not delivered to my outlook.com address
by
pumphouse
2 days, 23 hours ago -
Is data that is in OneDrive also taking up space on my computer?
by
WShollis1818
3 days, 9 hours ago -
Nvidia just fixed an AMD Linux bug
by
Alex5723
5 days, 1 hour ago -
50 years and counting
by
Susan Bradley
1 day, 23 hours ago -
Fix Bluetooth Device Failed to Delete in Windows Settings
by
Drcard:))
2 days, 2 hours ago -
Licensing and pricing updates for on-premises server products coming July 2025
by
Alex5723
5 days, 12 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.