Results 1 to 3 of 3
  1. #1
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    108

    Double to String without coercion

    Possibly more of a vba question, but it is in an Access app - so here goes.

    I have a Function with a return type of String.

    The function performs a lot of calculation with Double and Currency fields.

    If it can't complete the calculation it returns "***" (which is why String is the return type)

    But if the sum works then the return line is MyFunc = Round(wOrderLinePrice - (wOrderCost / 100), 2)
    where wOrderLinePrice is Double and wOrderCost is Currency.

    Now for 4 years this has always worked.(we always made a profit!!!!!) But last week the calculation should have returned -107.01, but in fact returned -9953.22

    Note there is no explicit coercion on my return line - should there be??

    e.g. MyFunc = CStr(Round(wOrderLinePrice - (wOrderCost / 100), 2))

    or MyFunc = Round(wOrderLinePrice - (wOrderCost / 100), 2).toString



    or is it something else??

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'd probably make the function return a variant, then in the code that called it, figure out what to to with the returned value based on its data type. Conversion functions will error if you try to pass values that can't be converted.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Must have something to do with the negative sign in a number treated as string. Agree Variant is probably better way to go.

    If you are using VBA, why do you show .tostring syntax?
    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.

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

Similar Threads

  1. Replies: 7
    Last Post: 01-17-2023, 01:48 PM
  2. Replies: 3
    Last Post: 07-20-2020, 01:04 PM
  3. Replies: 6
    Last Post: 06-01-2017, 03:25 PM
  4. Replies: 5
    Last Post: 05-10-2017, 12:10 PM
  5. Replies: 0
    Last Post: 10-22-2012, 02:45 PM

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