String Search and Add to

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jarremw
    New Member
    • Jan 2008
    • 50

    String Search and Add to

    hello all
    i am writing a database app for my company, the problem i have now is that when i pull the file path from the database it looks like this c:text.txt when i need it to look like this c:\text.txt,, i know the \ is the escape character in mysql, when im pulling the file path from the db i put it into a label first then use that to execute the file, is there anyway i can search that string of text for say the : then add a \ behind it when it finds it so the file path will be correct, i was thinking something like an if statement would work, but i do not have any idea on how to set that up, so please just a little advice!!! please!

    thanks tons....
  • 9815402440
    New Member
    • Oct 2007
    • 180

    #2
    hi

    use inStr() function

    usage:

    if instr(1,"C:\Tex .text",":") > 0 then
    ': found
    endif

    regards
    manpreet singh dhillon hoshiarpur

    Comment

    • jarremw
      New Member
      • Jan 2008
      • 50

      #3
      so i tried that but how do i add in a "\" behind the ":"... the add in will be in the same place everytime if that helps...i really new to vb and nothing on google really explains an insert method....

      thanks tons....

      Comment

      • WinblowsME
        New Member
        • Jan 2008
        • 58

        #4
        The following code was done in VBA.

        [CODE=vb]
        Sub Test()
        Debug.Print Fix_Path("c:tex t.txt")
        Debug.Print Fix_Path("c:\te xt.txt")
        End Sub

        Private Function Fix_Path(file_p ath) As String
        Dim regex As Object

        Set regex = CreateObject("V BScript.RegExp" )
        regex.Global = True
        regex.Pattern = ":\\*"
        Fix_Path = regex.Replace(f ile_path, ":\")
        End Function
        [/CODE]

        Comment

        • WinblowsME
          New Member
          • Jan 2008
          • 58

          #5
          Or,

          [CODE=vb]Sub Test()
          Dim file_path As String

          file_path = "c:text.txt "
          Debug.Print Replace(file_pa th, ":", ":\")
          End Sub[/CODE]

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            It's important to note that this sort of unconditional replacement will only work properly if you can absolutely guarantee that the original string cannot have the backslash there already.

            If that's the case, then fine. Otherwise you will need your code to check before changing. It could be something simple like using the Mid() function to see whether you have ":" in the second position, and whether you have "\" in the third.

            Even though this is probably not a really serious case, it does highlight up a very serious point. In programming, it's vitally important to make a distinction between things which cannot happen, and things which won't happen. You have to follow Murphy's law and assume that if something can go wrong, it will (usually when you're not around to fix it). One of the easiest ways to introduce bugs in your code is to believe someone who tells you "oh it's alright, we would never do that".

            Comment

            Working...