Results 1 to 9 of 9

Thread: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

  1. #1

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Resolved [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    I am having a problem; I have a string as follows:

    FirstName,LastName,PostalCode,"some address, with comma", whatever


    I am using the split function to break it down into an array. The problem I am having, is the array comes out like this:

    FirstName
    LastName
    PostalCode
    "some address
    with comma"
    whatever



    The output I would like, is to have it like this

    FirstName
    LastName
    PostalCode
    "some address, with comma"
    whatever

    All addresses that contain commas will be encased with ".

    *edit*
    Sorry, It is not just the address that may contain commas, any field may. Although any field wit a comma that is not meant to be used as a delimiter will be encased in "'s.
    *end edit*

    I am reading the string in 1 line at a time before I actually split by the delimiter.

    Sorry I cant post any code, as I am at work atm..but pseudocode is

    VB Code:
    1. Do While NOT EOF
    2.   textLine = ReadLine (bleh.txt)
    3.   splitLinex = Split(textline,",")
    4. Loop

    Thanks,

    Smitty
    Last edited by kfcSmitty; Jun 16th, 2006 at 12:49 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Ingoring comma, where comma is the delimiter VBA (access)

    Well, you could first replace any comma between quotes with a semicolon or similar, split, then replace the semicolon. Maybe there's an easier way, but that idea pops up.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Ingoring comma, where comma is the delimiter VBA (access)

    Quote Originally Posted by salvelinus
    Well, you could first replace any comma between quotes with a semicolon or similar, split, then replace the semicolon. Maybe there's an easier way, but that idea pops up.

    Thanks for he fast reply, I was thinking along those lines, but I cant seem to figure out how to remove the comma only between the quotes.

    Maybe I am just blanking out

  4. #4

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    Figured it out

    VB Code:
    1. Option Compare Database
    2.  
    3. Private Sub Command0_Click()
    4. Dim txtSplitter As String
    5. 'txtSplitter = "Aditya Vinayak,,Naik,D/8 Jeevan Bahar," & Chr(34) & "21st Road, chembur" & Chr(34) & ",Mumbai,MAH,400071,India,[email protected]"
    6. txtSplitter = Chr(34) & ",,,,,,,,,,,,," & Chr(34) & ",test,test,test"
    7. Dim secSplit() As String
    8. secSplit = Split(txtSplitter, ",")
    9. txtSplitter = ""
    10. Dim i, tmpNum As Integer
    11. Dim tmpString As String
    12. For i = LBound(secSplit) To UBound(secSplit)
    13.  
    14.  
    15.   If InStr(secSplit(i), Chr(34)) Then
    16.  
    17.        tmpNum = tmpNum + 1
    18.    
    19.        tmpString = tmpString + secSplit(i)
    20.        
    21.        If tmpNum = 2 Then
    22.          tmpNum = 0
    23.          txtSplitter = txtSplitter & tmpString & ","
    24.        End If
    25.  
    26.      ElseIf tmpNum < 2 And tmpNum > 0 Then
    27.      
    28.          txtSplitter = txtSplitter & secSplit(i)
    29.      
    30.      Else
    31.  
    32.        txtSplitter = txtSplitter & secSplit(i) & ","
    33.  
    34.   End If
    35. Next
    36.  
    37. secSplit = Split(txtSplitter, ",")
    38.  
    39. ReDim Preserve secSplit(UBound(secSplit) - 1) As String
    40.  
    41. For i = LBound(secSplit) To UBound(secSplit)
    42.    MsgBox secSplit(i)
    43. Next
    44.  
    45. End Sub
    Last edited by kfcSmitty; Jun 16th, 2006 at 01:31 PM.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    An easier option, if you can do it, is to use a different delimiter. But that may be out of your hands.
    Tengo mas preguntas que contestas

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    I know you have done it but I thought I would post some options anyway.
    1) read through the source string.
    -- for each char, add to an output array or string variable.
    -- if the string starts with speech marks, enable a flag to so the sub knows that there are open quotes
    ---- keep adding to the output array/string until closing quotes or end of line
    -- close string/array item (move to next column/row as applicable)

    2) split the line by commas
    -- go through each entry and check the first and last chars.
    ---- if the first is speechmarks but the last is not, merge with the next column
    ---- as in (1) hold a variable to be set true/false when the speechmarks are found.

    I will have to be doing this soon too. as my work place have a couple of files to be imported into a db.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    Another way is to split along commas like you did originally, then concatenate splitLinex(3) & (4).
    Tengo mas preguntas que contestas

  8. #8
    New Member
    Join Date
    Jun 2006
    Posts
    7

    Re: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    go to www.mvps.org/access and search for findandreplace(). this function allows you to replace any character or substring in a string with anything else. I used it to replace the comma with " and", then reversed the process after the user clicked OK on the form.

  9. #9

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: [RESOLVED] Ingoring comma, where comma is the delimiter VBA (access)

    Thanks for all the helpful tips. I actually ended up changing the code, as this one would error if there was an odd number of quotations.

    The code i finally decided to go with was:

    VB Code:
    1. Function FindQuotes (strUserInfo As String) As String
    2.    
    3.     Dim strTemp() As Variant
    4.     Dim numQuotes As Integer
    5.     Dim strFinal As String
    6.    
    7.    
    8.     For i = 1 To Len(strUserInfo)
    9.         Redim Preserve strTemp(i)  As Variant
    10.         strTemp (i) = Mid$(strUserInfo, i, 1)
    11.         If strTemp(i) = Chr(34) Then
    12.             numQuotes = numQuotes + 1
    13.         End If
    14.         If numQuotes > 1 Then
    15.             numQuotes = 0
    16.         Elseif numQuotes > 0 Then
    17.             strTemp(i) = Replace(strTemp(i),",","")
    18.         End If
    19.         strFinal = strFinal + strTemp(i)
    20.     Next
    21.    
    22.    
    23.     FindQuotes = strFinal
    24.    
    25.    
    26. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width