Link to home
Start Free TrialLog in
Avatar of stephenlecomptejr
stephenlecomptejrFlag for United States of America

asked on

Writing a code generator for Micrsoft Access

I would like to write my own code generator for Microsoft Access - I currently have version 2003.  But I don't see much out on the web out there?  Any hints on how to do this is much appreciated?
ASKER CERTIFIED SOLUTION
Avatar of stevbe
stevbe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stevbe
stevbe

I just looked ... I actually still have a form that you enter all the info in .. select from existing module, add new module, add procedure, scope, function or sub, description etc. I had *template* modules / procedures I copied so I could get the header / error handling written automagically. Let me know which parts I should brush up on.

Steve
Avatar of rockiroads
First of all, what kind of code do you want to generate? what is it for? is it for forms?
I dont think u can say just create a code generator until you have planned what kind of code u want to generate it for. From this, you can start to work on what u want. Creating a form with controls and modules can be done, thats not the issue though. Its how/what u want doing.

Do note, that a lot of wizards already exist that generate code, so perhaps u can base your ideas on that
e.g. ensure toolbar wizard is pressed (2nd button) then when u add a button, a wizard turns up


Here is a simple (and I mean simple) example of creating a form on a fly

Assume u had a driver form, contains one listbox which lists control types (lstControls), one textbox where u enter the new form name (txtFormName) and one button which does the creating (cmdCreate)

'Here we define our Value List of controls
Private Sub Form_Load()
    lstControls.RowSource = acTextBox & ";Textbox;" & acCommandButton & ";Cmd Btn;" & acListBox & ";List Box"
End Sub

Private Sub cmdCreate_Click()

    Dim frm As Form
    Dim iStartX As Integer
    Dim iStartY As Integer
    Dim i As Integer
    Dim sName as String    
   
    'Set initial coorddinates
    iStartX = 100
    iStartY = 100
   
    'Create new form
    Set frm = CreateForm(, Me.txtFormName)
    sName = frm.Name

    'Open form in design mode
    DoCmd.OpenForm sName, acDesign
   
    'create selected controls
    For i = 0 To lstControls.ListCount - 1
        If lstControls.Selected(i) = True Then
            CreateControl sName, lstControls.Column(0, i), , , , iStartX, iStartY
            iStartX = iStartX + 250
        End If
    Next i
   
    'Close form, saving it
    DoCmd.Close acForm, sName, acSaveYes
    if sName <> Me.txtFormName then DoCmd.Rename Me.txtFormName, acForm, sName

End Sub


This is just to give u an idea

If u want to create forms on the fly, u will have to do things like prompt for captions etc
You could also prompt for recordsource, then list the fields from that recordsource
U can get the users to assign fields to controls

Also, say for buttons, u can specify what kind of action and create the code for that button


Avatar of stephenlecomptejr

ASKER

I don't want to generate any forms.

I only want to be able to paste in entire functions for database connectivity.

Say within a procedure always the following:

On Error Resume Next

Dim sSelect As String
Dim rstData As DAO.Recordset

I'm thinking just create a toolbar and a macro that will paste the entire set of lines wherever I may be - correct?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<On Error Resume Next>

please don't put this in your code generator ... you are certainly sure to regret it ... put in your standard error handler.


I use to use code / forms to create modules / procedures but I found that I didn't like the extra code / form required so after I was ready to push to production I would then spend time stripping them out which never helped during maintenance mode. Then I use to keep 1 module that had my base templates for new module header, and function / sub skeleton and manually copy / paste. Now I have a couple of very short functions in my standard template that I can easily copy/paste manually and strip the body from anytime I need. Funny how over time you find what really suits you best.


All that being said, did my samples help any?

Steve

Hey Steve,

please forgive me - I won't have a chance to try out your samples till this weekend.

Sorry.
no worries ... I had it laying about :-)
Hey stevbe,

What reference do you have this tied to?

I get a user-defined type not allowed on Dim vbcNew As VBIDE.VBComponent.

the name in the references dialog is:
    Microsoft Visual Basic for Applications Extensibility 5.3

which points to the file:
    C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB
   
resulting in a library name of:
   VBIDE

Steve
OK - sorry about the stupid question...

I've got a a form with a button when clicked does:

Private Sub cmdPaste_Click()
 
  CreateProcedure ("modDefault")
 
End Sub

I already have a modDefault module already but I still get a Run-time error '9' on subscript out of range on the following line-item:  Set vbcBase = VBE.VBProjects(1).VBComponents("basProcedure").CodeModule

Not sure what "basProcedure" is supposed to be?

basProcedure was a module I was using as a tmeplate, you can use any string source in the String argument of InsetLines. Of course you will need to change around all the formatting I was doing ... I know I would :-)

Steve
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Split: stevbe {http:#17235944} & rockiroads {http:#17245393}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

jjafferr
EE Cleanup Volunteer