By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,190 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,190 IT Pros & Developers. It's quick & easy.

String Search and Add to

P: 50
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....
Jan 30 '08 #1
Share this Question
Share on Google+
5 Replies


9815402440
100+
P: 180
hi

use inStr() function

usage:

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

regards
manpreet singh dhillon hoshiarpur
Jan 30 '08 #2

P: 50
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....
Jan 30 '08 #3

P: 58
The following code was done in VBA.

Expand|Select|Wrap|Line Numbers
  1. Sub Test()
  2.    Debug.Print Fix_Path("c:text.txt")
  3.    Debug.Print Fix_Path("c:\text.txt")
  4. End Sub
  5.  
  6. Private Function Fix_Path(file_path) As String
  7.    Dim regex As Object
  8.  
  9.    Set regex = CreateObject("VBScript.RegExp")
  10.    regex.Global = True
  11.    regex.Pattern = ":\\*"
  12.    Fix_Path = regex.Replace(file_path, ":\")
  13. End Function
  14.  
Jan 30 '08 #4

P: 58
Or,

Expand|Select|Wrap|Line Numbers
  1. Sub Test()
  2.    Dim file_path As String
  3.  
  4.    file_path = "c:text.txt"
  5.    Debug.Print Replace(file_path, ":", ":\")
  6. End Sub
Jan 30 '08 #5

Expert 5K+
P: 8,434
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".
Jan 30 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.