473,386 Members | 1,698 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,386 software developers and data experts.

Code design Theory

Okay this may be a bit trivial, it is a concept Im having difficulty understanding. When creating VB code I like to break up job steps/functions into seperate modules for example;

01Globals
02Cleardatatables
03 CreateDateString
04 etc..etc

The module 01Globals list all of the variables used by every module and the code there calls each module in sequence;
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. 'Global Variables
  4.     Global gstrFileName As String
  5.     Global gstrFNDate As String
  6.     Global gstrDate As String
  7.     Global gstrFilePath As String
  8.     'Global gstrDate As String
  9.  
  10.  
  11. Public Sub Start()
  12.  
  13.     Dim dbs As Database
  14.  
  15.     Set dbs = CurrentDb
  16.  
  17.  
  18.     ClearDataTables '02ClearDataTables
  19.  
  20.  
  21.     DateString '03CreateDateString
  22.  
  23.  
  24.  
  25.  
  26.     gstrFilePath  "T:\Automation\01PkWood\KaladaData\AutomatedAdjustments_" & gstrDate & ".xls"
  27.     gstrFileName = "AdjClaimIn_" & gstrDate
  28.  
  29.     Debug.Print "file pathe = " & gstrFilePath
  30.     Debug.Print "file Name = " & gstrFileName
  31.  
  32.     ImportdataPull '04ImpotDataPull
  33.  
  34.     CreatePtrackinput '05CreatePtrackInput
  35.  
  36.     ExportTables '06ExportTables
  37.  
  38. End Sub
This is my question: Do I have to include the following in each additional module;

Pseudo code
Expand|Select|Wrap|Line Numbers
  1. Public Sub "Sub Name"()
  2.  
  3.     Dim dbs As Database
  4.  
  5.     Set dbs = CurrentDb
Once the workspace is created buy the initial module is it available to all of the other modules? What I've read to date isnt very clear and the few chance I've had to experiment have only added to my confusion.
Jan 2 '08 #1
11 1698
FishVal
2,653 Expert 2GB

Pseudo code

Public Sub "Sub Name"()

Dim dbs As Database

Set dbs = CurrentDb

Once the workspace is created buy the initial module is it available to all of the other modules? What I've read to date isnt very clear and the few chance I've had to experiment have only added to my confusion.
Hi, there.

What is this code supposed to do? And what it does in the posted subroutine?

Regards,
Fish
Jan 2 '08 #2
NeoPa
32,556 Expert Mod 16PB
In short - No.
You need to define dbs as a global variable (the recommended method is to use Public rather than the older Global) outside of a particular procedure.
With it defined that way you will only need to set it once for it to be available to your whole project.
Jan 3 '08 #3
Thanks NeoPa I appreciate you taking the time to answer such a simple question.

B
Jan 7 '08 #4
NeoPa
32,556 Expert Mod 16PB
Hey, no worries.
It's what I do after all :)
Jan 7 '08 #5
ADezii
8,834 Expert 8TB
Okay this may be a bit trivial, it is a concept Im having difficulty understanding. When creating VB code I like to break up job steps/functions into seperate modules for example;

01Globals
02Cleardatatables
03 CreateDateString
04 etc..etc

The module 01Globals list all of the variables used by every module and the code there calls each module in sequence;
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. 'Global Variables
  4.     Global gstrFileName As String
  5.     Global gstrFNDate As String
  6.     Global gstrDate As String
  7.     Global gstrFilePath As String
  8.     'Global gstrDate As String
  9.  
  10.  
  11. Public Sub Start()
  12.  
  13.     Dim dbs As Database
  14.  
  15.     Set dbs = CurrentDb
  16.  
  17.  
  18.     ClearDataTables '02ClearDataTables
  19.  
  20.  
  21.     DateString '03CreateDateString
  22.  
  23.  
  24.  
  25.  
  26.     gstrFilePath  "T:\Automation\01PkWood\KaladaData\AutomatedAdjustments_" & gstrDate & ".xls"
  27.     gstrFileName = "AdjClaimIn_" & gstrDate
  28.  
  29.     Debug.Print "file pathe = " & gstrFilePath
  30.     Debug.Print "file Name = " & gstrFileName
  31.  
  32.     ImportdataPull '04ImpotDataPull
  33.  
  34.     CreatePtrackinput '05CreatePtrackInput
  35.  
  36.     ExportTables '06ExportTables
  37.  
  38. End Sub
This is my question: Do I have to include the following in each additional module;

Pseudo code
Expand|Select|Wrap|Line Numbers
  1. Public Sub "Sub Name"()
  2.  
  3.     Dim dbs As Database
  4.  
  5.     Set dbs = CurrentDb
Once the workspace is created buy the initial module is it available to all of the other modules? What I've read to date isnt very clear and the few chance I've had to experiment have only added to my confusion.
Here are a couple somewhat related pointers that I think will help you out:
  1. VBA loads code only when it is needed, at execution time.
  2. VBA loads only the particular Module it needs at any given moment to run your code.
  3. VBA always loads an entire Module if it needs any portion of the Module or if it must use any Procedure in a Module.
  4. A variable in a Module reacts the same way. If your code attempts to set or retrieve the value of a Public Variable in a Module, VBA must load the entire Module.
  5. If for some strange reason you want to pre-load all Modules, you can actually force this kind of behavior. All you need to do is to add a Public Variable to each Module and attempt to retrieve the value of each in your Application's Start Up code.
  6. Hoped this information helped a little.
