A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Get User



 
 
Thread Tools Display Modes
  #1  
Old August 14th, 2007, 08:28 AM posted to microsoft.public.access
Muriukis
external usenet poster
 
Posts: 42
Default Get User

Thanks Daniels for your answer but am yet to be satisfied.The form login is
unbound and it has two textboxes UserName and Password.What I have done is
set the password in code below
Dim a As Integer
Dim b As String
Dim c As String
Dim d As Integer
Dim e As String
Dim f As Integer
a = "2008"
b = "Admin"
c = "User1"
d = "2007"
e = "User2"
f = "2004"

If UserName = b And Password = a Then
stDocName = "Switchboard1"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "Login"
ElseIf UserName = c And Password = d Then
stDocName = "Switchboard2"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "Login"
ElseIf UserName = e And Password = f Then
stDocName = "Switchboard2"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "Login"

Else
MsgBox "Invalid Password, try again!", , "ACCESS DENIED"
Password.SetFocus

End If

End Sub
Now how do I get the login UserName to show in a required text box in
another form in the program
NB its a point of sale and I have not used the access security wizard
anywhere and dont plan to
Any help because when I use the getuser function below am getting my name as
I am the admin of my computer

Public Function GetUser() As String
'return currently logged in user
Dim db As New Application
GetUser = db.CurrentUser
End Function
Where am I going wrong any help or advise is really appreciated

  #2  
Old August 17th, 2007, 01:54 AM posted to microsoft.public.access
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default Get User

One way would be to declare a public variable of string data type in the
Declarations area of any standard module (not a form's class module) in the
database:

Public strCurrentUser as String

Add a function to the module:

Public Function GetCurrentUser() As String

GetCurrentUser = strCurrentUser

End Function

In the code in you login form's module assign the value entered by the user
as their login name to the strCurrent user variable:

strCurrentUser = Me.UserName

In the other form's Load event procedure assign the return value of the
GetCurrentUser function to the relevant control:

Me.YourControl = GetCurrentUser()

Howevere, a better approach to storing a value in a public variable would be
to pass the value to the form in question as its OpenArgs property. However,
its not clear from your post whether the other form in question is the
switchboard form being opened from the login form. If it is you can dispense
with the above and pas the value to it:

DoCmd.OpenForm stDocName _
WhereCondition:=stLinkCriteria, _
OpenArgs = Me.UserName

In each switchboard form's Load event procedure you can then assign the
value to the relevant control:

If Not IsNull(Me.OpenArgs) Then
Me.YourControl = Me.OpenArgs
End If

I take it that you are aware how insecure this method of logging in is
compared with the use of Access's user and group security facility.

One or two other points:

1. You have declared variables a, d and f as Integer data types nut then
assign a string expression to them. The delimiting quotes are unnecessary to
assign a numeric value. In fact you don't need to declare variables and then
assign values to them at all; simply declare constants, e.g.

Const a = 2008
Const b = "Admin"

and so on.

2. You have returned a reference to the Application object with the db
object variable. This is slightly confusing as db suggests a reference to
the Database object. Naming the object variable something like app would be
better. To call the CurrentUser function its not actually necessary to
specify the Application object in fact; GetUser = CurrentUser(), with or
without the parentheses, will suffice. This function is only really of use
if you have implemented user and group security, however, as otherwise it
will return the default Admin user.

Incidentally, if you ever want to return the current Windows login user name
add the following module to the database:

''''module starts''''
Option Compare Database
Option Explicit

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal _
lpBuffer As String, nSize As Long) As Long


Public Function GetUser() As String

Dim strBuffer As String
Dim lngSize As Long, lngRetVal As Long

lngSize = 199
strBuffer = String$(200, 0)

lngRetVal = GetUserName(strBuffer, lngSize)

GetUser = Left$(strBuffer, lngSize - 1)

End Function
''''module ends''''

and call the GetUser function.

Ken Sheridan
Stafford, England

"Muriukis" wrote:

Thanks Daniels for your answer but am yet to be satisfied.The form login is
unbound and it has two textboxes UserName and Password.What I have done is
set the password in code below
Dim a As Integer
Dim b As String
Dim c As String
Dim d As Integer
Dim e As String
Dim f As Integer
a = "2008"
b = "Admin"
c = "User1"
d = "2007"
e = "User2"
f = "2004"

If UserName = b And Password = a Then
stDocName = "Switchboard1"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "Login"
ElseIf UserName = c And Password = d Then
stDocName = "Switchboard2"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "Login"
ElseIf UserName = e And Password = f Then
stDocName = "Switchboard2"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close acForm, "Login"

Else
MsgBox "Invalid Password, try again!", , "ACCESS DENIED"
Password.SetFocus

End If

End Sub
Now how do I get the login UserName to show in a required text box in
another form in the program
NB its a point of sale and I have not used the access security wizard
anywhere and dont plan to
Any help because when I use the getuser function below am getting my name as
I am the admin of my computer

Public Function GetUser() As String
'return currently logged in user
Dim db As New Application
GetUser = db.CurrentUser
End Function
Where am I going wrong any help or advise is really appreciated


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:57 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.