Using network login to populate combobox on a form

I am using this code to populate  a combobox on a form:

'******************** 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 **************************

The combobox IS getting the user's login but it doesn't appear to be doing anything with it.

My onopen event of the form is:

Me.cboEmployee = fOSUserName

The form itself is a password form which looks for the username and then password.  When I accept the username that has been populated and then enter the password it is as if the populated value in the combobox hasn't been entered.  If I manually enter the username it works fine.

???

--Steve
SteveL13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbizupCommented:
The code you posted only populates the combo box.

Manual data entry triggers events such as the after update event.

If that is what you are trying to simulate, you will have to explicitly call that event from your open event after populating the combo.

Me.cboEmployee_after update()
0
mbizupCommented:
Sorry... the call should not have a space it should be

Me.cboEmployee_AfterUpdate()

(Ignore any spaces added by my cellphone)
0
SteveL13Author Commented:
I guess I don't understand,  Here is what I have in my afterupdate event of the combobox but it isn't working.

Private Sub cboEmployee_AfterUpdate()

    Me.cboEmployee_AfterUpdate ()

    Me.txtName = Me.cboEmployee.Column(0)
    Me.txtLevel = Me.cboEmployee.Column(2)
    Me.txtInitials = Me.cboEmployee.Column(3)

End Sub
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

SteveL13Author Commented:
Also tried:

Private Sub cboEmployee_AfterUpdate()

    Me.cboEmployee_AfterUpdate (fOSUserName)

    Me.txtName = Me.cboEmployee.Column(0)
    Me.txtLevel = Me.cboEmployee.Column(2)
    Me.txtInitials = Me.cboEmployee.Column(3)

End Sub
0
mbizupCommented:
Sorry... what I meant is in your open event:

Me.cboEmployee=fosUserName
Me.cboEmployee_afterupdate()


Dont change the after update event...just CALL it from the open event.
0
SteveL13Author Commented:
In the onopen event of the form I have:

    Me.cboEmployee = fOSUserName
    Me.cboEmployee_AfterUpdate()
    Me.txtPassword.SetFocus

But the line that reads:

Me.cboEmployee_AfterUpdate()

turns red and indicates it need an = sign

??
0
mbizupCommented:
Try droppi
0
mbizupCommented:
Try dropping the parentheses ().
0
SteveL13Author Commented:
Then the form doesn't even open.  I just get a blank screen.  When I comment out the:

Me.cboEmployee_AfterUpdate

The form open fine but the combobox contents still don't appear to be "activated".
0
mbizupCommented:
Hmm... is the idea here to populate fields in an Unbound form (calling the after update event should do that)... or are you trying to filter a bound form according to the network login? Ie: open the form to a specific record?
0
SteveL13Author Commented:
Am trying to populate fields in an Unbound form.
0
mbizupCommented:
Okay... try this in your Open Event:


Private Sub Form_Open()

    Dim i As Integer
    Dim s as string

    s = fosUserName

    For i = 0 To Me.cboEmployee.ListCount - 1
        Debug.Print Me.cboEmployee.ItemData(i)
        If Me.cboEmployee.ItemData(i) = s Then
        Me.cboEmployee.SetFocus
        Me.cboEmployee.ListIndex = i
        End If
    Next

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.