Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2003
    Posts
    48

    Unanswered: Using a value from a form object as a default value in a table field?

    Hi All!!

    I have started working on a small database that uses a login system.

    I am trying to assign the CURRENTUSER value as the default value in a table field, this should provide a personalized 'stamp' on the records created by each user. However, I understand that I cannot use this expression directly within the table.

    Therefore, I was hoping I could take this value from a form textbox field that uses CURRENTUSER as a control source to display the name of the user logged in.

    Is it possible to use the value of a form object (a textbox) as a source for a default value in a table field?

    If so, please can you give an example of the code required and let me know where to put it.

    I would REALLY appreciate any advice on this as this problem is like an itch I can't scratch!!! Or of course if there is a better way to automatically 'tag' a new record with the name of the Current user, I'd love to hear about that!!!

    Cheers,

    Luke
    Last edited by Lukasx; 08-29-03 at 06:44.

  2. #2
    Join Date
    Mar 2003
    Location
    UK
    Posts
    71
    the best way to handel this is to create a global variable which is set to the user name as the users logs in.

    create a new module. In this module you will need the following code:

    Globale gUserName as string 'The variable you will hold the user name in

    Function Get_UserName() As String
    Get_UserName = gUserName
    End Function

    Sub Set_UserName(strUserName As String)
    gUserName = strUserName
    End Sub

    '#################################

    Then, when you want to set the username you use:

    Set_UserName ("testusername")

    and when you need to user the username you use:

    Get_UserName()

    Then whenever someone adds a record you can use the Get_UserName() to add their name to a username filed.

    There are 2 ways of doing this:

    either create a hidden filed on any data entery form and set this to Get_userName() on new records

    or

    after data has been changed use and UPDATE statment run from docmd.runsql and ammend the record just created adding the get_username to it.

  3. #3
    Join Date
    Aug 2003
    Posts
    48
    Thanks for this!!

    Can I just ask how I can apply this to the default value of the table field?

    (what do I type in there to access the string value from the code?)

    Sorry, I'm quite new to the more 'advanced' areas of Access, like VBA coding! :-)

    Cheers,

    Luke

  4. #4
    Join Date
    Mar 2003
    Location
    UK
    Posts
    71
    the easiest way is to use a form to enter the data. build a form (using the form wizard is fine) and in the filed that you want the user name to go in set the default vaule (n the properties) to:
    =Get_UserName
    ensure that this filed is locked and not enabled (so your users can't change it (unless you want them to be able to)) or you could make it hidden (set visable = false so they can't see it) and when they create a new record it will add the username for you.

Posting Permissions

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