Access-update username and name of the file which is imported
Question posted by: smugcool
(Member)
on
July 2nd, 2008 06:39 AM
Hi Experts,
I have got a MS-ACCESS database, where i import excel files into it. This file is kept in a network drive. So everybody can access it.
Now my problem is that i wanted to know who has imported which file. So, i have added a column in the table with username and another column with Imported_file name. How to auto-generate the systemname and imported file in the access column.
Regards
Anup
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
July 2nd, 2008 01:12 PM
# 2
|
Re: Access-update username and name of the file which is imported
Auto-generate?
Why would you think that were possible (It's not - that I've ever heard of at least)?
As far as coding it in is concerned, I imagine you already have access to the filename of the spreadsheet, but for the username, I would use Environ("UserName") myself, unless security is an issue (It would be unusual, but possible, for a user to change this value to reflect the username of someone else).
If security is an issue then the following code should work for you :
Code: ( text )
'GetUser returns the user's logged on name. Public Function GetUser() As String Dim strUserKey As String If Environ("OS") = "Windows_NT" Then strUserKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" GetUser = RegRead(conHKLM, strUserKey, "DefaultUserName") Else 'Windows strUserKey = "Network\Logon" GetUser = RegRead(conHKLM, strUserKey, "username") End If End Function
|
|
July 2nd, 2008 01:19 PM
# 3
|
Re: Access-update username and name of the file which is imported
For this to work you will need the function RegRead defined of course. For that add the following code into a separate module in any database that requires this.
Code: ( text )
Option Compare Database Option Explicit Public Const conHKCR = &H80000000 Public Const conHKCU = &H80000001 Public Const conHKLM = &H80000002 Public Const conHKU = &H80000003 Public Const conStandardRightsAll = &H1F0000 Public Const conReadControl = &H20000 Public Const conStandardRightsRead = (conReadControl) Public Const conRegSz = 1 Public Const conOK = 0& Public Const conKeyQueryValue = &H1 Public Const conKeySetValue = &H2 Public Const conKeyCreateLink = &H20 Public Const conKeyCreateSubKey = &H4 Public Const conKeyEnumerateSubKeys = &H8 Public Const conKeyNotify = &H10 Public Const conSynchronise = &H100000 Public Const conRegOptionNonVolatile = 0 Public Const conKeyAllAccess = ((conStandardRightsAll Or _ conKeyQueryValue Or _ conKeySetValue Or _ conKeyCreateSubKey Or _ conKeyEnumerateSubKeys Or _ conKeyNotify Or _ conKeyCreateLink) And _ (Not conSynchronise)) Public Const conKeyRead = ((conReadControl Or _ conKeyQueryValue Or _ conKeyEnumerateSubKeys Or _ conKeyNotify) And _ (Not conSynchronise)) Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _ Alias "RegOpenKeyExA" (ByVal hKey As Long, _ ByVal lpSubKey As String, _ ByVal ulOptions As Long, _ ByVal samDesired As Long, _ phkResult As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) _ As Long Private Declare Function RegQueryValueExStr Lib "advapi32.dll" _ Alias "RegQueryValueExA" (ByVal hKey As Long, _ ByVal lpValueName As String, _ ByVal lpReserved As Long, _ lpType As Long, _ ByVal lpData As String, _ lpcbData As Long) As Long Public Function RegRead(ByVal lngHive As Long, _ ByVal strKey As String, _ ByVal strValue As String) As Variant Dim intIdx As Integer, intHK As Integer Dim strWork As String Dim lngRet As Long, lngLen As Long, lngHKey As Long, lngType As Long RegRead = Null strKey = strKey & Chr(0) lngRet = RegOpenKeyEx(lngHive, strKey, 0, conKeyRead, lngHKey) If lngRet = conOK Then 'Create buffer to store value strWork = Space(255) lngLen = 255 lngRet = RegQueryValueExStr(lngHKey, _ strValue, _ 0&, _ lngType, _ strWork, _ lngLen) RegRead = Left(strWork, lngLen - 1) If Len(RegRead) = 254 Then RegRead = Null 'Close key Call RegCloseKey(lngHKey) End If End Function
|
|
July 2nd, 2008 11:48 PM
# 4
|
Re: Access-update username and name of the file which is imported
This will work locally, but I'm not sure if it will work in your specific case. Give it a try and see what happens:
Code: ( text )
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, nSize As Long) As Long
Code: ( text )
Dim strBuffer As String Dim lngPadding As Long Dim strUserName As String lngPadding = 255 strBuffer = Space(lngPadding) If GetUserName(strBuffer, lngPadding) > 0 Then strUserName = Left(strBuffer, lngPadding) Debug.Print "User Name: " & strUserName End If
|
|
July 4th, 2008 11:38 AM
# 5
|
Re: Access-update username and name of the file which is imported
Quote:
Originally Posted by ADezii
This will work locally, but I'm not sure if it will work in your specific case. Give it a try and see what happens:
Code: ( text )
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, nSize As Long) As Long
Code: ( text )
Dim strBuffer As String Dim lngPadding As Long Dim strUserName As String lngPadding = 255 strBuffer = Space(lngPadding) If GetUserName(strBuffer, lngPadding) > 0 Then strUserName = Left(strBuffer, lngPadding) Debug.Print "User Name: " & strUserName End If
|
HI
I have pasted the code in my database. But how user who imports the file into database will refelct in a Username column or file which has got imported will reflect in File name column. What should i do ? Kindly suggest.
|
|
July 4th, 2008 12:47 PM
# 6
|
Re: Access-update username and name of the file which is imported
Anup, as you've not actually posted any details about your data or any code you already have, this is not something we can easily answer.
Not the answer you were looking for? Post your question . . .
183,631 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Latest Articles: Read & Comment
Top Microsoft Access / VBA Forum Contributors
|