Avatar of Karen Schaefer
Karen Schaefer
Flag for United States of America asked on

Retrieve in User Login name update Combo

Having a issue with vba to auto populate a field(combo = id, Name, Email, LoginName.

Private Sub Approved_AfterUpdate()
If Me.Approved = True Then
    Me.Approved_by = DLookup("ID", "Users", "Login =" & "'" & GetUserName & "'")
    Debug.Print Me.Approved_by
End If
End Sub

Open in new window


Using the standatd GetUserName functiion.

The current code is erroring on the following "Cannot perform this operation"..

Any ideas.
Microsoft AccessVBA

Avatar of undefined
Last Comment
Karen Schaefer

8/22/2022 - Mon
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015

The built in Access 2003 function is CurrentUser not GetUserName.  This also requires using an .mdb and ULS.

Are you trying to get the  Current Windows user name?  If yes, try this: http://access.mvps.org/access/api/api0008.htm

Otherwise it would help to see the code for use are using for your "standard" GetUserName function
Karen Schaefer

ASKER
I am not using 2003 but 2010 - here is the get User function

Option Compare Database
Option Explicit

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function GetUserName() 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
        GetUserName = Left$(strUserName, lngLen - 1)
    Else
        GetUserName = vbNullString
    End If
End Function

Open in new window

Karen Schaefer

ASKER
I don't believe the issue is with the function but possibly with the Dlookup function syntax.  I am currently using the get user function elsewhere without issue.

K
Your help has saved me hundreds of hours of internet surfing.
fblack61
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015

What happens when you run the folowing in the immediate window (ctrl-g) of the VBA editor?

? DLookup("[ID]", "[Users]", "[Login] =" & "'" & GetUserName & "'")

Open in new window

Scott McDaniel (EE MVE )

You may be returning a null value, so wrap it in the Nz function:

Me.Approved_by = DLookup("ID", "Users", "Login ='" & Nz(GetUserName, "") & "'")

Also check spelling of Tables and Columns.
Karen Schaefer

ASKER
I have tried both suggestions and I am still getting the same error.

When I use the 1st suggestion it returns the value of 1 which is correct.

I also tried placing the NZ @ beginning of Dlookup statement - no difference.

I forgot to mention that the value of the "Approved by" is a combo and multiple selection option is on.

When I step thru the code the GetUserName works correctly, however setting the value to the correct field fails.

Private Sub Approved_AfterUpdate()
If Me.Approved = True Then
    'Me.Approved_by = DLookup("ID", "Users", "Login =" & "'" & Nz(GetUserName) & "'")
    Me.Approved_by = Nz(DLookup("ID", "Users", "Login ='" & Nz(GetUserName, "") & "'"))
    Debug.Print Me.Approved_by
End If
End Sub

Open in new window


Thanks,

K
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Scott McDaniel (EE MVE )

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015

What are you expecting to be return from the Dlookup?

Your DLookup is programmed to return the ID field from the table Users were the field Logon is equal to  the Windows User name.  Is that what you want?
Karen Schaefer

ASKER
the Id field is the value stored but not the value displayed - would that make any difference?
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015

What are you expecting to be return from the Dlookup?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Karen Schaefer

ASKER
here is the query behind the combo:

SELECT Users.ID, Users.Login, Users.FullName
FROM Users
ORDER BY Users.[FullName];

column count = 3
widths = 0";1";1"

On Select of the Approved checkbox I want to set the value = the current user  based on results from GetUserName function.  The Results format can contain multiple character types -  "a-username" or just Username.

Hope this helps.

k
Scott McDaniel (EE MVE )

What's the Bound Column of the Combo?

What is the ControlSource of the Combo?
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015

If the ID is appearing in the Me.Approved_by combo box then it is probably not bound to column 1.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Karen Schaefer

ASKER
column 1 is the bound column.

Control Source = Approved by
Karen Schaefer

ASKER
Here is a copy of property sheet for combo:
Property Sheet
Karen Schaefer

ASKER
User Table:
Screenshot.png
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Karen Schaefer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Karen Schaefer

ASKER
thanks for both of you for your input and time