Jan 8 '08 #6
ADezii:

Follow up question then (I hope this make sense):

A variable in a Module reacts the same way. If your code attempts to set or retrieve the value of a Public Variable in a Module, VBA must load the entire Module.
It would be considered good practice then to define as many of your public variable in the first module and then call them as needed rather then to define them in a separate module, for example:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Module1()
  2.  
  3. DimVariable1 as Integer (count)
  4.  
  5. Other Necessary code
  6. Call Module2()
  7.  
  8. End sub
  9.  
  10. Public Sub Module2()
  11.  
  12. Variable1 = 0
  13. For Variable1 = 0 to 10
  14. next
  15. return value Variable1
  16. End sub
OR........
Expand|Select|Wrap|Line Numbers
  1. Public Sub Module1()
  2.  
  3. Dim Variables for Module 1
  4.  
  5. Other Necessary code
  6. Call Module2() get variable1
  7.  
  8. End sub
  9.  
  10. Public Sub Module2()
  11.  
  12. Dim Variable1 as Integer (count)
  13.  
  14. Variable1 = 0
  15. For Variable1 = 0 to 10
  16. next
  17. return value Variable1
  18. End sub
What would the difference be between the two examples? Does one execute faster, or is one easier to maintain etc. etc. I would think it is easier to keep track of the variables in the module that creates/operates on them (the second example rather then the first)

If for some strange reason you want to pre-load all Modules, you can actually force this kind of behavior. All you need to do is to add a Public Variable to each Module and attempt to retrieve the value of each in your Application's Start Up code.
Under what circumstances would it be wise to do this. This implies the VB code is compiled at run time for each module, is that true? Or is the code compiled for all modules once at run time, with the compiled code being loaded as you described
Jan 8 '08 #7
ADezii
8,834 Expert 8TB
Hello beebelbrox, I'm a little confused here but don't worry since that seems to happen a lot these days. (LOL). Your first question relates to Public Variables in a Standard Code Module, but your examples depict Local Variables in Public Routines. A Public (Global) Variable is defined in a Code Module within the context of the General Declarations Section, as in:
Expand|Select|Wrap|Line Numbers
  1. Public dteStartDate As Date
As a general rule of thumb, keep the most frequently accessed Public Variables in the same Module in order to minimize Module Loading.

The only instances of which I would think it may be beneficial to pre-load Modules is when you are accessing External Data Sources or possibly Automation code, where you will sacrifice Load Time on the front end for enhanced performance during the life of the Application. This is analagous to Visual Basic, where many Programmers will pre-load all Forms in memory, while a Splash Screen is displayed, so they they may be instantly displayed further down the line.
Jan 8 '08 #8
ADezii

I think I see now, the confusion is on my end, as well as the bad example. Simply Puting the variable in a Public Sub doesnt give it scope for the Whole Project.


Public Sub Mod1()
Dim Variable1

is very diferent then from

Public Variable1
Public Variable2
Public VariableN
Public Sub Mod1()
Jan 8 '08 #9
ADezii
8,834 Expert 8TB
ADezii

I think I see now, the confusion is on my end, as well as the bad example. Simply Puting the variable in a Public Sub doesnt give it scope for the Whole Project.


Public Sub Mod1()
Dim Variable1

is very diferent then from

Public Variable1
Public Variable2
Public VariableN
Public Sub Mod1()
Bingo, for instance:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Mod1()
  2.   Dim Variable1 As String
  3.  
  4.   Variable1 = "Code Example"
  5.   Msgbox Variable1     'will Print Code Example
  6. End Sub 
  1. Mod1() is a Public Sub-Routine Procedure and can be called from anywhere within the entire Application, and the result 'Code Example' will be displayed in a Message Box.
  2. Variable1 is a Variable Local to Mod1() and has an existence (Scope) only within this Sub-Routine. Once you exit the Sub-Routine, Variable1 has no meaning. After you execute Mod1(), you can never reference Variable1 again, unless of course, you Declare it again.
  3. It's a very confusing area (Scope) and I hope that I made it a little clearer.
Jan 8 '08 #10
NeoPa
32,556 Expert Mod 16PB
...
  1. ...
  2. It's a very confusing area (Scope) and I hope that I made it a little clearer.
I think you did :)
Jan 9 '08 #11
ADezii
8,834 Expert 8TB
I think you did :)
Just because you're not confused, don't think for 1 minute that I'm not! (LOL).
Jan 9 '08 #12

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

Similar topics

36
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
3
by: MorrganMail | last post by:
Recently got to take over the responisibility for an application which is making heavy use of an Access database. It has been a long time since I came in contact with anything database related and...
23
by: JohnH | last post by:
I'm just recently come to work for an auto brokerage firm. My position involves performing mysterious rites, rituals and magick in order to get information out of their access database. This is...
2
by: King Ron | last post by:
Ola all. In responding to a recent post requesting help with a search issue, I recommended using a combo box lookup in the table design. "paii, Ron" (no relation) posted this reply: " There are...
3
by: Paul | last post by:
Hi all, I have posted a question in the Database design and theory ng, but I expect a lot of you will have suggestions to help me (and that ng doesn't seem very active). The post is here: ...
5
by: John Sheppard | last post by:
Hello there, I was wondering if anyone knew of a newsgroup for posting schema design questions? Is it inappropriate to post design questions here? (Not really specific to sql server design) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.