Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB 6 Loop Problem

Options
  • 23-06-2008 10:33am
    #1
    Registered Users Posts: 1,987 ✭✭✭


    i having an issue with a loop if anyone can help, the loop below is to take out extra commas at the end of address or to replace double commas in an address but its not working properly, its cutting off some of the address, any help appreciated.
    For i = Len(AddressLines) To 1 Step -1
      If Mid(AddressLines, i, 1) = "," Then
         If Mid(AddressLines, i - 1, 1) = "," Then
            Comma = i - 1
         End If
      End If
    Next i
    i = 0
    AddressLines = Left(AddressLines, Comma)
    

    Sample Address:
    11, walking lane,, street, , ,
    which should be
    11, walking lane, street


Comments

  • Registered Users Posts: 901 ✭✭✭EL_Loco


    the function "left" chops the string from the point you've set. so you're chopping your string from the point you've called "Comma"

    maybe you should have 2 strings, and as you step through your first string only write things to the 2nd string as they pass your critera. eg. only a certain amount of commas, and no commas in a row.

    EDIT: how are you on arrays? the split function could also be helpful here.


  • Registered Users Posts: 199 ✭✭DecTenToo


    Pity it's not VB .net as a regex would do the job
    function endComma(x)
    return x.replace(/,*$/,'')
    

    Then again http://www.regular-expressions.info/vb.html may be of interest


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    There are a couple of problems with your loop. First you dont initialise the variable Comma. Secondly your algorithm makes no sense at all. Your problem is with design not implementation. Have you worked through this loop on paper yet? If you cant get it to work on paper you will never get it work in code.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Would a simple replace do... say str = Replace(str,",,",",")

    Replace all double ,, with single ones


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    Ginger wrote: »
    Would a simple replace do... say str = Replace(str,",,",",")

    Replace all double ,, with single ones

    except for not getting rid of the comma at the end this would work


  • Advertisement
  • Registered Users Posts: 2,931 ✭✭✭Ginger


    quick check at the end to see if there is a comma and remove it..


Advertisement