Results 1 to 5 of 5
  1. #1
    CurrentUser is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    3

    Trouble with string variable in custom vba function

    I have a custom function that returns the DSUM of a query field and want to use variables for the DSUM criteria.

    This function works and returns the corrects DSUM calculation in the immediate window, using ?MyInvVarNeg(#1/9/2014#)

    ---
    Public Function MyInvVarNeg(CurrentDay As Date)

    MyInvVarNeg = Int(Nz(DSum("[RecCount]", "qInvIndNegVar", "[RecDate]>#" & CurrentDay & "#-.001 and [SenderID]=CurrentUser()"), 0))

    End Function

    ---

    When I change the CurrentUser() function that's in the DSUM into a string variable, I get a DSUM of zero, which is incorrect, even though I declare the variable as the CurrentUser() function in the immediate window, ?MyInvVarNeg(CurrentUser(), #1/9/2013#)

    ---
    Public Function MyInvVarNeg(CurrentUseID as String, CurrentDay As Date)

    MyInvVarNeg = Int(Nz(DSum("[RecCount]", "qInvIndNegVar", "[RecDate]>#" & CurrentDay & "#-.001 and [SenderID]=CurrentUseID"), 0))

    End Function

    ---

    The [SenderID] field that I'm attempting to match is of the Text data type in the table that is queried, and is generated by another function and defined as a string in that function.

    What am I missing? Why does CurrentUser() work in this function when hardcoded, but I can’t seem to get it to work as a variable?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    CurrentUseID cannot be within quote marks. Concatentate the variable, just as you do with CurrentDay.

    Why create another variable anyway? Is it populated with the CurrentUser() function?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CurrentUser is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    3
    Thanks for the reply.
    I thought it would be a good idea to write a common function for users to see their personal variance in the main form (based on their user ID) and also use the same function in an admin query to display multiple users' variance, so the variable can be the current user function or a user ID field, depending on the circumstance.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What is 'personal variance'.

    This is resolved?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    CurrentUser is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    3
    In this case, variance is a difference in inventory from that which was verified as received. This is resolved.

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

Similar Threads

  1. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  2. Oracle custom connect string
    By nvrwrkn in forum Import/Export Data
    Replies: 23
    Last Post: 10-09-2012, 11:02 AM
  3. Extract Value from Variable in String
    By nguyenak in forum Programming
    Replies: 3
    Last Post: 05-24-2012, 03:50 PM
  4. Trouble With Variable holding Value
    By michaelb in forum Programming
    Replies: 3
    Last Post: 02-25-2011, 11:32 AM
  5. Using a string variable to specify a control
    By Gerry in forum Programming
    Replies: 3
    Last Post: 04-14-2010, 02:28 PM

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