Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Posts
    3

    Unanswered: Use Public Function UserName to populate Form field and bind it to table

    Hello,

    I have a database where the user's enter their Windows ID into a text
    box on a form. I would like to change it so that the field on the
    form is automatically populated and the user's cannot change it. I
    have a module with the below defined:


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


    Public Function UserName() As String
    Dim sName As String * 256
    Dim cChars As Long
    cChars = 256
    If GetUserName(sName, cChars) Then
    UserName = Left$(sName, cChars - 1)
    End If
    End Function


    The text box currently has the Control Source set to OperatorRef#
    which is in tblTempMeter. If I change the Control Source to
    =UserName() it shows the Windows ID of the user but it does not put
    that into the OperatorRef# field in tblTempMeter. How can I pass the
    Windows ID from the text box to the OperatorRef# field in
    tblTempMeter?


    Thanks for any help you can provide,


    DIDS

  2. #2
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    Keep the TextBox bound to the column of the table and use the Form_Open or Form_Current event sub to assign its value:
    Code:
    Private Sub Form_Current()
    
        Me.<TextBox>.Value = UserName
        
    End Sub
    Have a nice day!

  3. #3
    Join Date
    May 2011
    Posts
    3
    Hi Sinndho,

    I am getting "Compile Error: Method or data member not found". The VBA editor is highlighting ".OperRef#" in the statement

    Private Sub Form_Current()

    Me.OperRef#.Value = UserName

    End Sub


    The textbox Name on the Property sheet is "OperRef#" and the Control Source has been changed back to "OperatorRef#". Any idea as to what is wrong?


    Thanks,

    DIDS
    Last edited by DIDS; 05-27-11 at 08:35.

  4. #4
    Join Date
    May 2011
    Posts
    3
    Hi Sinndho,

    I discovered the issue. I needed to change the statement from:

    Me.OperRef#.Value = UserName

    to

    Me.OperRef_.Value = UserName

    even though the textbox is called OperRef# it needed to be coded with _ instead of #.

    Thanks,

    DIDS

  5. #5
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    You're welcome!

    This is because the # sign should not be used to name an object. Me.[OperRef#].Value would probably work too.
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •