Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2024, 02:12 PM
capitala capitala is offline How to make dynamic function? Windows 7 64bit How to make dynamic function? Office 2003
Advanced Beginner
How to make dynamic function?
 
Join Date: Nov 2013
Posts: 34
capitala is on a distinguished road
Default How to make dynamic function?

Good day!


I'm using this macro:

dim db as dao.database
dim rst as dao.recordset
set db = MyDB
set rst=Mydb.OpenRecordset("MyRst")
...........et.c
I frequently use this style but with different recordsets.
Is it possible to create only one generic Sub and then pass certain values from other Sub?
Reply With Quote
  #2  
Old 04-12-2024, 06:42 PM
Guessed's Avatar
Guessed Guessed is offline How to make dynamic function? Windows 10 How to make dynamic function? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,980
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Yes, it is possible. You need to work out what the variables are and then create a function with those variables as inputs and return the result. You will find lots of example functions on this forum.

If you need help with specific code, post all of that code and we can make suggestions on how you might do it.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-12-2024, 07:41 PM
capitala capitala is offline How to make dynamic function? Windows 7 64bit How to make dynamic function? Office 2003
Advanced Beginner
How to make dynamic function?
 
Join Date: Nov 2013
Posts: 34
capitala is on a distinguished road
Default

Dear Guessed,
Thanks for your reply. Below a sample of my requirements (with thanks):
dim db as dao.database
dim rst as dao.recordset
set db = MYDB
set rst=Mydb.OpenRecordset("MYRST")
rst.index = "INDEX"
if not rst.nomatch then
rst.edit
rst.FEILD1 = "abc"
rst.FEILD2="xyz"
rst.update

All those in CAPS are the variables that change from one Sub to another

Thanks again
Reply With Quote
  #4  
Old 04-13-2024, 09:06 PM
Guessed's Avatar
Guessed Guessed is offline How to make dynamic function? Windows 10 How to make dynamic function? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,980
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The point of using functions is that you can modularise a chunk of the code so you don't need to repeat that chunk inside your Sub or when creating new Subs. This doesn't save you from needing the code at all - it just allows you to be slightly more efficient in creating code.

It might make sense to create a function to return a Recordset. But what you choose to do with the recordset should probably be in the Sub itself since it will vary in a range of ways with each macro.

But the capability of that recordset function won't be a 'one size fits all'. For instance, what format is the database you are querying? Is it Excel, Access, ODBC etc. What is the filepath to that database? What is the name of the table inside that database that you want? Are you also passing a SQL string to specify which fields and records to include in the returned recordset. Is the answer to all these questions always the same? If not, the function will need those key bits of information in order to return the recordset you are expecting.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 04-13-2024, 11:17 PM
capitala capitala is offline How to make dynamic function? Windows 7 64bit How to make dynamic function? Office 2003
Advanced Beginner
How to make dynamic function?
 
Join Date: Nov 2013
Posts: 34
capitala is on a distinguished road
Default

Thanks dear
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the Filter function to dynamic search large table le_robert Excel 3 06-27-2022 01:06 PM
Hi, I wonder if I can make a dynamic document to speed up the creation of document parisfranco Word 10 03-10-2022 01:20 PM
How to make dynamic function? How to Make a Dynamic URL Clickable? DKY Mail Merge 1 03-29-2020 04:43 PM
How to make dynamic function? Restrict Editing function disable insert textbox function IanM_01 Word 5 11-21-2015 02:29 AM
How to make dynamic function? #REF! Error in calling VBA function disappears when function is copied lcaretto Excel Programming 2 05-26-2014 07:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft