473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to pass Values from one function to another in same Module

78
Hello all,

Due to the size restriction of VBA code length I am forced to seperate my code accross Functions and call to them.

The setup is simple I have a private function that runs...Within that function, based on a recordcount # it then calls a separate function.

The problem I have is that it uses values detemined in the private function to run code is seperate sub.I dont want to reset the values but instead carry them over to the sub it calls.

For instances:
Private Sub ()
vDate = now()
Employee = current employee
Call ShiftRequest

(now as the code runs these values change based on certain criteria)

I would like to have something like this happen:
Public Sub ShiftRequest()
vDate = (private sub value of vDate when called)
Employee = (private sub value of Employee when called)

Can anyone help with how this is done. Or for that matter if it can be done.

Please let me know is this is not clear.

Thank you in advance.

Nick
Jun 9 '08 #1
11 27807
Stewart Ross
2,545 Expert Mod 2GB
Hi Nick. There are several ways to carry over values from one procedure call to the next, and which of these you use will depend on the scope in which you want to be able to access the values.

If you wish to carry forward the last value of a variable within a specific Sub or Function you can do so by declaring that variable as Static (instead of using the usual DIM form). Using somevar to stand for the name of a variable, and sometype to stand for its type, a static declaration is just:
Expand|Select|Wrap|Line Numbers
  1. Static somevar as sometype
You will not be able to access the values outside of the scope of the Sub or Function concerned, but on each call you will be able to refer to the previous value of the variable without passing its value as a parameter - the Static keyword means that its last value is retained and not destroyed on exit from the procedure.

Alternatively, if you need to retain values and refer to them outside of the scope of individual procedures you can define global variables in a public code module and refer to these from any other code module in your project. Such variables are declared in the header of a public code module as follows:
Expand|Select|Wrap|Line Numbers
  1. Public somevar as sometype
If you need to encapsulate a number of variables along with procedures to set and reset those properties you could create a class module to do so, but it sounds like this would be overkill for what you need at present.

Finally, if you need to retain data values between user sessions of your database you can use a dedicated table to do so. Access itself frequently uses system tables (not directly visible to users) to capture not just table and query design-related data but state and other data which has to be retained from one session to the next.

-Stewart
Jun 9 '08 #2
nspader
78
That was it. I had tried that. However, I was in a procedure when I named it public. I didnt think about it having to be in the module but not in a procedure. Thank you for your help as always.

Much appreciated

Nick
Jun 13 '08 #3
missinglinq
3,532 Expert 2GB
I'm curious about your statement " Due to the size restriction of VBA code length." What exactly are you referring to here?

Linq ;0)>
Jun 13 '08 #4
ADezii
8,834 Expert 8TB
In my opinion, the most efficient mechanism for passing Variables between Routines, whether they be Function or Sub, are through the receiving Routine's Arguments. The following code will illustrate this point:
  1. Initial Sub-Routine
    Expand|Select|Wrap|Line Numbers
    1. Private Sub TestSub()
    2. 'Declare Variables to be passed to ShiftRequest()
    3. Dim dteDate As Date
    4. Dim strEmployee As String
    5.  
    6. dteDate = Now()
    7. strEmployee = CurrentUser()
    8.  
    9. 'Pass the Calculated Vales in TestSub() to ShiftRequest
    10. 'via its Arguments
    11. Call ShiftRequest(dteDate, strEmployee)
    12. End Sub
  2. Receiving Routine, accepts and processes values passed to it as Arguments from TestSub()
    Expand|Select|Wrap|Line Numbers
    1. Public Sub ShiftRequest(dteTheDate As Date, strCurrentEmployee As String)
    2.   Debug.Print "Date passed to ShiftRequest: " & dteTheDate & vbCrLf & _
    3.               "Current Employee Name passed to ShiftRequest(): " & strCurrentEmployee
    4. End Sub
  3. Sample OUTPUT
    Expand|Select|Wrap|Line Numbers
    1. Date passed to ShiftRequest: 6/13/2008 9:02:06 AM
    2. Current Employee Name passed to ShiftRequest(): Admin
  4. Any questions, please feel free to ask.
Jun 13 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
Adezii is right, Nick - best practice is to ensure that all values passed between routines are explicitly passed as arguments (parameters) of the procedure or function involved. The use of global variables can introduce side effects that can cause failures as a result of interactions which are not clear to the users.

It is good software engineering practice to treat modular code as 'black boxes' which interact solely through their interfaces - the calling statement and the arguments passed. The workings of the inner code should make no difference to anything except what is passed through the arguments or specified to be the output of the function concerned.

In designing an object class globals are used for the object and its state variables - but in designing the class itself these internal variables are not normally made available to the 'outside world' except through interface routines (property set and get, and the methods for the class).

-Stewart
Jun 13 '08 #6
ADezii
8,834 Expert 8TB
nspader, one significant point to mention here is Load on Demand, a VBA concept whereby Modules are dynamically loaded into memory only when a component of them is referenced, i.e. Function, Sub-Routine, Variable, etc. If you have a situation in which Function1 calls Function2 and Function2 references a Global Variable, contain both Functions and the Global Declaration within a single Module so only one Module is loaded at Run Time. If you have cascading Function/Sub Calls, and these Routines are in different Modules, all those Modules will be loaded into memory. Bottom line, keep all related code/logic contained within single Modules!
Jun 13 '08 #7
nspader
78
nspader, one significant point to mention here is Load on Demand, a VBA concept whereby Modules are dynamically loaded into memory only when a component of them is referenced, i.e. Function, Sub-Routine, Variable, etc. If you have a situation in which Function1 calls Function2 and Function2 references a Global Variable, contain both Functions and the Global Declaration within a single Module so only one Module is loaded at Run Time. If you have cascading Function/Sub Calls, and these Routines are in different Modules, all those Modules will be loaded into memory. Bottom line, keep all related code/logic contained within single Modules!
Thank you for your advise. I did know about containing them into one module to keep dynamic memory to a minimum. Which they are.

My comment about being too long is that a single function in Access 2000 (mine atleast) cannot contain more the 1700 lines of code. I was running over this with the single function. It is very long and calls multiple variables. It only made sense to break it up into different functions to call anyway, I just didnt know how to pass the variables. Depending on where it is in the code loop will determine certain values for the variables.

One question I do have is why the different name of passing variable? i.e. Call Shiftrequest1 (acDate, acEmployee)

Private function Shiftrequest1(acTheDate, acCurrentEmployee)<---- Can you explain how to know what to write?

Thank You for all your help

Nick
Jun 16 '08 #8
ADezii
8,834 Expert 8TB
Thank you for your advise. I did know about containing them into one module to keep dynamic memory to a minimum. Which they are.

My comment about being too long is that a single function in Access 2000 (mine atleast) cannot contain more the 1700 lines of code. I was running over this with the single function. It is very long and calls multiple variables. It only made sense to break it up into different functions to call anyway, I just didnt know how to pass the variables. Depending on where it is in the code loop will determine certain values for the variables.

One question I do have is why the different name of passing variable? i.e. Call Shiftrequest1 (acDate, acEmployee)

Private function Shiftrequest1(acTheDate, acCurrentEmployee)<---- Can you explain how to know what to write?

Thank You for all your help

Nick
Hello Nick, the names within the Fuction/Sub Procedure are not important since they are just 'placeholders' for the actual Values being passed. What is important is that the values being passed are in the right order (unless you are using Named Arguments) and are of the correct Data Type. If a Function accepts a Date, Integer, and String, be sure to pass a Date, Integer, then String to it. Hope this helps. BTW, a single Function should 'never' contain anywhere near 1,700 lines of code.
Jun 16 '08 #9
nspader
78
Hello Nick, the names within the Fuction/Sub Procedure are not important since they are just 'placeholders' for the actual Values being passed. What is important is that the values being passed are in the right order (unless you are using Named Arguments) and are of the correct Data Type. If a Function accepts a Date, Integer, and String, be sure to pass a Date, Integer, then String to it. Hope this helps. BTW, a single Function should 'never' contain anywhere near 1,700 lines of code.
I know it shouldnt. However with my limited knowledge I have to perform things the way I know how. Unfortunately it is very long winded Tiering If Then statements. I am trying to learn case statements in hopes this will greatly shorten it. Also, The large amount of variables included to run this code makes it extremely long.

