VB Annoyances



About Me
Favorite Quotes
Contact Me



This is a collection of annoyances I've encountered in VB6 and VB.Net.



Object reference not set to an instance of an object
What I was doing:
Calling a function within a user-defined class from a program module
What the problem was:
Forgot to Dim the object reference as new
What I had:
Dim MyClassInstance as MyClass
in my calling module
What fixed it:
Dim MyClassInstance as New MyClass
in my calling module
Trying to use DAO in VB.NET
What I was doing:
Trying to use database access without shelling out $1400 to figure out ADO.NET, which from things that I've read on USENET written by people other than MVP's is slower than DAO anyway
What the problem was:
Converter doesn't make everything obvious
What fixed it:
Click to download clsNetRecordset.txt - change the extension to .vb once you've downloaded it. 

This class creates a workspace and links to a database and recordset, and makes available standard DAO recordset procedures.

It can be invoked from your main program with a call such as

Dim myRecordset As New clsNetRecordset("mydatabase.mdb","mytable")

This Dim statement will provide you with access to the underlying database via your myRecordset object and the DAORecordset object in the class.

Unfortunately, this does not support the "!" fields syntax; fields must be referenced with a call such as

strVar = myRecordset.DAORecordset.Fields("myFieldNm").Value

Note that just like in VB6, you must add a reference to the DAO library you wish to use.  The DAO libraries are found under the COM tab:

Feel free to use the above class in any program, whether for non-commercial use or commercial; all I ask is that you maintain the comment credit line.

Word prompting to save a document created via VB6
What I was doing:
Trying to close the document with various close commands and do-not-save option set.
What the problem was:
According to everything I've found, what I tried to do should have worked.  However, it didn't.
What I had:
Tried each of the below commands:
  • mvar_wdApp.ActiveWindow.Close False
  • mvar_wdDoc1.Close _ SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges
  • mvar_wdDoc1.Close _ (Word.WdSaveOptions.wdDoNotSaveChanges)
  • mvar_wdApp.Documents(1).Close _ (Word.WdSaveOptions.wdDoNotSaveChanges)
  • mvar_wdApp.ActiveDocument.Close_ Word.wdDoNotSaveChanges 
What fixed it:
mvar_wdDoc1.Saved = True

This tells Word that the document has been saved already and fools it into not displaying the prompt.



Page created and maintained by steward
Last updated: Sunday, May 8, 2005