(VB6) Slicing a string before a certain point - vb6

Suppose I have a variable set to the path of an image.
Let img = "C:\Users\Example\Desktop\Test\Stuff\Icons\test.jpg"
I want to slice everything before "\Icons" using Vb6. So after slicing the string it would be "\Icons\test.jpg" only.
I have tried fiddling with the Mid$ function in VB6, but I haven't really had much success. I am aware of the fact that Substring isn't a function available in vb6, but in vb.net only.

After the first \icons
path = "C:\Users\Example\Desktop\Test\Stuff\Icons\test.jpg"
?mid$(path, instr(1, path, "\icons\", vbTextCompare))
> \Icons\test.jpg
Or after the last should there be > 1
path = "C:\Users\Example\Desktop\Test\Icons\Stuff\Icons\test.jpg"
?right$(path, len(path) - InStrRev(path, "\icons\", -1, vbTextCompare) + 1)
> \Icons\test.jpg

This is pretty easy to do generically using the Split function. I wrote a method to demonstrate it's use and for grins it takes an optional parameter to specify how many directories you want returned. Passing no number returns a file name, passing a very high number returns a full path (either local or UNC). Please note there is no error handling in the method.
Private Function GetFileAndBasePath(ByVal vPath As String, Optional ByVal baseFolderLevel = 0) As String
Dim strPathParts() As String
Dim strReturn As String
Dim i As Integer
strPathParts = Split(vPath, "\")
Do While i <= baseFolderLevel And i <= UBound(strPathParts)
If i > 0 Then
strReturn = strPathParts(UBound(strPathParts) - i) & "\" & strReturn
Else
strReturn = strPathParts(UBound(strPathParts))
End If
i = i + 1
Loop
GetFileAndBasePath = strReturn
End Function

Related

unable to search character in a given string using VBScript?

I am trying to find whether the character is present in a given string or not but unable to search and increment value though it is present
Dim testchar,noOfSpecialChar
noOfSpecialChar=0
Dim specialChars
specialChars="*[#.^$|?#*+!)(_=-]."
for lngIndex = 1 to Len("test#123")
testchar = mid("test#123",lngIndex,1)
if((InStr(specialChars,testchar))) then
noOfSpecialChar=noOfSpecialChar+1
end if
next
The problem here is InStr() as highlighted in the documentation;
Returns the position of the first occurrence of one string within another.
We can use this knowledge to create a boolean comparison by checking the return value of InStr() is greater than 0.
Dim testString: testString = "test#123"
Dim testchar, foundChar
Dim noOfSpecialChar: noOfSpecialChar = 0
Dim specialChars: specialChars = "*[#.^$|?#*+!)(_=-]."
For lngIndex = 1 To Len(testString)
testchar = Mid(testString, lngIndex, 1)
'Do we find the character in the search string?
foundChar = (InStr(specialChars, testchar) > 0)
If foundChar Then noOfSpecialChar = noOfSpecialChar + 1
Next

How can I invert the case of a string in VB6?

I'm trying to make a program that can take the letters of a string and invert their case. I know that in vb.net there exists the IsUpper() command, but I don't know of such a thing in vb6.
What can I use in place of it?
Thanks!
Something like this should work:
Private Function Invert(strIn As String) As String
Dim strOut As String
Dim strChar As String
Dim intLoop As Integer
For intLoop = 1 To Len(strIn)
strChar = Mid(strIn, intLoop, 1)
If UCase(strChar) = strChar Then
strChar = LCase(strChar)
Else
strChar = UCase(strChar)
End If
strOut = strOut + strChar
Next
Invert = strOut
End Function
This loops through the supplied string, and extracts each character. It then tries to convert it to upper case and checks it against the extracted character. If it's the same then it was already upper case, so it converts it to lower case.
It handles non alpha characters just fine as UCase/LCase ignores those.

Symbol already defined differently VB

I'm trying to compile the following code, and I keep getting an error. I got this erro before multiple times so I was forced to use workaround functions. This time I'm really tired of this issue and I need to know what's wrong here.
sub SQL_AddTestResults (byval sData as string, byval testID as integer)
dim i as integer
dim dataChain as string
dim aData (Split(sData, ";").length) as string
aData = Split(sData, ";")
for i = 0 to aData.Length
if(i = 4) then
goto skip
elseif (i = 68) then
goto skip
elseif (i = 72) then
goto skip
end if
if(i = aData.length) then
dataChain = dataChain & aData(i)
else
dataChain = dataChain & aData(i) & ", "
end if
skip:
next
MsgBox (dataChain)
SQL_statement = "INSERT INTO ""TestData"" VALUES (" & dataChain & ");"
Stmt = connection.createStatement()
Stmt.executeUpdate(SQL_statement)
end sub
Compiling this code gives me the following error on "for i = 0 to aData.Length" line:
Basic syntax error.
Symbol aData already defined differently.
Have no idea why. Apologies if that's a trivial problem, but I'm completely new to VB. C++ didn't prepare me for this.
Arrays in classic VB don't have a "length" property. I'm not sure where you got that from.
The way to get the bounds of an array in classic VB is with the LBound and UBound functions.
for i = LBound(aData) to UBound(aData)
This way you can even handle arrays that don't have 0 as the starting index, as yes, one of VB's wonderful quirks is that it lets you use any range of numbers for your indexes.
VB6 isn't a language I'd recommend for new development. If you're trying to learn something new, there are plenty of other options. As you've no doubt noticed, it's harder and harder to find documentation on how classic VB does things, and how it differs from VBScript and VB.NET. If you need to be maintaining an older VB6 code base, I'd recommend finding a used book somewhere that goes over VB6 syntax and usage.
Try this code corrected code:
sub SQL_AddTestResults (byval sData as string, byval testID as integer)
dim i as integer
dim dataChain as string
dim aData as variant
aData = Split(sData, ";")
for i = 0 to ubound(aData)
if(i = 4) then
goto skip
elseif (i = 68) then
goto skip
elseif (i = 72) then
goto skip
end if
if(i = ubound(aData)) then
dataChain = dataChain & aData(i)
else
dataChain = dataChain & aData(i) & ", "
end if
skip:
next
MsgBox (dataChain)
SQL_statement = "INSERT INTO ""TestData"" VALUES (" & dataChain & ");"
Stmt = connection.createStatement()
Stmt.executeUpdate(SQL_statement)
end sub
What I could gather, you are defining aData twice but in different ways -
dim aData (Split(sData, ";").length) as string
aData = Split(sData, ";")
aData length will return an integer of the actual length whilst you are asking it to return a string, and you are using it in your integer loop for i as counter.
Immediately after that you are telling it to return just some data causing the crash. Rather use another nominator to hold the two different kinds of returned information you need -
dim aData (Split(sData, ";").length) as Long ''Rather use long as the length might exceed the integer type. Use the same for i, change integer to long
Dim bData = Split(sData, ";") as String
for i = 0 to aData.Length
if(i = 4) then
goto skip
elseif (i = 68) then
goto skip
elseif (i = 72) then
goto skip
end if
if(i = aData.length) then
dataChain = dataChain & bData(i)
else
dataChain = dataChain & bData(i) & ", "
end if
skip:
next

How to reduce the decimal length

I want to reduce the decimal length
text1.text = 2137.2198231578
From the above, i want to show only first 2 digit decimal number
Expected Output
text1.text = 2137.21
How to do this.
Format("2137.2198231578", "####.##")
I was about to post use Format() when I noticed p0rter comment.
Format(text1.text, "000.00")
I guess Int() will round down for you.
Been many years since I used VB6...
This function should do what you want (inline comments should explain what is happening):
Private Function FormatDecimals(ByVal Number As Double, ByVal DecimalPlaces As Integer) As String
Dim NumberString As String
Dim DecimalLocation As Integer
Dim i As Integer
Dim LeftHandSide As String
Dim RightHandSide As String
'convert the number to a string
NumberString = CStr(Number)
'find the decimal point
DecimalLocation = InStr(1, NumberString, ".")
'check to see if the decimal point was found
If DecimalLocation = 0 Then
'return the number if no decimal places required
If DecimalPlaces = 0 Then
FormatDecimals = NumberString
Exit Function
End If
'not a floating point number so add on the required number of zeros
NumberString = NumberString & "."
For i = 0 To DecimalPlaces
NumberString = NumberString & "0"
Next
FormatDecimals = NumberString
Exit Function
Else
'decimal point found
'split out the string based on the location of the decimal point
LeftHandSide = Mid(NumberString, 1, DecimalLocation - 1)
RightHandSide = Mid(NumberString, DecimalLocation + 1)
'if we don't want any decimal places just return the left hand side
If DecimalPlaces = 0 Then
FormatDecimals = LeftHandSide
Exit Function
End If
'make sure the right hand side if the required length
Do Until Len(RightHandSide) >= DecimalPlaces
RightHandSide = RightHandSide & "0"
Loop
'strip off any extra didgits that we dont want
RightHandSide = Left(RightHandSide, DecimalPlaces)
'return the new value
FormatDecimals = LeftHandSide & "." & RightHandSide
Exit Function
End If
End Function
Usage:
Debug.Print FormatDecimals(2137.2198231578, 2) 'outputs 2137.21
Looks fairly simple, but I must be missing something subtle here. What about:
Option Explicit
Private Function Fmt2Places(ByVal Value As Double) As String
Fmt2Places = Format$(Fix(Value * 100#) / 100#, "0.00")
End Function
Private Sub Form_Load()
Text1.Text = Fmt2Places(2137.2198231578)
End Sub
This also works in locales where the decimal point character is a comma.

How to get only a filename?

Using VB6
Code.
Dim posn As Integer, i As Integer
Dim fName As String
posn = 0
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
fName = Right(flname, Len(flname) - posn)
posn = InStr(fName, ".")
If posn <> 0 Then
fName = Left(fName, posn - 1)
End If
GetFileName = fName
FileName: Clockings8.mis06042009 120442.fin
But it is showing a filename is “Clockings8”. It should show “Clockings8.mis06042009 120442”
How to modify a code?
Need vb6 code Help
It is a bit cleaner to use the Scripting.FileSystemObject component. Try:
Dim fso as New Scripting.FileSystemObject
GetFileName = fso.GetBaseName(fname)
The reason why your code stops short is that InStr works from the beginning of the string to the end and stops wherever it finds a match. The filename "Clockings8.mis06042009 120442.fin" contains two periods. For this reason, you should use InStrRev instead to start the search from the end of the string.
Going with FileSystemObject's GetBaseName like David suggests is a good idea. If you can't or don't want to (and there are reasons why you might not want to) work with the FileSystemObject there's a simple solution: Remove all characters from a filename string starting with the last dot in the name.
Here's what I mean:
Dim fn As String
fn = "Clockings8.mis06042009 120442.fin"
Dim idx As Integer
idx = InStrRev(fn, ".")
GetFileName = Mid(fn, 1, idx - 1)
If your filename does not have an extenstion but has a dot somewhere in the filename string, then this method will return bad results.

Resources