Results 1 to 6 of 6
  1. #1
    tkilback is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Canada
    Posts
    3

    Convert Formula to String without Evaluating

    I need to do is convert a formula to a string, without evaluating it first



    ie.

    cstr(2-3/8) converts to 1.625, when what I need it to be is "2-3/8"

    Any ideas?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Where is 2-3/8 coming from?
    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
    tkilback is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Canada
    Posts
    3
    Its a user entered value representing inches; its entered into a table with a field type string/text.
    User has options to enter 2.375, 2-3/8, or 2 3/8, all should evaluate to 60.33mm in the function, but 2-3/8 does the math, then converts to 41.28mm

    The format is borrowed from Autocad, and is familliar to all users of this database, so I'd like to keep it.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Textbox bound to text field, then the value should be treated as text string. Where are you using it that does calc? Post the function code.

    Are they entering/selecting from a combobox?
    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
    tkilback is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Canada
    Posts
    3
    The values are entered either on a textbox on the main form, or on a subform in datasheet view. (Both of which are bound to text fields)

    They're passed into the following function as strImpIn and the ouput is added to a double field.

    Code:
    Public Function getMetric(strImpIn As String) As Double
    Dim Result As Double
    
    If InStr(CStr(strImpIn), "-") = 0 Then
        Result = CDbl(strImpIn) * 25.4
    Else
        Dim wholeNum, leftFraction, rightFraction, decNum As Double
        Dim dashLoc, slashLoc As Integer
        
        dashLoc = InStr(CStr(strImpIn), "-")
        slashLoc = InStr(CStr(strImpIn), "/")
        
        wholeNum = CDbl(Left(CStr(strImpIn), dashLoc - 1))
        leftFraction = CDbl(Mid(strImpIn, dashLoc + 1, CInt((Len(strImpIn) - slashLoc))))
        rightFraction = CDbl(Right(strImpIn, (Len(strImpIn) - slashLoc)))
        decNum = CDbl(leftFraction / rightFraction)
        
        Result = Round((wholeNum + decNum) * 25.4, 2)
        
    End If
    
    getMetric = Result
    
    End Function
    Last edited by tkilback; 05-15-2015 at 02:42 PM. Reason: Missing some info

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I tested your function with: getMetric("2-3/8")

    It returns 60.32.

    However, unbound textbox with default value of 2-3/8 displays 1.625 and returns 41.275 when referenced in function. BUT, with default value of "2-3/8" (including the quote marks) returns 60.32.

    Then I tried binding textbox to text field and entered value of 2-3/8. Function returns 60.32.

    I cannot replicate the issue with bound textbox.

    Why don't you use combobox for input? It can have columns that show the English and metric equivalents but save the metric if that's what you want. No function needed to convert. That's what I've done.

    Be aware that type for variables must each be individually declared or will default to Variant type.
    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. Convert String to Date in Web App
    By carlo76 in forum SharePoint
    Replies: 3
    Last Post: 03-06-2015, 11:05 AM
  2. Convert rows into string
    By KPABA in forum Queries
    Replies: 1
    Last Post: 06-06-2013, 06:03 PM
  3. How to convert Date to String?
    By thebaul in forum Access
    Replies: 1
    Last Post: 08-01-2012, 05:51 AM
  4. Replies: 5
    Last Post: 12-14-2011, 08:24 AM
  5. Convert string to date
    By ~SwAmPdOnKeY~ in forum Queries
    Replies: 4
    Last Post: 09-11-2008, 07:19 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