Avatar billede jensen363 Forsker
29. oktober 2020 - 12:01 Der er 12 kommentarer

Log af user i tabel

Dette er godt nok mange år siden jeg har prøvet dette, ... men nu kaster jeg mig over at lave en database igen :-)

Jeg har behov for at der i forbindelse med oprettelse og/eller ændring af rækker i en tabel bliver 'logget' hvem der foretager dette

Environ("username") kan vist ikke benyttes mere .... korrekt ???

Hvad er alternativet ????
Avatar billede zuschlag Juniormester
29. oktober 2020 - 12:40 #1
Hej Jensen
Environ("username") fungerer vist fint.
VH Anders
Avatar billede jensen363 Forsker
29. oktober 2020 - 12:52 #2
Ikke som inputvalue i tabeller, - kun som visning :-(
Avatar billede martens Guru
29. oktober 2020 - 12:56 #3
Jeg benytter nedenstående for at finde aktuel bruger .. Virker på såvel Domæne samt standalone PC´er
----------------------------------------------------
opret en VBA funktion

Public Function GetUserName() As String
      GetUserName = CreateObject("WScript.Network").UserName
End Function

og kald funktionen :
=GetUserName()

---
mvh martens
Avatar billede terry Ekspert
29. oktober 2020 - 12:59 #4
Ikke som inputvalue i tabeller? There isnt any default values you can use directly on the table, youll need to implement your own methods to do that.

There is also an API you can use to get the currentky logged in user
http://theaccessweb.com/api/api0008.htm
Avatar billede jensen363 Forsker
29. oktober 2020 - 13:02 #5
I tried to use this ( suggested by Terry ) :

   
API: Get Login name

Author(s)
Dev Ashish


(Q) How do I retrieve the UserName with which the user is logged into the network?

(A) Paste the following code in a new module and call the function fOSUserName.

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

When I insert the code in a module, these two lines is shown in red :

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Avatar billede terry Ekspert
29. oktober 2020 - 13:10 #6
This works
Option Compare Database
Option Explicit

Private Const sModName = "mod_Utilities" 'Application.VBE.ActiveCodePane.CodeModule

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
   
Private Declare Function apiGetComputerName Lib "kernel32" Alias _
    "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
   
   
   
   
   
'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Function fosusername() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If (lngX > 0) Then
        fosusername = Left$(strUserName, lngLen - 1)
    Else
        fosusername = vbNullString
    End If
End Function
'******************** Code End **************************
Avatar billede terry Ekspert
29. oktober 2020 - 13:14 #7
The code you show works fine, no idea why ist not working
Avatar billede jensen363 Forsker
29. oktober 2020 - 13:15 #8
Still same issue

I'm using 64 Bit ...

The error message claim that the function needs to be updated to 64 Bit
Avatar billede terry Ekspert
29. oktober 2020 - 13:15 #9
try martens, that works too
Avatar billede terry Ekspert
29. oktober 2020 - 13:17 #10
64 bit access? Not a good idea...
Avatar billede terry Ekspert
29. oktober 2020 - 13:23 #11
I cant test this but try changing the declaration

Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long
Avatar billede terry Ekspert
29. oktober 2020 - 13:26 #12
https://vba.miraheze.org/wiki/Access_fOSUserName_function

There is no need to use Access 64 bit just because you use Windows 64 bit, and I doubt you will experience performance improvements by using Access 64 bit, but you can be sure you will experience other issues.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester