Link to home
Start Free TrialLog in
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag for Zambia

asked on

How to concatenate in VBA Ms Access

I want to concatenate the code below so that whoever is  loged in my software is addressed by the username, any idea? I want to add Environ("Username")
on the code below:



Private Sub Form_Activate()

'When the database is opened display welcome user message.


    If Time() < 0.5 Then
        [lblMorning].Visible = True
        [lblAfternoon].Visible = False
        [lblEvening].Visible = False

    ElseIf Time() > 0.5 And Time() < 0.75 Then
        [lblMorning].Visible = False
        [lblAfternoon].Visible = True
        [lblEvening].Visible = False

    ElseIf Time() > 0.75 Then
        [lblMorning].Visible = False
        [lblAfternoon].Visible = False
        [lblEvening].Visible = True
    End If
End Sub
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

there is an API call that will get you the username of the person logged into Windows.

You can find the code here:  http://theaccessweb.com/api/api0008.htm

Then, you would store those values in a table (users or employees or something like that) and use the value returned by the function to return the actual name of the user rather then their login username.
BTW, rather than having three separate labels, you might want to consider a single label, and then just change the Caption property of the label to reflect the time of day and the users name, based on the Time() value.
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER

All I need is just how to concatenate the code like

        [lblMorning].Visible = True & " "Environ("Username")

But is give me an error

Regards

Chris
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or:

Private Sub Form_Activate()

    ' When the database is opened display welcome user message.

    Dim Caption As String

    Caption = "Welcome " & Environ("Username") & "!"

    [lblMorning].Visible = False
    [lblAfternoon].Visible = False
    [lblEvening].Visible = False

    Select Case Time
        Case < #12:00:00#
            [lblMorning].Visible = True
             [lblMorning].Caption = Caption
        Case < #18:00:00#
            [lblAfternoon].Visible = True
            [lblAfternoon].Caption = Caption
        Case Else
            [lblEvening].Visible = False
            [lblEvening].Caption = Caption
    End Select

End Sub 

Open in new window

Environ can be spoofed, which is why many developers use the API call instead.

[lblMorning].Visible = True & " "Environ("Username")

The visible property of the control must have either a True or False value.  you might want to concatenate the username to the caption, with something like:

me.[lblMorning].Caption = me.[lblMorning].Caption & " " & Environ("UserName")