I am trying to write VB code that would allow Access to recognize the computer ID of the person opening the database, open a form, and filter to the records assigned to the user. Does anyone know how to accomplish that?
相关问题
- Error handling only works once
- Excel formula in VBA code
- Excel VBA run time error 450 from referencing a ra
- Importing data from MS Access db to PostgreSQL db
- DoCmd.TransferSpreadsheet is not recognizing works
相关文章
- Unregister a XLL in Excel (VBA)
- numeric up down control in vba
- Declare a Range relative to the Active Cell with V
- COALESCE, IFNULL, or NZ() function that can be use
- CurrentDb.RecordsAffected returns 0. Why?
- How to disable changes in a cell using vba?
- MsgBox Yes/No Excel VBA
- Rounding up to nearest higher integer in VBA
An API to get the user name is probably better than using Environ. If the name is available in a table, you can use DLookUp to get an ID, which can be used with the Where argument of the OpenForm method of the DoCmd object to open a filtered list.
You can use Environ$("ComputerName") if that is what you mean by ComputerID. Here's a quick and dirty way I did it. Note that the user can usually turn off the form's filter. As others have stated, the environment variables on the PC have the potential to be modified by a non-admin user.
If you're concerned that the user will modify the environment variable for computer (i.e., SET COMPUTERNAME=MYBOSS via cmd) you can use a Win32 API call to get the ComputerName value if you know that the user is a non-admin user who cannot modify the computername via registry or through windows (see below).
CAVEAT with API
If the user has Admin privilege on the PC and they are able to edit the HKLM registry values then they could make the following registry entry:
If they restart the PC the Windows API call will use the incorrect name. They could then set the registry entry back to normal and restart PC.
In my scenario I have multiple users working on different days from same PC and Access Database. They open the database from a shared folder location on the PC. When they click the switchboard item and the form opens up the form's Load event runs. This form is bound to a table with a column named USERNAME which is simply the environment variable value of UserName (ex. John.Smith).
Here's the VBA I put in the load event:
API Call Solution
If you'd prefer to make an API call to get the ComputerName instead then you can do the following:
The API call is GetComputerName is a derivation from code either from Ken Getz (VBA Developer's Handbook) or Dan Appleman (Visual Basic Programmer's Guide to the Win32 API). You'll also find it all over the internet. First add a Module to your project rename it API or something meaningful. Then insert the following code:
In the form load event (modified to use API wrapper function instead of Envrion$):