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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Define Global Variable ? 1

Status
Not open for further replies.

Ktx7ca

IS-IT--Management
Apr 5, 2008
88
CA
I have 7 forms using the same piece of code to add new records. So I thought I would put it in a module. So I could stop changing it 7 times.

But of course as it can't find the field names as all forms have differnt names.

SO I was thinking maybe I could open some global variables then have the forms could put there data in 2 or 3 of them so the module code could find it.

Alas I can't seam to define any variables that the module code can see.

Any Ideas or A beter way to do this are greatly Aprecaited

Thanks chris
 
It is hard to answer this question without seeing what you have. A little code will help. Please provide some more details.

There are many possible ways to do this, and I think you can compartmentalize your code without using global variables. You could possibly pass the name of the form/control or the form/control object to a common sub routine. You can also grab the active form.

I personally would most likely build my own custom class, but that may be a little too advanced.


Global variables have there place and can be very helpful, but the underlying rule is to always limit the life and scope of any variables.

Global variables are defined in a standard module (not a form or report's module) using the public key word.
Understanding Scope and Visibility


Scope refers to the availability of a variable, constant, or procedure for use by another procedure. There are three scoping levels: procedure-level, private module-level, and public module-level.

You determine the scope of a variable when you declare it. It's a good idea to declare all variables explicitly to avoid naming-conflict errors between variables with different scopes.

Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible outside that procedure. Only the procedure that contains the variable declaration can use it. In the following example, the first procedure displays a message box that contains a string. The second procedure displays a blank message box because the variable strMsg is local to the first procedure.

Sub LocalVariable()
Dim strMsg As String
strMsg = "This variable can't be used outside this procedure."
MsgBox strMsg
End Sub

Sub OutsideScope()
MsgBox strMsg
End Sub

Defining Private Module-Level Scope
You can define module-level variables and constants in the Declarations section of a module. Module-level variables can be either public or private. Public variables are available to all procedures in all modules in a project; private variables are available only to procedures in that module. By default, variables declared with the Dim statement in the Declarations section are scoped as private. However, by preceding the variable with the Private keyword, the scope is obvious in your code.

In the following example, the string variable strMsg is available to any procedures defined in the module. When the second procedure is called, it displays the contents of the string variable strMsg in a dialog box.

' Add following to Declarations section of module.
Private strMsg sAs String

Sub InitializePrivateVariable()
strMsg = "This variable can't be used outside this module."
End Sub

Sub UsePrivateVariable()
MsgBox strMsg
End Sub

Note Public procedures in a standard module or class module are available to any referencing project. To limit the scope of all procedures in a module to the current project, add an Option Private Module statement to the Declarations section of the module. Public variables and procedures will still be available to other procedures in the current project, but not to referencing projects.

Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available to all procedures in the project. In the following example, the string variable strMsg can be used by any procedure in any module in the project.

' Include in Declarations section of module.
Public strMsg As String

All procedures are public by default, except for event procedures. When Visual Basic creates an event procedure, the Private keyword is automatically inserted before the procedure declaration. For all other procedures, you must explicitly declare the procedure with the Private keyword if you do not want it to be public.

You can use public procedures, variables, and constants defined in standard modules or class modules from referencing projects. However, you must first set a reference to the project in which they are defined.

Public procedures, variables, and constants defined in other than standard or class modules, such as form modules or report modules, are not available to referencing projects, because these modules are private to the project in which they reside.
 
I have never really understood classes maybe you have something to read that will explain it better

here is a piece of very ugly code:

Dim db2 As Database, INSSoldDADD As Recordset
Set db2 = DBEngine.Workspaces(0).Databases(0)
Set INSSoldDADD = db2.OpenRecordset("INS-MoveInvenD", DB_OPEN_DYNASET)

INSSoldDADD.AddNew
INSSoldDADD![Session ID] = Forms!setup.[Set General1]
INSSoldDADD![Book ID] = Me![INSSOLD]

INSSoldDADD.Update
INSSoldDADD.Close

Me![List26].Requery

Me.[INSSOLD] = Null

Thanks for your Help


 
So lets say I define a generic routine in a standard module. Something like

Code:
Public Sub genericRoutine(sessionID As Long, bookID As Long, lstList As Access.ListBox)
  'do not need to define the db
  Dim INSSoldDADD As DAO.Recordset
  'never seen the parameter written as DB_OPEN_DYNASET
  'AKAIK dbOpenDynaset
  Set INSSoldDADD = CurrentDb.OpenRecordset("INS-MoveInvenD", dbOpenDynaset)
   INSSoldDADD.AddNew
   INSSoldDADD![Session ID] = sessionID
   INSSoldDADD![Book ID] = bookID
   INSSoldDADD.Update
   INSSoldDADD.Close
   lstList.Requery
   'not sure what the below is
   'Me.[INSSOLD] = Null
End Sub

I can call this routine from any form like

call genericRoutine(me.txtBxSession.value, me.TxtBxBookID.value, me.lst26)

from another form
call genericRoutine(me.txtBx32.value, me.TxtBx33.value, me.lst34)
 
How are ya Ktx7ca . . .
MajP said:
[blue]You could possibly pass the name of the form/control or the [blue]form/control object[/blue] to a common sub routine.[/blue]
Definitely the way to go. The folowing is just an example (since you've provided no code):

In a module in the modules window you'd have something like:
Code:
[blue]Public Sub CommonFormCode(ctl As Control)

   If Trim(ctl & "") <> "" Then
      [green][b]'Your Code Here[/b][/green]
   Else
      MsgBox "Can't Continue! Textbox is Empty!"
   End If

End Sub[/blue]
Calls to the routine from any form or subform would look like:
Code:
[blue]   Call CommonFormCode(Me![purple][b][i]ControlName[/i][/b][/purple][/quote])[/blue]
[blue]Your Thoughts? . . .[/blue]


Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Sorry about that . . . should be:
Code:
[blue]   Call CommonFormCode(Me![purple][b][i]ControlName[/i][/b][/purple])[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
HI MajP and Theaceman1 I just wanted to say thanks for your help adn for filling a gap of knolwage I was missing

Thanks it works Great
 
Ktx7ca . . .

When you have the time, do have a look at one of the links at the bottom of my post. The links will help you ask better questions, get faster responses, and gives insight into etiquette here in the forms . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top