473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to use login username to filter all records in the current database.

17
hello guys,

how can i use the login username to filter all records in the specific database the user is working on, including looking into his profile information table and editing it (only finding his profile data), create new records in performance tables (with his username stored as well) etc.

how can i store the username till the user is logout or exit the database?
Jun 8 '15 #1

✓ answered by jforbes

Hey rod4,

If your users have Unique Windows Logins, I would use this to identify them. Then use that Identity to Filter Forms as needed to just the currently logged in user.

This is a function I use to get the Currently logged in User (I stripped out some things to make it more generic):
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Global gImpersonateUser As String
  5.  
  6. Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  7.  
  8. Public Function GetWindowsUser() As String
  9. On Error GoTo ErrorOut
  10.  
  11.     ' Source:  http://www.techrepublic.com/blog/10-things/10-plus-of-my-favorite-windows-api-functions-to-use-in-office-applications/
  12.  
  13.     Dim lngResponse As Long
  14.     Dim strUserName As String * 32
  15.  
  16.     If Len(gImpersonateUser) > 0 Then
  17.         GetWindowsUser = gImpersonateUser
  18.     Else
  19.         lngResponse = GetUserName(strUserName, 32)
  20.         GetWindowsUser = Left(strUserName, InStr(strUserName, Chr$(0)) - 1)
  21.     End If
  22.  
  23. ExitOut:
  24.     Exit Function
  25. ErrorOut:
  26.     MsgBox "Could not determine Windows User."
  27.     Resume ExitOut
  28. End Function
Then you can reference the GetWindowsUser() function when you want to Filter a Form as well as use it for a default when entering data on a Form.

There is a few ways to use it to Filter the Recordset of a Form. One way is to create a Query with the GetWindowsUser() function as part of the criteria:
Expand|Select|Wrap|Line Numbers
  1. SELECT Employees.*, Employees.UserID
  2. FROM Employees
  3. WHERE (((Employees.UserID)=GetWindowsUser()));
  4.  
Another way would be to create the Form as normal, then apply a Filter On Load either from the DoCmd.OpenForm method or in the OnLoad Event of the Form:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "Preferences", WhereCondition:="UserID='" & GetWindowsUser() & "'"
  2. OR
  3. Private Sub Form_Load()
  4.     Me.Filter = "UserID='" & GetWindowsUser() & "'"
  5.     Me.FilterOn = True
  6. End Sub
  7.  
You can also use the GetWindowsUser() function as a default for fields that you want to default to the currently logged in user. Easiest way to do this is add a TextBox to the Form, set it's ControlSource to the field name that you want to default, set the Default Property to =GetWindowsUser() and then either Lock or Hide the field.

One last thing to note, in the code above, it makes use of a Global Variable named gImpersonateUser. If you want to do some debugging and want to act like you are someone else, set gImpersonateUser equal to their Windows User ID and everything that uses GetWindowsUser() will get their Windows User Name instead of yours.

2 2326
jforbes
1,107 Expert 1GB
Hey rod4,

If your users have Unique Windows Logins, I would use this to identify them. Then use that Identity to Filter Forms as needed to just the currently logged in user.

This is a function I use to get the Currently logged in User (I stripped out some things to make it more generic):
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Global gImpersonateUser As String
  5.  
  6. Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  7.  
  8. Public Function GetWindowsUser() As String
  9. On Error GoTo ErrorOut
  10.  
  11.     ' Source:  http://www.techrepublic.com/blog/10-things/10-plus-of-my-favorite-windows-api-functions-to-use-in-office-applications/
  12.  
  13.     Dim lngResponse As Long
  14.     Dim strUserName As String * 32
  15.  
  16.     If Len(gImpersonateUser) > 0 Then
  17.         GetWindowsUser = gImpersonateUser
  18.     Else
  19.         lngResponse = GetUserName(strUserName, 32)
  20.         GetWindowsUser = Left(strUserName, InStr(strUserName, Chr$(0)) - 1)
  21.     End If
  22.  
  23. ExitOut:
  24.     Exit Function
  25. ErrorOut:
  26.     MsgBox "Could not determine Windows User."
  27.     Resume ExitOut
  28. End Function
Then you can reference the GetWindowsUser() function when you want to Filter a Form as well as use it for a default when entering data on a Form.

There is a few ways to use it to Filter the Recordset of a Form. One way is to create a Query with the GetWindowsUser() function as part of the criteria:
Expand|Select|Wrap|Line Numbers
  1. SELECT Employees.*, Employees.UserID
  2. FROM Employees
  3. WHERE (((Employees.UserID)=GetWindowsUser()));
  4.  
Another way would be to create the Form as normal, then apply a Filter On Load either from the DoCmd.OpenForm method or in the OnLoad Event of the Form:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "Preferences", WhereCondition:="UserID='" & GetWindowsUser() & "'"
  2. OR
  3. Private Sub Form_Load()
  4.     Me.Filter = "UserID='" & GetWindowsUser() & "'"
  5.     Me.FilterOn = True
  6. End Sub
  7.  
You can also use the GetWindowsUser() function as a default for fields that you want to default to the currently logged in user. Easiest way to do this is add a TextBox to the Form, set it's ControlSource to the field name that you want to default, set the Default Property to =GetWindowsUser() and then either Lock or Hide the field.

One last thing to note, in the code above, it makes use of a Global Variable named gImpersonateUser. If you want to do some debugging and want to act like you are someone else, set gImpersonateUser equal to their Windows User ID and everything that uses GetWindowsUser() will get their Windows User Name instead of yours.
Jun 8 '15 #2
rod4
17
hi jforbes,

thanks for your help! i am trying this right away!
Jun 10 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Matt | last post by:
Hi everyone, still pretty new to MySQL. I was wondering if there is a way to automatically filter records based on a mysql userlogin name?? I have serveral databases that I want to combine in...
0
by: Nasos | last post by:
I want to have a command button which enables the user to make a copy of the current database, but without any records in it. Of course all the forms and reports will be intact. I want the old...
2
by: FayeC | last post by:
I have created a site in Joomla with a login (no self registration, users are provided with username and password by the admin). The users are supposed to login to a specific page where they can...
1
by: masri999 | last post by:
How to find All Permissions in the Current Database for a particular User in SQL 2005 ? Thanks M A Srinivas
0
by: =?Utf-8?B?Sm9keQ==?= | last post by:
Does anyone know how to explain to the user what the comparison options in filter records in mail merge mean? For example, "Equal to".
11
by: MarkTingson | last post by:
I am creating a VB6 project that uses listview as my datagrid. So far I am successful in adding, editing, deleting and refreshing the table (I'm talking about the listview). My problem now is to do...
2
by: Raymond Chiu | last post by:
Dear all, If I have the dataset, What the code should be to filter records in the dataset by some fields criteria? Is it like a SQL? Thanks for your help,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.