[AccessD] Class builder

Shamil Salakhetdinov shamil at users.mns.ru
Sat Jan 14 11:08:55 CST 2006


John,

This should work I think for both MS Access and COM-Add ins - please try:

Public Function a_test()
dim objApp as object

   set objApp = Access.Application ' for MS Access add-ins
   'set objApp = <Application object reference COM Add-in gets on
   '                       connection>

'
' Add reference to:
'
' 1. Microsoft Visual Basic for Applications
'     Extensibility 5.3 - VBE6EXT.OLB
'
' before running this code
'
Dim strCode As String
Dim strName As String
    strName = "CTest"
    strCode = _
      "public function Test()" & vbCrLf & _
      "    Msgbox ""Hello, World!""" & vbCrLf & _
      "end function"
    AddClassModule objApp, strName, strCode
End Function

Public Sub AddClassModule( _
                ByRef rapp As object, _
                ByVal vstrName As String, _
                ByVal vstrCode As String, _
                Optional ByVal vstrProjectName As String = "")
Dim prj As VBIDE.VBProject
Dim prjItem As VBIDE.VBComponent
Dim strLst As String

    If Len(vstrProjectName) = 0 Then
       Set prj = rapp.VBE.ActiveVBProject
    Else
       Set prj = rapp.VBE.VBProjects.Item(vstrProjectName)
    End If

    Set prjItem = prj.VBComponents.Add(vbext_ct_ClassModule)
    prjItem.CodeModule.AddFromString vstrCode
    prjItem.Name = vstrName
    rapp.DoCmd.Save acModule, vstrName
End Sub

> apparently the .RunCommand acCmdNewObjectClassModule
> isn't available in the
> context of code running out in an add-in.
.RunCommand acCmdNewObjectClassModule is available in an addin but you have
to have your front-end MS Access mdb/adp opened and of course code line
should be:

objApp.RunCommand ....

where objApp is Access.Application object reference, which COM Add-in gets
on connection and MS Access Add-in has natively as default instance of
Access.Application ...

Shamil

----- Original Message ----- 
From: "John Colby" <jwcolby at ColbyConsulting.com>
To: "'Access Developers discussion and problem solving'"
<accessd at databaseadvisors.com>
Sent: Saturday, January 14, 2006 5:07 PM
Subject: Re: [AccessD] Class builder


> Shamil,
>
> It appears that I am not going to solve this so any help is much
> appreciated.
>
> Thanks,
>
>
> John W. Colby
> www.ColbyConsulting.com
>
>
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil
> Salakhetdinov
> Sent: Friday, January 13, 2006 12:17 PM
> To: Access Developers discussion and problem solving
> Subject: Re: [AccessD] Class builder
>
> John,
>
> The code I posted here works because it was tested before posting but it
> works in another context.
>
> You didn't say you write a (kind of) COM add-in and manipulate class
modules
> via VBE.
>
> If you will not solve this problem until tomorrow I will check my archives
> to find how it can be done - I did that both in MS Access and MS Excel -
> it's simple but needs some testing...
>
> I have to do some urgent work now...
>
> Shamil
>
> ----- Original Message -----
> From: "John Colby" <jwcolby at ColbyConsulting.com>
> To: "'Access Developers discussion and problem solving'"
> <accessd at databaseadvisors.com>
> Sent: Friday, January 13, 2006 5:32 PM
> Subject: Re: [AccessD] Class builder
>
>
> > Shamil,
> >
> > Well, that didn't work.  I am trying to build this as an Add-In and
> > apparently the .RunCommand acCmdNewObjectClassModule isn't available in
> the
> > context of code running out in an add-in.  I had been using the
following
> > code to get a module:
> >
> > Dim lMdl As VBComponent
> > Dim VBProj As VBProject
> >     Set VBProj = Application.VBE.ActiveVBProject
> >     Set lMdl =
> > Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_ClassModule)
> >     lMdl.Name = strModuleName
> >     Set AddModule = lMdl.CodeModule
> >
> > The "lMdl.Name = strModuleName" does correctly set the name of the class
> > module.  The problem is that the created class is in limbo somewhere.
It
> > exists, i.e. if I try it again, the "lMdl.Name = strModuleName" causes a
> run
> > time error.  I cannot save it however.  Even opening a module in the
> current
> > app and clicking save doesn't save it.
> >
> > John W. Colby
> > www.ColbyConsulting.com
> >
> >
> > -----Original Message-----
> > From: accessd-bounces at databaseadvisors.com
> > [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil
> > Salakhetdinov
> > Sent: Friday, January 13, 2006 3:53 AM
> > To: Access Developers discussion and problem solving
> > Subject: Re: [AccessD] Class builder
> >
> > John,
> >
> > Here is one of the methods how a new class module can be created and
saved
> > using VBA:
> >
> > Dim mdl As Access.Module
> > Dim objApp As Access.Application
> > Dim strTempMdlName As String
> > Dim strMdlName As String
> >     Set objApp = Access.Application
> >     strMdlName = "CTestClassModule"
> >
> >     With objApp.DoCmd
> >         .RunCommand acCmdNewObjectClassModule
> >         Set mdl = objApp.Modules.Item(objApp.Modules.Count - 1)
> >         strTempMdlName = mdl.Name
> >
> >         mdl.InsertText "public TestProperty as integer"
> >
> >         .Close acModule, strTempMdlName, acSaveYes
> >         .Rename strMdlName, acModule, strTempMdlName
> >     End With
> >
> > Make sure there is no class module with the name CTestClassModule in
your
> > database...
> >
> > Shamil
> >
> > ----- Original Message -----
> > From: "John Colby" <jwcolby at ColbyConsulting.com>
> > To: "'Access Developers discussion and problem solving'"
> > <accessd at databaseadvisors.com>
> > Sent: Friday, January 13, 2006 4:08 AM
> > Subject: [AccessD] Class builder
> >
> >
> > > I have just finished a little class builder.  If anyone wants to play
> with
> > > it let me know.
> > >
> > > In the meantime, the one thing I can't figure out is how to save the
> newly
> > > created class.  I can click the save button and it saves the new
module,
> > but
> > > I can't make it happen from VBA.  Anyone know what object saves
unsaved
> > > modules?
> > >
> > > John W. Colby
> > > www.ColbyConsulting.com
> > >
> > >




More information about the AccessD mailing list