Get User

M

Muriukis

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
 
K

Ken Sheridan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top