Results 1 to 8 of 8
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862

    breaking a string down

    Example String of the "Nets" column:

    [SW1|GARS2::GARS1]
    [OS::SW1|GARS1]
    [OS::GARS1] [SRW2::GARS2|SW1]


    [OS::SW1|GARS1] [SW1|GARS2::SW2]


    Or any iteration of the above

    How do I pick out the string beside the |?

    SW1|GARS2 to SW1 and GARS2 (2 Columns)

    I can get to it from here: H1: IIf([Nets]<>"",Mid(Left([Nets],InStr([Nets],":")-1),2,20),"")

    Via: H1a: IIf([H1] Like "*|*",Left([H1],InStr([H1],"|")-1),[H1])
    And: H1b: IIf([H1] Like "*|*",Mid([H1],InStr(1,[H1],"|")+1))

    But I would like to use 1 less column and get straight to it

    Thanks

  2. #2
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862
    1 down 3 to go:
    H1a: IIf(Left([Net],InStr([Net],":")-1) Like "*|*",Replace(Left(Left([Net],InStr([Net],":")-1),InStr([Net],"|")-1),"[",""),Mid(Left([Net],InStr([Net],":")-1),2,20))

    If there is a more elegant way to do this......

    Thanks

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I would probably write UDFs to do this instead of trying to use calculated fields.
    As you have shown, it is possible to do, but often very messy and difficult to maintain.
    In the UDFs, you can iterate through each characters in the string easily and determine start and stop points.

  4. #4
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862
    Joe,
    Whats an UDF? User Defined Feature/Function? How do I go about it?

    Update: Found what you mean by UDF. But it looks like you'd just put the same code in a different spot, and have a column call up in the query.


    Like this?
    Function R1CH1()
    If [Nets] <> "" Then
    R1CH1 = Mid(Left([Nets], InStr([Nets], ":") - 1), 2, 20)
    End If
    End Function

    Then I put this in the query as test: R1CH1() - I get undefined function.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Whats an UDF? User Defined Feature/Function? How do I go about it?
    Sorry, I saw that your title was "Expert", so I thought that you would be familiar with VBA and that term already.

    But it looks like you'd just put the same code in a different spot, and have a column call up in the query.
    No, you cannot use loops in a function without VBA (UDF). With a User Defined Function, you create your own function. You can then use it in Queries and Calculated Fields like other functions.

    In your original post, you posted 4 different example strings. If you can let me know what you expect to get back for each one of those, I can probably help you write the function.
    You may also want to add some other examples/expected outputs to represent other scenarios (i.e. is it possible to have some entries without a "|"? If so, what is the expected output of that?)

  6. #6
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862
    Shown as expert because I have a LOT of questions. I am am much better off than when I started through what I have learned on here. I have not done many Modules at all other than VBA on the forms or reports etc. Stand alone modules are somewhat new. I have done 2 with strings for signature block and date format. I could put in a function like? It's not looping:

    R1CH1 = Radio 1 Channel 1
    R1CH2 = Radio 1 Channel 2
    R2CH1 = Radio 2 Channel 1
    R2CH1 = Radio 2 Channel 2

    A = left side of Pipe (Subset of Channel)
    B = right side of Pipe ((Subset of Channel))

    Examples:

    [Nets] = "[SW1|GARS2::GARS1]"

    R1CH1 = SW1|GARS2
    R1CH1A = SW1
    R1CH1B= GARS2

    R2CH1 = GARS1


    [Nets] = "[OS::GARS1] [SW2::GARS2|SW1]"

    R1CH1 = OS
    R1CH2 = GARS1

    R2CH1 = SW2

    R2CH2 = GARS2|SW1
    R2CH1A= GARS2
    R2CH1B= SW1


    [Nets] = "[OS::SW1|GARS1] [SW1|GARS2::SW2|GARS4]"

    R1CH1 = OS

    R1CH2 = SW1|GARS1
    R1CH2A = SW1
    R1CH2B = GARS1

    R2CH1 = SW1|GARS2
    R2CH1A = SW1
    R2CH1B= GARS2

    R2CH2= SW2|GARS4
    R2CH2A = SW2
    R2CH2B= GARS4




    Code:
    Option Compare Database
    Option Explicit
    Function R1() As String
    If [Nets] <> "" Then
     R1CH1 = Mid(Left([Nets], InStr([Nets], ":") - 1), 2, 20)
    End If
    End Function
    Function R2() As String
    If [Nets] <> "" Then
     R1CH2 = Mid(Mid(Left([Nets], InStr([Nets], "]") - 1), 2, 20), InStr(1, [Nets], ":") + 1)
    End If
    End Function
    I get an external name not defined for [Nets]. This is multiple queries so how do I define it? (multiple queries because of the amount fo radio nets and different radios)


    Does this provide better clarification of my intentions?


    Thanks
    T

  7. #7
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you are just rewriting your existing formula, pretty much "as-is" within the UDF, then there really isn't much point in writing a UDF (just use the formula). UDFs are more helpful useful when you have formulas that tend to be very long or messy, or you just cannot envision a way to write it without the use of VBA, where you can take advantage to things like having the ability to use loops.

    Let me show you how I would write one of the functions in your example as a UDF.

    In your original example [SW1|GARS2::GARS1], you wanted to return GARS2 in your second column (the value between the "|" and the first ":").
    Granted, this can be done fairly easily with a function and probably doesn't require a UDF, but I will just use it to show you a few different ways that this could be done using a UDF, so you can begin to see how using UDFs and VBA may be advantageous in certain situations.

    The first one is pretty straightforward. Just locate the "|" and ":" and take everything in between. That code might look something like this:
    Code:
    Function GetSecondWord(myString As String) As String
    
        Dim startChar As Long
        Dim endChar As Long
    
    '   Check to see if length of entry is greater than zero
        If Len(myString) > 0 Then
    '       Find location of first "|"
            startChar = InStr(myString, "|") + 1
    '       Find location of first ":"
            endChar = InStr(myString, ":") - 1
    '       Extract characters between the start and end
            GetSecondWord = Mid(myString, startChar, endChar - startChar + 1)
        End If
        
    End Function
    Now, for more complex functions, you may actually need or want to loop through all the characters of the string, one-by-one. This example wouldn't really require it, but I will show you how to do it that way for illustration purposes. Essentially, what we are going to do is loop through it, and when we find the "|", we will indicate that the string has started, and we will keep adding to it one character at a time until we find the ending character (":"). That code may look something like this:
    Code:
    Function GetSecondWord2(myString As String) As String
    
        Dim i As Long
        Dim myLen As Long
        Dim curChar As String
        Dim stringStart As Boolean
        Dim workingString As String
    
    '   Default setting to start
        stringStart = False
    
    '   Check to see if length of entry is greater than zero
        myLen = Len(myString)
        If myLen > 0 Then
    '       Loop through each character in string
            For i = 1 To myLen
    '           Capture current character
                curChar = Mid(myString, i, 1)
    '           If current character is "|", mark the string is about to start
                If curChar = "|" Then
                    stringStart = True
                Else
    '               If the current character is ":", string is done so exit loop
                    If curChar = ":" Then
                        Exit For
                    Else
    '                   If string is started and not at end, add current character to working string
                        If stringStart = True Then
                            workingString = workingString + curChar
                        End If
                    End If
                End If
            Next i
    '   Set final working string to value to return
        GetSecondWord2 = workingString
        End If
        
    End Function
    Obviously, the first one is preferable and more efficient, as it doesn't involve loops. But if you run in to some complex situations, you may need to do something like the second example.

    Also note that these functions are "scaled" down, and you might need/want to add more checks or error handling (like what if a string does have a starting or ending character, etc), as your situation dictates. But hopefully this helps give you a "blueprint" of how you can write UDFs to handle some complex or messy function writing.

    To me, I find UDFs a lot easier to follow, maintain, and update than long, complex messy formulas. And if it is a function you will be re-using over and over again, if you ever need to change it, you only need to change it in one place, as opposed to formulas that you have written throughout your project (where you would need to update every single instance of that formula).

    Hope this helps!

  8. #8
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862
    Holy Crap!! Now that is an in depth answer. Thanks! I'll play with it and see the outcome.

    I can see what you have done in the simplified, nut in my case where it is looking for "[Nets]" is in several queries. "[Nets]" is the rename of the pertinent column I am looking for information on that radio. There sre several columns, 1 for each radio.

    Example:
    Radio1 = "Radio xyz WLS-1"
    Radio2 = "Radio xyz WLS-2"
    Radio3 = "Radio xyz WLS-3"
    Radio4 = "Radio ABC WLS-1"
    Radio5 = "Radio DEF WLS-1"

    They are all in 1 table though, NBOI. This rtable derives from an excel sheet made by another entity and i don't have control over how they develop it.

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

Similar Threads

  1. Breaking down a table with too many fields
    By maultiper in forum Database Design
    Replies: 3
    Last Post: 01-07-2016, 01:41 PM
  2. Database breaking
    By jj1 in forum Access
    Replies: 5
    Last Post: 11-25-2014, 02:17 PM
  3. Subreport Breaking Up Primary Report
    By Rustin788 in forum Reports
    Replies: 1
    Last Post: 07-23-2014, 10:14 AM
  4. Breaking Up table in FK and PK
    By drunkenneo in forum Programming
    Replies: 6
    Last Post: 12-07-2013, 12:59 AM
  5. Breaking the string into Text / Number
    By Amerigo in forum Queries
    Replies: 15
    Last Post: 05-20-2011, 03:29 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