Results 1 to 3 of 3
  1. #1
    access#1 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    1

    Mid function

    I am trying to rearrange text contained in a mid function. my code is:



    Private Sub MaterialA_Reportable_Quantity_AfterUpdate()
    Dim USDOTlen As Integer

    If Me.MaterialA_Reportable_Quantity = False Then
    USDOTlen = Len(Me.MaterialA_US_DOT_Description)
    Me.MaterialA_US_DOT_Description = Mid(Me.MaterialA_US_DOT_Description, 7, USDOTlen - 6)
    Else
    Me.MaterialA_US_DOT_Description = Chr(34) & "RQ" & Chr(34) & ", " & Me.MaterialA_US_DOT_Description
    End If

    End Sub


    The print result is: "RQ", Mercury Chloride, 6.1 un1624 PGII

    I want to read: "RQ", 6.1 un1624 PGII, Mercury Chloride

    How do I rearrange the mid function to do this?

    Thanks,

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Try this -
    Code:
    Private Sub MaterialA_Reportable_Quantity_AfterUpdate()
       Dim USDOTlen As Integer
       Dim intComma As integer 
       Dim strUSDot1 As String
       Dim strUSDot2 As String
       If Me.MaterialA_Reportable_Quantity = False Then
          ' Drop the first six characters
          Me.MaterialA_US_DOT_Description = Mid(Me.MaterialA_US_DOT_Description & "", 7)
       Else
          ' Add "RQ" and if there is a comma in the USDOT Description
          ' then reverse the order of the two parts of the USDOT Description
          USDOTlen = len(Me.MaterialA_US_DOT_Description & "")
          intComma = Instr( , Me.MaterialA_US_DOT_Description & "", ",")
          If intComma = 0 Then 
              Me.MaterialA_US_DOT_Description = Chr(34) & "RQ" & Chr(34) & ", " & Me.MaterialA_US_DOT_Description
          Else
              strUSDot2 = left(Me.MaterialA_US_DOT_Description, intcomma - 1)
              strUSDot1 = IIF(USDOTlen>intcomma,trim(right(Me.MaterialA_US_DOT_Description, USDOTlen - intcomma)) & ", ","")
              Me.MaterialA_US_DOT_Description = Chr(34) & "RQ" & Chr(34) & ", " & strUSDot1 & strUSDot2
           End If
       End If
    End Sub
    I've explicitly handled the possibilities of a Null field, no comma in the field, and a comma at the very end of the field. I removed the last parameter from your Mid calculation; since "to the end of the input field" is the default, you don't have to calculate the remaining length.

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Do you want to do this only if MaterialA_Reportable_Quantity > 0? (I don't see why you are using "False" in your If statement, except for the fact that "False" is zero.)

    From what I can see, if MaterialA_Reportable_Quantity > 0, you want to reverse the two parts of the description, which are separated by a comma. Something like this should work:

    Dim CommaPos as integer, strTemp as string

    strTemp = Me.MaterialA_US_DOT_Description
    CommaPos = instr(strTemp, ",")
    Me.MaterialA_US_DOT_Description = Chr(34) & "RQ" & Chr(34) & ", " & trim(mid(strTemp,CommaPos+1)) & ", " & left(strTemp, CommaPos - 1)

    Please clarify if I have misunderstood your logic.

    HTH

    John

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

Similar Threads

  1. Using the IIf Function?
    By djclntn in forum Queries
    Replies: 14
    Last Post: 03-19-2012, 03:50 PM
  2. VBA Function
    By antoncata in forum Programming
    Replies: 16
    Last Post: 02-11-2012, 07:50 PM
  3. HELP with IIF function
    By lpfluger in forum Queries
    Replies: 3
    Last Post: 04-01-2011, 12:01 AM
  4. if function
    By lolo in forum Queries
    Replies: 1
    Last Post: 08-01-2010, 11:38 PM
  5. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 AM

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