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

Global Vars

Status
Not open for further replies.

fernandezd

IS-IT--Management
Jan 30, 2002
48
US
Hi all,

I am thick and cannot unserstand this.

I have the following variables in the database that need to be global and set once so that I do not have to set them up.

1) Varibales for the install directory (InstPath)
2) variables for my custom temporary directory (SourcePath)
3) Variables for my files directory (DestPath)

Also to add a twist I use a lot of bmp files for my button faces and they are all linked files not embedded.

How do I set a variables up so that they pick up this vatiable and I do not have to change every button once I complete the database??

So how do I do it???

Thanks U very very much.. Man I hate access some times.
FernandezD
At your service.
Unix systems Admin
 
You can declare variables as Globals in a main module
Global MyVar As String JHall
 
Hi Thanks

How Do I set these varibales then..

I can make them global with the code but How Do I set them up to have a value that is set globally..?? FernandezD
At your service.
Unix systems Admin
 
When do you want to set them, when the database opens?

If so then you can create a Macro named Autoexec to call a function that sets them at the outset. But if these values are not going to be changed interactively you should create them as constants and then their values will be assigned at compile time.

Global Const MyVar As String = "This value"
JHall
 
Hi,

I store permanent globals to a hidden table. And from that table I read the value whenever I need it.

Ivo
 
Assign them as constants on the function that the macro calls ??

then I will be able to get them in any other function I use is this correct ? FernandezD
At your service.
Unix systems Admin
 
You don't even need the macro or anything else. The values are set at compile time. They will always be available application wide. JHall
 
Ok

So where Do I set them Up.?
In a module ??


Ivo
Can U please please please please send me an example please??

FernandezD
At your service.
Unix systems Admin
 
Yes, in a module, in the declarations section at the top

I suggest creating a module called Main

Here is an example declaration of a global constant

Global Const MyVar As String = "This value"


From the moment this is compiled the Constant MyVar will be available application wide.
JHall
 
Excelent...

Thanks

IVOEF can U stil send me an example of the code to retreve the information from the table from code ans set the global constants PLEASE...!!!

FernandezD
At your service.
Unix systems Admin
 
Hi fernandez,

sorry for my delay I forgot this thread. There is an example.

In my application I have a hidden table that I use for storing everything. The table has the folloving fields:

Variable (text) - variable name
Category (text) - in order to use the same names of variables in different circumstances
Value (memo) - value of the variable, type memo because of long strings
Type (number) - VBA constants defining the type of variable
e.g. vbInteger, vbDate etc.

Primary key is set on Variable and Category

Next I have two function for saving and reading the variables.

Please be aware that the function setVariable doesn't work for linked table. In that case you should use method FindFirst instead of Seek.

Public Function setVariable(acVariableName As String, vVariable As Variant, Optional acVariableCategory _
As String = "General", Optional nVariableType As Integer = -1) As Integer

' Code Header inserted by the Procedure Header Add-In
'=============================================================
' AppGlobal.setVariable
' Function creates new or modifies an existing variable
'-------------------------------------------------------------
' Parameters:
'-----------
' acVariableName (String) Name of variable
' vVariable (Variant) Value
' acVariableCategory (String) Category
nVariableType (Integer) Variable type, only when a new variable is created
'-------------------------------------------------------------
' Returns: = 0 - OK
' < 0 - Error number
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
' 12-13-2000 IF:
'=============================================================
' End Code Header block

On Error GoTo HandleErr

Dim db As DAO.Database
Dim rsSettings As DAO.Recordset

Set db = CurrentDb

Set rsSettings = db.OpenRecordset(&quot;Settings&quot;)

With rsSettings
.index = &quot;PrimaryKey&quot;
.Seek &quot;=&quot;, acVariableName, acVariableCategory
If .NoMatch = True Then
If nVariableType = -1 Then
Err.Raise ERROR_PARAM, &quot;AppGlobal.setVariable&quot;, &quot;Unspecified variable type&quot;
End If
'New variable record
.AddNew
![Variable] = acVariableName
![Category] = acVariableCategory
![Value] = CStr(vVariable)
![Type] = nVariableType
Else
.Edit
![Value] = CStr(vVariable)
End If
.Update
End With
EXIT_CLOSE:
rsSettings.Close
CLOSE_DB:
db.Close
CLOSE_NOTHING:
Set rsSettings = Nothing
Set db = Nothing
Exit Function

HandleErr:
Select Case Err.Number
Case Else
MsgBox &quot;Error &quot; & Err.Number & &quot;: &quot; & Err.Description, vbCritical, &quot;AppGlobal.setVariable&quot;
End Select
setVariable = -1
If db Is Nothing Then
Resume CLOSE_NOTHING
End If
If rsSettings Is Nothing Then
Resume CLOSE_DB
End If
Resume EXIT_CLOSE
End Function

Public Function getVariable(acVariableName As String, Optional acVariableCategory As String = &quot;General&quot;) As Variant
' Parameters:
'-----------
' acVariableName (String) variable name
' acVariableCategory (string) variable category
'-------------------------------------------------------------
' Returns: variable value as variant
'-----------------------------------------------------------

Dim vVarValue As Variant

On Error GoTo HandleErr

vVarValue = DLookup(&quot;[Value]&quot;, &quot;Settings&quot;, &quot;[Variable] = '&quot; & acVariableName & &quot;' AND [Category] = '&quot; & acVariableCategory & &quot;'&quot;)
getVariable = vVarValue

ExitHere:
Exit Function

HandleErr:
Select Case Err.Number
Case Else
MsgBox &quot;Error &quot; & Err.Number & &quot;: &quot; & Err.Description, vbCritical, &quot;AppGlobal.getVariable&quot;
End Select
End Function


 
Thanks IVOEF.

This will help me a great big deal. FernandezD
At your service.
Unix systems Admin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top