[RESOLVED] Just Checking ..
if there is another way to do this .. or is this pretty much it?
The sentence can look like this .. item in bold is the text needed
05/24/06 16:00 -- Start working on #1
VB Code:
iPos = (InStr(sTemp, "-- ") + 3) ' START POINT
iPoe = InStr(iPos, sTemp, " ") ' END POINT
sTemp = Trim$(Mid$(sTemp, iPos, (iPoe - iPos))) ' GET TEXT
Re: [RESOLVED] Just Checking ..
As I've already mentioned earlier, I wouldn't use Split for this purpose. Even though it may look cool with a one-liner like the above it consumes more memory and runs much slower then the code you used from start.
Re: [RESOLVED] Just Checking ..
It was something I was previously unaware of though...could be useful.
Rory, if you parse words out of strings often, consider putting your code into a function...that way you use one line to call the 3 lines you need actioned...all you'd have to give the function is the original source string, start and end points and it should return the required word :-)
Re: [RESOLVED] Just Checking ..
Thats good to know also Joacim .. and smux Ill see if i can just put it in a function ..
Re: [RESOLVED] Just Checking ..
it shuold be as simple as "private function getparse(source as string, start as string, end as string) as string" with "getparse=results" (results would be whatever you call the string that you put the results into...to return results from a function you use {function name} = {results to return}
Re: [RESOLVED] Just Checking ..
;)
VB Code:
sTemp = StripText(sTemp, "-- ", " ")
'// CUT TEXT FROM STRING
Public Function StripText(ByVal source As String, _
ByVal start As String, ByVal finish As String) As String
Dim iPos As Integer, iPoe As Integer
iPos = (InStr(source, start) + Len(start))
iPoe = InStr(iPos, source, finish)
StripText = Trim$(Mid$(source, iPos, (iPoe - iPos)))
End Function
Re: [RESOLVED] Just Checking ..
*almost* right...iPos line...you have +3...it needs to be + len(start) as start length is variable length now :-)
Also, do you need trim$ on the mid$? If you NEED it and never need untrimmed data returned, stay with it...just remember if you use it in other projects (that's the beauty of subs...you can use them elsewhere easily) just remember you trim it too :-)
Re: [RESOLVED] Just Checking ..
yep your right there .. ;-)
i had that originally, took it out .. der yah go .. :-))
Actually i use common functions in VBscript more than i do in VB ..
but then vbscript doesnt give us as much to work with ..
Yeah i think we need Trim in this case ..
here's another i use all the time .. brought over from vbscript ..
i usually put them all in modCommon.bas,
theres tons of database related ones i use..
VB Code:
'// CHECK FILE EXISTS FSO METHOD
Public Function DoesFileExist(ByVal rFile As String) As Boolean
Dim objFso 'As FileSystemObject (reference)
On Error Resume Next
Set objFso = CreateObject("Scripting.FileSystemObject")
If (objFso.FileExists(rFile)) Then
DoesFileExist = True
End If
Set objFso = Nothing
End Function
Re: [RESOLVED] Just Checking ..
"doesfileexist" is a bit like a sledgehammer to crack a nut when you can just as easily use dir(filename) :-)
"if dir("c:\filename.txt") then" should work just as well, I believe :-P
Re: [RESOLVED] Just Checking ..
Dir wont work if there is no string .. if it is blank for example. like if you dont hard code it in there .. it will still return TRUE. It works if you know for sure you have entered the file path manually. Most cases it will be fine ..
example ...
VB Code:
Private sFile As String
Private Sub Command1_Click()
If Len(Dir$(App.Path & "\" & sFile)) Then
Debug.Print "Exists: " & App.Path & "\" & sFile
End If
End Sub
Private Sub Command1_Click()
If Len(Dir$(sFile)) Then
Debug.Print "Exists: " & sFile
End If
End Sub
Re: [RESOLVED] Just Checking ..
That's simply because if you pass an empty string to the Dir function it looks for any file in the current directory and returns the first it finds. In other words it's the same thing as doing Dir("*.*"). But you can easily code around that by first checking if the passed string actually contains anything (using the Len function). You should also use error checking in the function if you use the Dir function since it might raise error 53 (File not found). Just try passing a string that only contains a space to it.