E-Mail Me


Code Snippets

I really don't have much here but I think I cover some of the more important/popular questions. These all apply to VB5 although some may work in previous versions of Visual Basic.

Using default file handlers.

This just means opening a file with it's default application. For instance, opening an html file with the users default browser. I've seen people do this the hard way. They use API to check the default application in the system registry. The easiest way I've found is to shell the start command but hide the DOS box.

Public Sub startFile (strFileName as String)

Call Shell("Start " & """" & strFileName & """"", vbHide)

End Sub

Short, simple, efficient, best of all, no API calls.


Simple Encryption

This should NOT be used for anything too serious.

Public Function encodedStr (strSimple As String) As String

Dim strTemp, tempStr As String
Dim I, intTemp As String

For I = 1 To Len(strSimple)

strTemp = Mid(strSimple, I, 1)
intTemp = Int(Asc(strTemp) + 111)

tempStr = tempStr & Chr$(intTemp)

Next I

encodedStr = strTemp

End Function

This Just gets the ASCII value for each character and turns it into a number plus 111, then converts it back to an ASCII character. You can really make the number anything you want, but you do want it to be too long or else when you try to set it back to an ASCII character you'll get an error. For instance, there is no Chr$(19487), just doesn't exist. Now to decode it...

Function decodedStr (strEncoded As String) As String

Dim tempStr, strTemp As String
Dim I, tempInt As Integer

For I = 1 To Len(strEncoded)

tempStr = Mid(strEncoded, I, 1)
tempInt = (Asc(tempStr) - 111)
strTemp = strTemp + Chr$(tempInt)

Next I

decodedStr = strTemp

End Function

That's all there is to it. Like I said, you probably wouldn't want to use this for anything too serious.


Getting/Setting your application's settings in the registry

This one is pretty easy. To save a setting use..

SaveSetting(AppName As String, Section As String, Key As String, Setting As String)

Or... for example

SaveSetting("MailApp", "Server", "ServerName", strServer)

To get the settings use..

Setting = GetSetting(AppName As String, Section As String, Key As String, [Default]) As String

Or.. for example (again)

strServer = GetSetting("MailApp", "Server", "ServerName", "") As String

This will set the variable strServer to whatever the setting in the registry is. If the setting does not exist, then strServer will be set to the default value of ""


Checking for previous instances of your application.

This just means, checking if your program is already running. Just use this in your startup form or module.

If App.PrevInstance Then End

Simple enough eh?


Returning the file name from a path.

This would be used as something like ... theFile = getFileName(path)

Public Function getFileName(thePath As String) As String

thePath = Trim(thePath) ' trim off white space

Do While InStr(1, thePath, "\") > 1

thePath = Mid(thePath, InStr(1, thePath, "\") + 1)


getFileName = thePath

End Function

Only allow numbers in a text box.

This would go in the KeyPress event of the text box that you only want to contain numbers. The only exception is the backspace, so the user can correct mistakes.

Private Sub txtInt_KeyPress(KeyAscii As Integer)

' don't allow a non-numeric character to be entered unless it's a backspace

If Not KeyAscii = 8 And KeyAscii < 48 Or _
KeyAscii > 57 Then

KeyAscii = 0

End If

End Sub

More to come..