VBNotebook Home | Articles | Q&A Session: Common VB Programming Pitfalls
Here are some common mistakes often made by novice (and even sometimes experienced) VB programmers.
__________________________________________________________________________________________
Q1: I have declared my variables
like so...
Dim minval, maxval, curval, oldval, newval As Double
...but strangely this code...
minval = "Test"
... doesn't cause an error even though minval is declared as a double, but...
newval = "Test"
...does cause a "Type Mismatch" error at
runtime. What gives?
A1: You are declaring minval, maxval, curval,
oldval as variants and only newval as a double. You have to
explicitly declare each variable as specific type or it will
become a variant by default (unless you override this with a
DEF??? statement). The line should look like this...
Dim minval As Double, maxval As Double, curval As Double, oldval As Double, newval As Double
...or, the preferred method is to list each variable on a separate line, like so...
Dim minval As Double
Dim maxval As Double
Dim curval As Double
Dim oldval As Double
Dim newval As Double
__________________________________________________________________________________________
Q2: My application causes an Invalid Page Fault error when it exits using End statement.
A2: Never, ever, use the End statement. The End statement stops program execution abruptly. Nothing is cleaned up or unloaded properly. DLLs and other ActiveX objects (such as ADO) that are providing callback addresses to external processes (such as database calls) become invalid, thus causing invalid page fault errors. Even if you don't get these errors, exiting with End will cause .TMP files to be left over from the run, which in turn may cause problems in the future. Everything must be cleaned up properly prior to exit and End does not allow this to take place.
__________________________________________________________________________________________
Copyright 2000-2005, J. Frank Carr