adding form objects/code dynamically

  • Thread starter Thread starter Sam Farhar
  • Start date Start date
S

Sam Farhar

Thanks to a client who won't budge, I'm tasked with creating a form that
I'll need to add objects (text boxes, etc) to dynamically. I can deal with
that. However, can I also add VBA code behind the objects dynamically? If
so, how?

Thanks in advance.
 
Hi.
Firstly, do try and get your client to tell you exactly what he/she needs.
I've seen some serious headaches when trying to develop an application to be
all things to all people simply because the client doesn't explain what they
want; either because they don't know, or they don't want to know, how a work
process works.
A trick I've used is to work one on one with the client, getting them to
justify each inclusion for the wizard ('cause that's essentially what you're
creating). Hopefully the smoke clears and the moutain turns in to a mole
hill.
Having said that, I don't know your situation (or experience) so applogies
if I've over stepped the mark.

To answer your question, use CreateEventProc to add events to your controls
at design time.
This example is from the help files.

Function ClickEventProc() As Boolean
Dim frm As Form, ctl As Control, mdl As Module
Dim lngReturn As Long

On Error GoTo Error_ClickEventProc
' Create new form.
Set frm = CreateForm
' Create command button on form.
Set ctl = CreateControl(frm.Name, acCommandButton, , , , 1000, 1000)
ctl.Caption = "Click here"
' Return reference to form module.
Set mdl = frm.Module
' Add event procedure.
lngReturn = mdl.CreateEventProc("Click", ctl.Name)
' Insert text into body of procedure.
mdl.InsertLines lngReturn + 1, vbTab & "MsgBox ""Way cool!"""
ClickEventProc = True

Exit_ClickEventProc:
Exit Function

Error_ClickEventProc:
MsgBox Err & " :" & Err.Description
ClickEventProc = False
Resume Exit_ClickEventProc
End Function

HTH, Graeme.
 
Back
Top