Thank you for your help.

Nick
Jun 16 '08 #10
nspader
78
Adezii,

Also, When passing to a called function, When that function ends and it returns to the original function to run will it retain the new value from the called function back to the original function?

I hoep that is clear. Basically it is a loop in values used to make a rotating shift schedule. The vEmployee number changes continually as the code runs. I need to retain last value from any given function to use for the next, until the Original function is completed. Will this still work that way. I know it did in Public code setup. I am adjusting the code to your recommendation as it does appear that it will be more properly written that way.

I just want to make sure that the value will go back and forth the way you stated to run.

Thanks

Nick
Jun 16 '08 #11
ADezii
8,834 Expert 8TB
Adezii,

Also, When passing to a called function, When that function ends and it returns to the original function to run will it retain the new value from the called function back to the original function?

I hoep that is clear. Basically it is a loop in values used to make a rotating shift schedule. The vEmployee number changes continually as the code runs. I need to retain last value from any given function to use for the next, until the Original function is completed. Will this still work that way. I know it did in Public code setup. I am adjusting the code to your recommendation as it does appear that it will be more properly written that way.

I just want to make sure that the value will go back and forth the way you stated to run.

Thanks

Nick
I think a little example will make things clearer. Calling fIntroFunction() will yield the following output:
Expand|Select|Wrap|Line Numbers
  1. Public Function fIntroFunction()
  2. Dim lngLength As Long
  3. Dim lngWidth As Long
  4. Dim lngHeight As Long
  5.  
  6. lngLength = 150
  7. lngWidth = 75
  8. lngHeight = 50
  9.  
  10. Debug.Print "Volume of space: " & FormatNumber(fCalculateVolume(lngLength, lngWidth, lngHeight), 0) & _
  11.             " Cubic Feet"
  12. End Function
Expand|Select|Wrap|Line Numbers
  1. Private Function fCalculateVolume(lngLen, lngWdth, lngHgt) As Long
  2.   fCalculateVolume = (lngLen * lngWdth * lngHgt)
  3. End Function
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Volume of space: 562,500 Cubic Feet
NOTE: fCalculateVolume() can be declared Privately since it exists in the same Module as fIntroFunction().
Jun 17 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steven Bethard | last post by:
I'd like to merge a module written in C with a module written in Python so that the user can access objects from either module by importing the single, merged module. Specifically, I'm looking...
2
by: Martin Tully | last post by:
I am trying to a create a page that will query a SQLDB get customer information and then pass this information on to another page creating almost like a mail merge letter. I can get the information...
4
by: Sullivan WxPyQtKinter | last post by:
In python, these expression seems yields the same result: inputstring='ABC' print inputstring.lower() print lower(inputstring) print string.lower(inputstring) result: abc
3
by: drec | last post by:
I am creating a search box that the user types a value in, and then this gets passed to another page called search.php I would like to be able to pass these values through the URL, but I cant...
6
by: kath | last post by:
hi everyone......... I have a task, I have fragmented the task into subtask. I have planned to create a class to each subclass and one parent class to handle the sub tasks. Each subclass are...
5
by: codercoder | last post by:
Hello Helpers, I have a question about passing values through multiple C++ functions: Function A calls function B; Function B calls function C; Function C calls function D; D has two values...
8
by: laredotornado | last post by:
Hi, I want to pass my function myFunc('a', 'b', 'c') as an argument to another function. However, this will not work doStuff('x', 'y', myFunc('a', 'b', 'c'))
2
by: Prashant P | last post by:
help in asp.net to pass values from a page to a frame inside the same page -------------------------------------------------------------------------------- help in asp.net to pass values from a...
15
by: J. Peng | last post by:
Hello, I saw this statement in Core Python Programming book, All arguments of function calls are made by reference, meaning that any changes to these parameters within the function affect the...
1
by: gopim | last post by:
in my registration page having around 20 fields,it is very difficult for users. i want to devide this registratino for into two sections.but my doubt is how can we store first section values with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.