Results 1 to 6 of 6
  1. #1
    marisacoul is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    27

    Cool Setting Default Value as User-Defined Function

    Hi there,

    I have a function that gets the name of the user, and it works well:



    Public Function GetUserName() As String
    GetUserName = CreateObject("WScript.Network").UserName
    End Function

    I've placed =GetUsername() in a textbox on my form and it comes up, which is great. But how do I make the username a default value in the table? I've tried placing =Getusername() in the default value slot of the field in design view but it says it can't find the function.

    I understand it's difficult to make a user-defined function a default value. Is there any way around this?

    Marisa

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is there a field in the table where you want to place the UserName?

  3. #3
    marisacoul is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    27
    Hi there,

    Yes! It's my "User" field on the table.

    Marisa

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    There are several ways to accomplish this. I would use the BeforeUpdate event of the form and just put code like:
    Me!User = Me.YourTextBoxName.Value
    This will fill in the "User" field just before updating a record.
    You may wish to check the firld for a value first in case it already has a value.

  5. #5
    marisacoul is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    27
    Thanks! That works

    Marisa

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Great! I marked this thread as Solved for you.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Setting default with an IIF Function
    By cptdave1996 in forum Access
    Replies: 14
    Last Post: 12-14-2015, 01:57 PM
  2. User Defined IIf Function
    By EddieN1 in forum SQL Server
    Replies: 6
    Last Post: 09-22-2015, 11:38 AM
  3. How to use User Defined Function in VBA
    By sbehera in forum Programming
    Replies: 7
    Last Post: 05-12-2014, 07:38 AM
  4. Replies: 9
    Last Post: 10-12-2013, 07:19 AM
  5. Query using a User defined function for dates
    By RonanM in forum Programming
    Replies: 4
    Last Post: 06-16-2011, 04:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums