.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘Type’

ACC2000: VBA Functions Break in Database with Missing References

Symptoms
If you have a procedure that contains a Visual Basic for Applicationsfunction, and your database contains a reference to a missing objectlibrary or type library, you may receive the following error message whenyou compile your modules or run the procedure:

Compile Error:
Can’t find project or library
Resolution
Your database contains a reference to a database, type library, or objectlibrary that is marked as MISSING: <referencename> in the References dialog box.

ACC2000: How to Use Collections to Manage Class Objects in VBA

Symptoms
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article shows you how to use collections in Visual Basic forApplications to manage references to class objects in Access 2000.This technique allows your class objects to persist, and enables you tocontrol the individual properties of those objects by using the familiarcollection syntax used in Microsoft Access for implementing Data AccessObjects (DAO) and other Microsoft Office Object models.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Resolution
In order to use collections to manage class objects, you must do thefollowing:Create an instance of the classSet the properties and methods of the classAdd the class to a public collectionUnload the instance of the classYou might expect that unloading the instance of the class results in theclass being closed and terminated. However, the class object persistsbecause you add it to a collection, which then owns the reference to theclass. This is a very powerful technique that allows you to control objectreferences through a collection; the class object does not terminate untilyou remove it from the collection.
The following example creates a class object and a form object, and thenmanages both objects from a collection in a standard module.
Create a Class ModuleCreate a new database called ClassTest.mdb.On the Insert menu, click Class Module.Save the class module as clsTest.Type the following lines in the Declarations section:

Private This_ClassID As StringPrivate This_frm As New Form_frmTest Select Class in the Object box of the Module window. “Initialize” isautomatically selected in the Procedure box.Type the following procedure:

Private Sub Class_Initialize()On Local Error GoTo Class_Initialize_ErrDim Msg As StringThis_frm.Visible = TrueThis_ClassID = “Initialized”This_frm.Caption = This_ClassIDMsgBox “Class Initialized”, vbInformation, “Class Example”Class_Initialize_End:Exit SubClass_Initialize_Err:Msg = “Error #: ” & Format$(Err.Number) & vbCrLfMsg = Msg & Err.DescriptionErr.Raise vbObjectError, “clsTest.Initialize (Private)”, MsgResume Class_Initialize_EndEnd Sub On the Insert menu, click Procedure.In the Insert Procedure dialog box, type ClassID in the Name box andclick Property in the Type box. Then type the following procedures

Public Property Get ClassID() As VariantClassID = This_ClassIDEnd PropertyPublic Property Let ClassID(ByVal vNewValue As Variant)This_ClassID = vNewValueThis_frm.ClassID = This_ClassIDThis_frm.Caption = This_ClassIDEnd Property Save and close the clsTest class module.
Create a FormCreate the following form not based on any table or query in Designview:

Form: frmTest—————–Caption: TestForm With the form still open in Design view, click Code on the View menu.Type the following line in the Declarations section of the form’s classmodule:

Dim This_ClassID As String Add the following event procedure to the form’s Unload property:

Private Sub Form_Unload(Cancel As Integer)col.Remove This_ClassIDEnd Sub On the Insert menu, click Procedure.In the Insert Procedure dialog box, type ClassID in the Name box andclick Property in the Type box. Then type the following procedures. Notethat the Get ClassID() function and vNewValue variable in this exampleare dimensioned as String instead of the default, which is Variant:

Public Property Get ClassID() As StringClassID = This_ClassIDEnd PropertyPublic Property Let ClassID(ByVal vNewValue As String)This_ClassID = vNewValueEnd Property Save and close the frmTest form.
Create a Standard ModuleCreate a new standard module and save it as Module1.Type the following line in the Declarations section:

Public col As New Collection Type the following procedure:

Function CreateClassTest() As String’ Create an instance of the clsTest class module, which creates’ an instance of the frmTest form.Dim cls As New clsTest’ Create a unique identifier string and set it to the upper index’ of the Public col Collection plus one.Dim varClassId As StringvarClassId = “Key_” & CStr(col.Count + 1)’ Set the clsTest class module’s ClassID property to the value of’ varClassId, which in turn sets the frmTest.ClassId property to’ the same value. This is so the form has a method to track its’ relationship to the collection.cls.ClassID = varClassId’ Add the instance of the class object to the collection passing’ varClassId as the Key argument.col.Add cls, varClassIdMsgBox “Created New Collection Item: ” & varClassId, _vbInformation, “Class Example”‘ Unload the cls object variable.Set cls = Nothing’ Return the varClassId.CreateClassTest = varClassIdEnd Function Close and save the module.
Test the ExampleWhen you call the CreateClassTest() function multiple times, it opensmultiple instances of the frmTest form, each of which is unique and capableof managing itself and its participation in the public collection. Eachform is aware of its Key position in the collection, and each one removesitself from the collection when you close the form.
The following sample procedure creates three instances of the clsTestclass:Create a standard module and type the following procedure:

Function CreateThreeItems() As BooleanDim strKeys(1 To 3) As StringDim i As IntegerFor i = LBound(strKeys) To UBound(strKeys)strKeys(i) = CreateClassTest()Next iFor i = LBound(strKeys) To UBound(strKeys)MsgBox col.Item(strKeys(i)).ClassID, vbInformation, _”Class Test”Next iEnd Function To test this function, type the following line in the Immediate window,and then press ENTER:

?CreateThreeItems() Note that messages boxes are displayed each time the clsTest classmodule initializes, when each of three instances of the frmTest form iscreated, and again after all three instances of the form are open.