Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically creating a Class Module 2

Status
Not open for further replies.

VB400

Programmer
Sep 8, 1999
359
US
<br>I'm trying to create a very small case-tool whereby I'd pass a few parameters to a subroutine and that routine would:<br><br>1. Create a &quot;Visual Basic Class Module&quot;<br>2. Set its properties (i.e. Name, Instancing, etc.)<br>3. Insert predetermined lines of code<br>4. Save the class module to the HD.<br><br>Does anyone know how to do this?
 
VB400,<br><br>I have done this more times than I care to remember.&nbsp;&nbsp;There are only a few 'tricks' to remember.<br><br><b><u><font color=red>ALL</font></u></b> VB 'modules' are - just plain old text files.<br><br>Class modules have a few lines at their start which VB does not show.&nbsp;&nbsp;You can get these by using a text program - NotePad is good for this.&nbsp;&nbsp;Open NotePad, use it to opne any VB Class file, copy the lines which you don't see in the class module when you open it in VB.&nbsp;&nbsp;Save these to some text file ('clsHdr.Txt').<br><br>For most groups of 'classes' there are functions which are the same for each class (using a data access class as an example, open/close; first/last; next/previous; LtKey/LeKey/EqKey/GtKey/GeKet; ...) For these, I generally create the prototype of the class and just copy these functions to a text file.<br><br>Additional Functions are unique to the Class - even within the group (again, using a data access class, these would represent the individual field elements of the class.&nbsp;&nbsp;For these, I generally make a small MDB database which list each &quot;Type&quot; (dataset/table), the field name and characteristics (type/length ...).&nbsp;&nbsp;by getting each field of each dataset I &quot;build&quot; the UDT which represents the class as a UDT - again as a text file.<br><br>When the various 'constant' parts of the Class module are some where on disk as text file(s), I have a VB project/module which accepts the a token representing the name of the class, opens the various text files, reads them, and writes them back to disc as a single file with the extension &quot;.cls&quot;.<br><br>Of course, each of the parts needs to be written in a specific order, and the variable parts need to be inserted in their proper &quot;place&quot;.&nbsp;&nbsp;Again, using the data access model, most of the variable parts and the property Get/Let procedures.&nbsp;&nbsp;These are basically the field elements of the UDT, and are normally vert short (mine are mostly three lines;: <br>Declaration <br>&nbsp;&nbsp;&nbsp;&nbsp;Statement <br>End Function.<br><br>The declaration is just the Function Get ¦ Let (for the Let, the var passed to the function) For the Get, the var type passed from the function.<br><br>The statement obviously just passes the var info to ¦ the class to the outside world.<br><br>End Function is just end function.<br><br>The var and it's type are taken from the small MDB table noted earlier.<br><br>It is possible to bypass some of this by having the skeleton of the fixed parts in place as a class module with some embedded tags to show where to place the various variable parts and just do that.&nbsp;&nbsp;My experience has been that I need to make as many changes to the fixed parts as the variable one(s), and it is just as easy to do it in the text file as the skeleton class, so I do not maintain the skeleton class.<br><br>Also, for most of the classes I generate in this manner, hter are a few pieces (KeyWords) which are changes from Calss to Class - enven though they are in the &quot;fixed&quot; part.&nbsp;&nbsp;For these, I just embed a KEY_WORD in the text file.&nbsp;&nbsp;When the text file ir read in, I parse for the KEY_WORD and replace it/them with the class specific repalcement word.&nbsp;&nbsp;In (almost?)&nbsp;&nbsp;cases, these Key_Word(s) are just the class name of some variation thereof.<br><br>MichaelRed<br>There is never time to do it right but there is always time to do it over
 
I don't usually do it this way but if you want to create a class module or any other component on the fly the best way is to use the VBIDE <br><br>try the following code if you pass it a Project object and a name and specify the instancing that you require then it returns to you a code module within which it is very easy to write code <br><br>Option Explicit<br><br>Public Enum enumVBInstancing<br>&nbsp;&nbsp;&nbsp;&nbsp;en_Private = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;en_Public = 5<br>&nbsp;&nbsp;&nbsp;&nbsp;en_PublicNotCreateable = 2<br><br>End Enum<br><br>Public Function NewClassWriter(mProject As VBProject, Name As String, enInstancing As enumVBInstancing) As VBIDE.CodeModule<br><br>Dim oNewComp As VBIDE.VBComponent<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;Set oNewComp = mProject.VBComponents.Add(vbext_ct_ClassModule)<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;oNewComp.Name = Name<br>&nbsp;&nbsp;&nbsp;&nbsp;oNewComp.Properties!Instancing = enInstancing<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'This return the code module so that it is easy to&nbsp;&nbsp;&nbsp;write code into it<br>&nbsp;&nbsp;&nbsp;&nbsp;NewClassWriter = oNewComp.CodeModule<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>End Function<br><br>Private Sub Command1_Click()<br>Dim oCodeModule As CodeModule<br><br>oCodeModule = NewClassWriter(&quot;testing&quot;, en_Private)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;With oCodeModule<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InsertLines 1, &quot;Private Sub Test()&quot; & vbCrLf & &quot;End Sub&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;&nbsp;<br><br>End Sub
 
<br>Thanks Michael,<br><br>The trick to this whole thing is the hidden lines in the class module.<br><br>When I started this, I was creating the text file with the .cls extension; however, when I added it to an ActiveX DLL project, it came in as a basic module -- now I know why!&nbsp;&nbsp;At that point however, I thought/hoped there was a component that I could reference that allowed me to create &quot;Class Modules&quot; but I couldn't find any.&nbsp;&nbsp;I was hoping for a:<br><br>Dim objClass as VBClassModule<br>objClass.Name = &quot;abc&quot;<br>objClass.Instancing = vbMultiUse<br><br>or something like that...<br><br>Anyway, inserting those lines into a text file works great and I thank you for your help.
 
<br>Vince,<br><br>It seems that when you're calling the NewClassWriter, you're missing the project parameter.&nbsp;&nbsp;I dimmed a new variable as VBProject and tried to pass it but I get &quot;Object variable or With block variable not set&quot;.&nbsp;&nbsp;Then I tried:&nbsp;&nbsp;&quot;Set oProject = New VBProject&quot;&nbsp;&nbsp;and I get &quot;ActiveX component can't create object&quot;<br><br>Any ideas?
 
Sorry it appears that you have to do this from within an addin project as you require a reference to the VBIDE object from which you can get the activeproject or create a new one. So the solution might be to create this as an addin <br><br>hope that this helps<br><br>
 
There are several ways to assist you in creating a class in Visual BASIC. From the 'Add-Ins' menu in VB, there is a VB 6 Class Builder that will give you the basics for creating a class. You enter the properties, mehtods and events and give them their paraemters and it will create the skeleton code for you. You may have to go into the Add-In manager to turn these options on.

Another alternative is to use Visual Modeler that comes with the Enterprise version of Visaul BASIC. This will let you represent your model graphcially and then all you have to do is tell it to create the code. I prefer this method, since the VB Class builder tends to assign some pretty BAD names for the local variables for you public properties!

Hope that helps.

 
Hi VB400, i noticed ur post here, but the code that Vince posted dosent work? can u help me?, my email is cquek77@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top