Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

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).
NeoPa's Avatar
NeoPa
Administrator
10,384 Posts
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 )
  1. 'GetUser returns the user's logged on name.
  2. Public Function GetUser() As String
  3.     Dim strUserKey As String
  4.  
  5.     If Environ("OS") = "Windows_NT" Then
  6.         strUserKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
  7.         GetUser = RegRead(conHKLM, strUserKey, "DefaultUserName")
  8.     Else
  9.         'Windows
  10.         strUserKey = "Network\Logon"
  11.         GetUser = RegRead(conHKLM, strUserKey, "username")
  12.     End If
  13. End Function

Reply
NeoPa's Avatar
NeoPa
Administrator
10,384 Posts
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 )
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Const conHKCR = &H80000000
  5. Public Const conHKCU = &H80000001
  6. Public Const conHKLM = &H80000002
  7. Public Const conHKU = &H80000003
  8. Public Const conStandardRightsAll = &H1F0000
  9. Public Const conReadControl = &H20000
  10. Public Const conStandardRightsRead = (conReadControl)
  11. Public Const conRegSz = 1
  12. Public Const conOK = 0&
  13. Public Const conKeyQueryValue = &H1
  14. Public Const conKeySetValue = &H2
  15. Public Const conKeyCreateLink = &H20
  16. Public Const conKeyCreateSubKey = &H4
  17. Public Const conKeyEnumerateSubKeys = &H8
  18. Public Const conKeyNotify = &H10
  19. Public Const conSynchronise = &H100000
  20. Public Const conRegOptionNonVolatile = 0
  21. Public Const conKeyAllAccess = ((conStandardRightsAll Or _
  22.                                 conKeyQueryValue Or _
  23.                                 conKeySetValue Or _
  24.                                 conKeyCreateSubKey Or _
  25.                                 conKeyEnumerateSubKeys Or _
  26.                                 conKeyNotify Or _
  27.                                 conKeyCreateLink) And _
  28.                                (Not conSynchronise))
  29. Public Const conKeyRead = ((conReadControl Or _
  30.                             conKeyQueryValue Or _
  31.                             conKeyEnumerateSubKeys Or _
  32.                             conKeyNotify) And _
  33.                            (Not conSynchronise))
  34.  
  35. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
  36.     Alias "RegOpenKeyExA" (ByVal hKey As Long, _
  37.                            ByVal lpSubKey As String, _
  38.                            ByVal ulOptions As Long, _
  39.                            ByVal samDesired As Long, _
  40.                            phkResult As Long) As Long
  41. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) _
  42.                              As Long
  43. Private Declare Function RegQueryValueExStr Lib "advapi32.dll" _
  44.     Alias "RegQueryValueExA" (ByVal hKey As Long, _
  45.                               ByVal lpValueName As String, _
  46.                               ByVal lpReserved As Long, _
  47.                               lpType As Long, _
  48.                               ByVal lpData As String, _
  49.                               lpcbData As Long) As Long
  50.  
  51. Public Function RegRead(ByVal lngHive As Long, _
  52.                         ByVal strKey As String, _
  53.                         ByVal strValue As String) As Variant
  54.     Dim intIdx As Integer, intHK As Integer
  55.     Dim strWork As String
  56.     Dim lngRet As Long, lngLen As Long, lngHKey As Long, lngType As Long
  57.  
  58.     RegRead = Null
  59.     strKey = strKey & Chr(0)
  60.     lngRet = RegOpenKeyEx(lngHive, strKey, 0, conKeyRead, lngHKey)
  61.     If lngRet = conOK Then
  62.         'Create buffer to store value
  63.         strWork = Space(255)
  64.         lngLen = 255
  65.         lngRet = RegQueryValueExStr(lngHKey, _
  66.                                     strValue, _
  67.                                     0&, _
  68.                                     lngType, _
  69.                                     strWork, _
  70.                                     lngLen)
  71.         RegRead = Left(strWork, lngLen - 1)
  72.         If Len(RegRead) = 254 Then RegRead = Null
  73.         'Close key
  74.         Call RegCloseKey(lngHKey)
  75.     End If
  76. End Function

Reply
ADezii's Avatar
ADezii
Expert
3,864 Posts
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 )
  1. Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
  2.                        (ByVal lpBuffer As String, nSize As Long) As Long


Code: ( text )
  1. Dim strBuffer As String
  2. Dim lngPadding As Long
  3. Dim strUserName As String
  4.  
  5. lngPadding = 255
  6.  
  7. strBuffer = Space(lngPadding)
  8.  
  9. If GetUserName(strBuffer, lngPadding) > 0 Then
  10.   strUserName = Left(strBuffer, lngPadding)
  11.   Debug.Print "User Name: " & strUserName
  12. End If

Reply
smugcool's Avatar
smugcool
Member
80 Posts
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 )
  1. Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
  2.                        (ByVal lpBuffer As String, nSize As Long) As Long


Code: ( text )
  1. Dim strBuffer As String
  2. Dim lngPadding As Long
  3. Dim strUserName As String
  4.  
  5. lngPadding = 255
  6.  
  7. strBuffer = Space(lngPadding)
  8.  
  9. If GetUserName(strBuffer, lngPadding) > 0 Then
  10.   strUserName = Left(strBuffer, lngPadding)
  11.   Debug.Print "User Name: " & strUserName
  12. 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.

Reply
NeoPa's Avatar
NeoPa
Administrator
10,384 Posts
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.

Reply
Reply
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