|
|
|
date: Thu, 3 Jul 2008 09:39:03 -0700,
group: microsoft.public.access.modulescoding
back
Newbie... Need to filter a form by information stored in a module
Newbie to VB here. I'm working in Access 2007
I've created a login screen to a dbase I'm working on. In the code for the
script to validate the user and their password, I've ended it by establishing
values to store the name and ID of the user that just logged in. I did that
with the following code before the end of the sub:
lngCurrentUser = Me.UserName.Value
lngCurrentUserID = DLookup("ID", "lst_SalesRep",
"[forms]![Login_01-Open]![UserName] = [Logon]")
Then I also created a module in app to store those values for later use
(i.e. to filter reports, forms, etc.)
That module is called CrntUser and has the following code:
Public lngCurrentUser As String
Public lngCurrentUserID As Long
Now all I need to do is reference those two values in my forms, reports,
etc. Haven't a clue how to do this. I see the module as an object in the
database. But I can't seem to get any of the forms to see that data. I may
be doing this completely incorrectly. This is my first time working in VB.
I'm sure the answer is pretty easy, but being a newbie, it's not so easy for
me.
date: Thu, 3 Jul 2008 09:39:03 -0700
author: Tellis2112
RE: Newbie... Need to filter a form by information stored in a module
If the code you posted runs without an error, that means the public variable
are visible. You didn't say you were getting an error, so I assume that is
working.
When you want to use the values in the variables, you would address them
like any other variable, but they will not be visible to queries. Queries
cannot see any variable regardless of scope.
That is just one problem with public variables. There are others. Any
unhandled error will reset the the values of public variables. If you happen
to use a local variable with the same name, the local variable takes
precidence and the public variable will not be seen.
As a matter of practice I don't use public variables at all. When I need a
globally available vaule, I use a static function in standard module. Here
is an example:
Static Function CurrentUserID(Optional varUID As Variant) As Variant
Dim varOldVal As Variant
If Not IsMissing(varUID) Then
varOldVal = varUID
End If
If varOldVal = vbNullString Then
CurrentUserID = Null
Else
CurrentUserID = varOldVal
End If
End Function
The way it works is that if you call it without passing a value, it returns
the last value it received. When you pass it a value, it retains it until
you close the application or pass it a new value. If you have not
initialized it with a value, it returns Null. So to use it in your code, it
would be like:
CurrentUserID(DLookup("ID", "lst_SalesRep",
"[forms]![Login_01-Open]![UserName] = [Logon]"))
That will pass it the value. Now, when you need to retrieve the value it is:
SomeVarialbe = CurrentUserID
And, you can use it in a query to do filtering:
WHERE [UserID] = CurrentUserID()
--
Dave Hargis, Microsoft Access MVP
"Tellis2112" wrote:
> Newbie to VB here. I'm working in Access 2007
> I've created a login screen to a dbase I'm working on. In the code for the
> script to validate the user and their password, I've ended it by establishing
> values to store the name and ID of the user that just logged in. I did that
> with the following code before the end of the sub:
>
> lngCurrentUser = Me.UserName.Value
>
> lngCurrentUserID = DLookup("ID", "lst_SalesRep",
> "[forms]![Login_01-Open]![UserName] = [Logon]")
>
> Then I also created a module in app to store those values for later use
> (i.e. to filter reports, forms, etc.)
>
> That module is called CrntUser and has the following code:
>
> Public lngCurrentUser As String
> Public lngCurrentUserID As Long
>
> Now all I need to do is reference those two values in my forms, reports,
> etc. Haven't a clue how to do this. I see the module as an object in the
> database. But I can't seem to get any of the forms to see that data. I may
> be doing this completely incorrectly. This is my first time working in VB.
>
> I'm sure the answer is pretty easy, but being a newbie, it's not so easy for
> me.
date: Thu, 3 Jul 2008 11:26:01 -0700
author: Klatuu
RE: Newbie... Need to filter a form by information stored in a mod
Dave,
Your a beautiful human being. I can't make complete sense yet of what
you had me add, but it worked like a charm. Thanks for your help. Once I
get this down better, I hope to add some help to this forum of my own.
Thanks again.
--
Any help is appreciated. And thanks to all you guys and gals who read and
reply to these threads. You''''re the best!
"Klatuu" wrote:
> If the code you posted runs without an error, that means the public variable
> are visible. You didn't say you were getting an error, so I assume that is
> working.
>
> When you want to use the values in the variables, you would address them
> like any other variable, but they will not be visible to queries. Queries
> cannot see any variable regardless of scope.
>
> That is just one problem with public variables. There are others. Any
> unhandled error will reset the the values of public variables. If you happen
> to use a local variable with the same name, the local variable takes
> precidence and the public variable will not be seen.
>
> As a matter of practice I don't use public variables at all. When I need a
> globally available vaule, I use a static function in standard module. Here
> is an example:
>
> Static Function CurrentUserID(Optional varUID As Variant) As Variant
> Dim varOldVal As Variant
>
> If Not IsMissing(varUID) Then
> varOldVal = varUID
> End If
> If varOldVal = vbNullString Then
> CurrentUserID = Null
> Else
> CurrentUserID = varOldVal
> End If
>
> End Function
>
> The way it works is that if you call it without passing a value, it returns
> the last value it received. When you pass it a value, it retains it until
> you close the application or pass it a new value. If you have not
> initialized it with a value, it returns Null. So to use it in your code, it
> would be like:
>
> CurrentUserID(DLookup("ID", "lst_SalesRep",
> "[forms]![Login_01-Open]![UserName] = [Logon]"))
>
> That will pass it the value. Now, when you need to retrieve the value it is:
>
> SomeVarialbe = CurrentUserID
>
> And, you can use it in a query to do filtering:
>
> WHERE [UserID] = CurrentUserID()
>
> --
> Dave Hargis, Microsoft Access MVP
>
>
> "Tellis2112" wrote:
>
> > Newbie to VB here. I'm working in Access 2007
> > I've created a login screen to a dbase I'm working on. In the code for the
> > script to validate the user and their password, I've ended it by establishing
> > values to store the name and ID of the user that just logged in. I did that
> > with the following code before the end of the sub:
> >
> > lngCurrentUser = Me.UserName.Value
> >
> > lngCurrentUserID = DLookup("ID", "lst_SalesRep",
> > "[forms]![Login_01-Open]![UserName] = [Logon]")
> >
> > Then I also created a module in app to store those values for later use
> > (i.e. to filter reports, forms, etc.)
> >
> > That module is called CrntUser and has the following code:
> >
> > Public lngCurrentUser As String
> > Public lngCurrentUserID As Long
> >
> > Now all I need to do is reference those two values in my forms, reports,
> > etc. Haven't a clue how to do this. I see the module as an object in the
> > database. But I can't seem to get any of the forms to see that data. I may
> > be doing this completely incorrectly. This is my first time working in VB.
> >
> > I'm sure the answer is pretty easy, but being a newbie, it's not so easy for
> > me.
date: Thu, 3 Jul 2008 11:49:00 -0700
author: Tellis2112
RE: Newbie... Need to filter a form by information stored in a mod
Glad I could help.
Don't be shy. If you see a question and you know the answer, jump right in.
--
Dave Hargis, Microsoft Access MVP
"Tellis2112" wrote:
> Dave,
>
> Your a beautiful human being. I can't make complete sense yet of what
> you had me add, but it worked like a charm. Thanks for your help. Once I
> get this down better, I hope to add some help to this forum of my own.
>
> Thanks again.
>
> --
> Any help is appreciated. And thanks to all you guys and gals who read and
> reply to these threads. You''''re the best!
>
>
> "Klatuu" wrote:
>
> > If the code you posted runs without an error, that means the public variable
> > are visible. You didn't say you were getting an error, so I assume that is
> > working.
> >
> > When you want to use the values in the variables, you would address them
> > like any other variable, but they will not be visible to queries. Queries
> > cannot see any variable regardless of scope.
> >
> > That is just one problem with public variables. There are others. Any
> > unhandled error will reset the the values of public variables. If you happen
> > to use a local variable with the same name, the local variable takes
> > precidence and the public variable will not be seen.
> >
> > As a matter of practice I don't use public variables at all. When I need a
> > globally available vaule, I use a static function in standard module. Here
> > is an example:
> >
> > Static Function CurrentUserID(Optional varUID As Variant) As Variant
> > Dim varOldVal As Variant
> >
> > If Not IsMissing(varUID) Then
> > varOldVal = varUID
> > End If
> > If varOldVal = vbNullString Then
> > CurrentUserID = Null
> > Else
> > CurrentUserID = varOldVal
> > End If
> >
> > End Function
> >
> > The way it works is that if you call it without passing a value, it returns
> > the last value it received. When you pass it a value, it retains it until
> > you close the application or pass it a new value. If you have not
> > initialized it with a value, it returns Null. So to use it in your code, it
> > would be like:
> >
> > CurrentUserID(DLookup("ID", "lst_SalesRep",
> > "[forms]![Login_01-Open]![UserName] = [Logon]"))
> >
> > That will pass it the value. Now, when you need to retrieve the value it is:
> >
> > SomeVarialbe = CurrentUserID
> >
> > And, you can use it in a query to do filtering:
> >
> > WHERE [UserID] = CurrentUserID()
> >
> > --
> > Dave Hargis, Microsoft Access MVP
> >
> >
> > "Tellis2112" wrote:
> >
> > > Newbie to VB here. I'm working in Access 2007
> > > I've created a login screen to a dbase I'm working on. In the code for the
> > > script to validate the user and their password, I've ended it by establishing
> > > values to store the name and ID of the user that just logged in. I did that
> > > with the following code before the end of the sub:
> > >
> > > lngCurrentUser = Me.UserName.Value
> > >
> > > lngCurrentUserID = DLookup("ID", "lst_SalesRep",
> > > "[forms]![Login_01-Open]![UserName] = [Logon]")
> > >
> > > Then I also created a module in app to store those values for later use
> > > (i.e. to filter reports, forms, etc.)
> > >
> > > That module is called CrntUser and has the following code:
> > >
> > > Public lngCurrentUser As String
> > > Public lngCurrentUserID As Long
> > >
> > > Now all I need to do is reference those two values in my forms, reports,
> > > etc. Haven't a clue how to do this. I see the module as an object in the
> > > database. But I can't seem to get any of the forms to see that data. I may
> > > be doing this completely incorrectly. This is my first time working in VB.
> > >
> > > I'm sure the answer is pretty easy, but being a newbie, it's not so easy for
> > > me.
date: Thu, 3 Jul 2008 12:11:01 -0700
author: Klatuu
|
|