Results 1 to 4 of 4

Dlookup based on a environ Username

  1. #1
    Carloj is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    22

    Dlookup based on a environ Username

    Im trying to let users open specific forms. Like the admin can open all and got usertype "1" and the operator got usertype "4" and can only open the orders. Now my idea was to lookup the username of the PC like:
    Code:
    Me.TekstLogin = Environ("USERNAME")
    This gives for me the output in textbox TekstLogin : Car. Now i got a table with username and usertype and want to lookup the usertype based on Tekstlogin so i can put if statements on formbuttons (if usertype = 2 ; open ; do not open).

    The problem i got is the dlookup. im using this code:


    Code:
    Private Sub Form_Open(Cancel As Integer)
    Me.TekstLogin = Environ("USERNAME")
    Tekst454 = DLookup("[UserType]", "tblUser", "[Username]=" & [TekstLogin])
    End Sub
    This gives an error on the dlookup line. Any idea how i can do this and what im doing wrong?

  2. #2
    Minty is online now Hacker
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    933
    You aren't looking up the correct item, in fact [TekstLogin] doesn't exist. Try setting a variable, and I would do this on form load not form open.


    Code:
    Dim sUser as String
    Dim iUserType as Integer
    
    sUser = Environ("USERNAME")
    
    
    Me.TekstLogin = sUser
    
    iUserType = DLookup("UserType", "tblUser", "[Username]='" & sUser & "'") ' This assumes your user type is a number not text
    
    Me.Tekst454  = iUserType
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Ajax is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    6,313
    you can simplify the code

    iUserType=nz(DLookup("UserType", "tblUser", "[Username]='" & Environ("USERNAME") & "'"),0)

    this will return null if username is not in tblUser, hence using the nz function

  4. #4
    Carloj is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    22
    Thanks alot. Got this working code now:
    Code:
    Private Sub Form_Open(Cancel As Integer)Dim sUser As String
    Dim iUserType As Integer
    
    
        Set objAD = CreateObject("ADSystemInfo")
        Set objUser = GetObject("LDAP://" & objAD.UserName)
        sUser = objUser.DisplayName
    
    
    
    
    Me.TekstLogin = sUser
    
    
    iUserType = Nz(DLookup("UserType", "tblUser", "[User_gebruiker]='" & sUser & "'"), 1)
    
    
    Me.Tekst_UserType = iUserType
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 10-10-2017, 05:52 PM
  2. Query Based on Username
    By whisp0214 in forum Queries
    Replies: 4
    Last Post: 09-18-2017, 01:49 PM
  3. Replies: 8
    Last Post: 08-13-2015, 10:08 AM
  4. Replies: 1
    Last Post: 02-17-2013, 03:46 PM
  5. Environ("username") ---> #Name? Error
    By KiEESH in forum Forms
    Replies: 2
    Last Post: 02-15-2013, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums