 |
|
 |
GeoffS Level: VB Lord

 Registered: 29-09-2004 Posts: 536
|
Re: Queries using global variables
Hi,
There are several ways in which you can store Variable details in memory. You can create a Global Variable in the Declarations Section of the main code module, so that it will then be visible throughout the project, or if you want to be "neater" from a programming point of view then create a Class to store all your Global data.
The following holds for Access 2002 (Office XP) so I presume it will be much the same for 2003 version.
Select "Insert" from main menu, and click on "Class Module".
You need to create local variables in the declaration section, then create the Properties of your Class. So for your Users it would be good to create Properties for "UserName", "UserPassLevel", and "UserID". Then when the User logs in you set the properties, which you can then reference from code at any time.
Option Compare Database
Option Explicit
Private mvarUserName As String
Private mvarUserID As Long
Private mvarUserPassLevel As Integer
Public Property Let UserID(ByVal vData As Long)
mvarUserID = vData
End Property
Public Property Get UserID() As Long
UserID = mvarUserID
End Property
Public Property Let UserName(ByVal vData As String)
mvarUserName = vData
End Property
Public Property Get UserName() As String
UserName = mvarUserName
End Property
Public Property Let UserPassLevel(ByVal vData As Integer)
mvarUserPassLevel = vData
End Property
Public Property Get UserPassLevel() As Integer
UserPassLevel = mvarUserPassLevel
End Property
|
Save your Class Module as "clsGlobals"
You need to create an instance of the Class when your application starts, so in the Declarations section of the main module of your app declare "Public gvals As clsGlobals" - Then, in the first Procedure that you run when your app opens initialise the Class with the line "Set gvals = New clsGlobals" - Now you can reference any of the properties that you create in the Class from anywhere in your app.
So, when you have checked the credentials of your User you can set the Properties from within that procedure, it would look something like:-
'after the code that returns user details in Recordset
gvals.UserID = Recordset("user_id")
gvals.UserPassLevel = Recordset("user_security")
gvals.UserName = Recordset("user_name")
|
So, when you want to run a Query with your Users ID in it then you can access the information directly from your Class Property :-
Dim strSQL As String
strSQL = "SELECT TableName.* FROM TableName " _
& "WHERE (((TableName.user_id)=" & gvals.UserID & "));"
|
Hope that helps.
____________________________
multi-tasking - the ability to hang more than one app. at the same time.
|
|
10-06-2007 at 01:54 PM |
|
|
|
|
 |
 |