Visual Basic

Sometimes you Don’t Need a Variable

If you’re going to use an object and get rid of it after a few lines, you don’t need to declare a variable to it. Just use With. Here is an example: With CreateObject(“Scripting.FileSystemObject”) FullPath = .BuildPath(Path, FileName) If Not .FileExists(FullPath) Then Rem Do something… End IfEnd With

Something to Remember When Filtering Keystrokes

Filtering user input is extremely mundane. Who never captured user’s keystrokes in a TextBox in order to avoid some forbidden input, such as numbers or letters? If you went down this rabbit hole, here are some things to keep in mind: User can hit navigational keys like Left Arrow, Right

Read All Text From a File

There are a number of ways to read all content from a text file. The function below uses only VB6’s features to do that. Public Function ReadAll(ByVal FilePath As String) As StringDim FileNumber As LongDim Result As String If FilePath “” Then If Dir(FilePath) “” Then FileNumber = FreeFile Open

IsPrime Function

Everybody knows how to check if a number is prime: 1) Set a divisor to two; 2) “Mod” the number by the divisor; 3) If the result is zero, then we have our answer: It is not prime; 4) If it is something different from zero, then increment the divisor;

Passing the Missing Function Value

Ever?needed?to?pass?or?not?pass?a?value?to?a?sub?(or?function)?having?an?optional?argument?depending?on?the?business?rules? I?mean?something?like?this: Function?MyFunction(ByVal?SomeArg?As?Integer)?As?String ????If?SomeArg??0?Then ????????MyFunction?=?NotMyFunction(SomeArg)?’OK,?we?can?pass?SomeArg ????Else ????????MyFunction?=?NotMyFunction?’We?cannot?pass?it.?NotMyFunction?will?got?it?missing. ????End?If End?Function Instead?of?repeating?the?call?to?such?a?function?or?sub,?you?can?pass?it?a?true?missing?value?using?the?function?below: Public?Function?Missing(Optional?PlaceHolder?As?Variant)?As?Variant ????Missing?=?PlaceHolder End?Function Our?example?would?change?to?something?like?this: Function?MyFunction(ByVal?SomeArg?As?Integer)?As?Integer Dim?Arg?As?Variant ????If?SomeArg??0?Then?Arg?=?SomeArg?Else?Arg?=?Missing ????MyFunction?=?NotMyFunction(Arg) End?Function

Adding Longs Without Overflowing

Suppose you are porting a C code, for example, that sums 32 bit integers (Longs) to VB6. In C, when the result of a sum does not fit into an integer, it overflows silently. In VB, you will get a nasty runtime error instead. So, this is a handy function

Runtime Mode

If you ever need to know whether your code running inside the IDE or is compiled, drop these two functions in a module and use the first one to figure it out: Public Function RunTimeMode() As Boolean Dim bool As Boolean     Debug.Assert SetToTrue(bool)     RunTimeMode = Not bool End Function Private Function SetToTrue(ByRef bool As Boolean) As Boolean     bool = True     SetToTrue = True End Function

Validating Dynamic Arrays

When you have a dynamic array that has no dimensions defined yet in VB, trying to get its lower or upper bound raises an error (#9 – Subscript out of range). There are a number of ways to check whether a variable contains a valid array or not, but the

Converting PNG to HICON

Loading any PNG resource as an hIcon. Vista introduced PNG encoding for icons. There is an API that will convert PNGs to valid hIcon handles. API Declaration follows: Private Declare Function CreateIconFromResourceEx Lib “user32.dll” ( _ pbIconBits As Any, ByVal cbSize As Long, ByVal fIcon As Long, _ ByVal dwVersion