Introduction: Programming in VB6: Tips and Tricks

I program in my spare time, and am a fairly proficient programmer using VB6. It is easy and I have yet to find anything I need it to accomplish that it cannot, though sometimes it can be finicky to accomplish your task. Along the way I found so many annoying things mostly the way it counts certain things. So here is a compilation of some things I learned along the way.

I urge you if you have any problems comment and I'll try to help with more steps.

Step 1: Strings

The way VB deals with strings is confusing sometimes.

texta = "abcdefg"
put " symbols on both sides of text you want to be counted as a string

Mid(String, Start, Length) selects a section of text from a string
in strings there is no zero start strings start at 1
textb = mid(texta, 2, 2)
this makes the variable textb = "bc"

textb = mid(texta, 4, 4)
this makes the variable textb = "defg"

textb = mid(texta, 4, 5)
this makes the variable textb = "defg"
Thats right if the text you're trying to select is longer then the available text it ends at the last character
depending on your program you'll have to place traps to avoid errors.
This is such a trap:

textb = mid(texta, 4, 5)
if len(textb) < 5 then 'Len() returns the length of a string the < means less then
msgbox "text is too short" 'Shows a message box with the string
exit sub 'This exits the sub you are in. ending the code. if you are in a function then you would put exit function
end if

finding the place of a character in a string
we use instr(Start, String1, String2)
texta = "abcdefg"
textb = instr(1, texta, "c") 'this finds the letter c in string texta
textb now equals 3
Instr returns 0 (zero) if it does not find the character

this is an annoying one
say you want "abcdefg" to be "defgabc"
texta = "abcdefg"
textb = mid(texta, 4, 4) & mid(texta, 1, 3)
That wasn't too bad but now lets add in the instr() function
say you want to reorganize a string at a curtain character but it is not the same spot everytime
texta = "abcdefg"
textb = "gfabdec"
textc = mid(texta, instr(1, texta, "c"), len(texta) - instr(1, texta, "c") + 1) & mid(texta, 1, instr(1, texta, "c") - 1)
note the part len(texta) - instr(1, texta, "c") + 1 because instr counts the character you're looking for so you go back one character so the mid() takes the character you're looking for as the first on in the string

textc now equals "cdefgab"
textd = mid(textb, instr(1, textb, "c"), len(textb) - instr(1, textb, "c") + 1) & mid(textb, 1, instr(1, textb, "c") - 1)
textd now equals "cgfabde"

lets try finding a character that is not in the string
texta = "abcdefg"
textc = mid(texta, instr(1, texta, "h"), len(texta) - instr(1, texta, "h") + 1) & mid(texta,1, instr(1, texta, "h") - 1)
you get an error because the start part of Mid() cannot = 0 since strings start at 1
so you have to avoid the runtime error. this is one way to do it

texta = "abcdefg"
if instr(1, texta, "h") <> 0 then
textc = mid(texta, instr(1, texta, "h"), len(texta) - instr(1, texta, "h") + 1) & mid(texta,1, instr(1, texta, "h") - 1)
else 'only one else statement per if situation
msgbox "Cannot find character in string" 'shows a message box with the string
end if

This sums up pretty much everything you need to know about strings

Step 2: Converting Characters

One I found earlier this week has to do with the Hex(number) function the length on the number does not matter.
it returns a string in hex
hex(11111111) returns "A98AC7"
when converting an ascii character to hex you would do
texta = "a"
hex( asc(texta)) returns "61"
texta = "k"
hex( asc(texta)) returns "6B"
the string will be 2 characters for MOST of the ASCI characters
unless the are under a value of 16 then it only returns 1 character
if you want them all to be 2 characters you would do as follows:

textb = hex( asc(texta))
if len(textb) = 1 then
textb = "0" and textb 'that is a zero not a capital o
end if

Step 3: File Editing

to open a file you need a freefile and a filename

gfile = FreeFile 'gfile is now the next available freefile
c = "c:\test.txt"

then there are the 4 ways you can open a file
Input - You can only read.
Output - You can only write and it will erase the file if it exists. carriage return and linefeed last characters
Append - You can only write. it will add to the existing file. carriage return and linefeed last characters
Binary - Read and write. very precise. the only one I use now.

for building log files and the such use append it will add the new entry to the end of the file.
for more complex log files load the entire file into a variable and add the new entry to the beginning of the file.

dim stro as string 'this is necessary or there will be an error
gfile = FreeFile
c = "c:\test.txt"
open c for binary as #gfile 'remember gfile is a number
stro = space$( lof(gfile) ) 'when opening for binary you have to tell how big the length of text you want to take is lof( gfile) will return the length of gfile and space$ ( ) returns a string of spaces the length of the number you enter
get #gfile, 1, stro 'get from file number, starting byte, variable to save it to. and loads only the length of the variable
close #gfile
stro now is equal to the file test.txt.

you can put information to a file by
ffile = FreeFile
stro = "New line" & VbCrLf & stro ' adds "New line" and carraige return and linefeed followed by the original text
open c for binary as #ffile
kill c
put #ffile, 1, stro 'put overwrites previous information if the file is getting smaller you should kill the file first which deletes it
close #ffile
the file now has "New line" in it

one thing I found with the put function is it has errors if you try to put a character like
put #ffile, 133, chr(1) ' this replaces the character at byte 133 with character 1 or hex 0x01
I found when I did this with one program I made it actually added hex 0x08000100000001 which is freaking wierd. I solved this by loading the file to a variable and doing string mods
i = 133
stro = mid(stro, 1, i - 1) & chr(1) & mid(stro,i + 1, len(stro) - i)
put #ffile, 1, stro
this worked perfectly