Programmatically adding code to existing controls

P

pietlinden

I have a database that someone else wrote that I have been contracted
to work on to add a few features.

One feature I have been tasked to add is to implement something like
the adhCalendar from the Developer's Handbook to all the textboxes
formatted as not general date. Finding these programmatically is
fine. I wrote some code to open the forms in design view and loop
through the controls and log them to a table.

For each of these controls, I wanted to add the adhCalendar code call.
(Can't quote the VBA exactly, as my newer copy of the book is still in
the mail, but I do know it's in the ADH Desktop volume.) Just in case
I'm not making myself clear (and I'm tired so there's a good chance!),
I'll give an example.

Each of the controls the code finds need exactly the same VBA.

me.Value = adhCalendar(Me.Value)

It would look something like this (well, in my dreams, anyway!)

Private Sub Command8_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
If ctl.Format = "Short Date" Then
'---This is the line I'm having a problem with!
ctl.OnDblClick = "adhCalendar(" & ctl.Value & ")"
End If
End If
Next ctl
End Sub

is there a method to add event code to an object? So I could loop
through all the controls as above and add the same code to the Enter
and Double-Click events of each one?

the Enter code would check
if ctl.IsNull(ctl.Value) then
ctl.Value = adhCalendar()
end if

in the double-click

ctl.Value=adhCalendar(ctl.Value)

I could do it manually, sure. I was just wondering if there were a
way to do it programmatically. <g> I know, it sounds like I'm being
really lazy. but I'm trying to learn something, so I guess I'll play
with it in the meantime.

Thanks,

Pieter
 
S

storrboy

I have a database that someone else wrote that I have been contracted
to work on to add a few features.

One feature I have been tasked to add is to implement something like
the adhCalendar from the Developer's Handbook to all the textboxes
formatted as not general date. Finding these programmatically is
fine. I wrote some code to open the forms in design view and loop
through the controls and log them to a table.

For each of these controls, I wanted to add the adhCalendar code call.
(Can't quote the VBA exactly, as my newer copy of the book is still in
the mail, but I do know it's in the ADH Desktop volume.) Just in case
I'm not making myself clear (and I'm tired so there's a good chance!),
I'll give an example.

Each of the controls the code finds need exactly the same VBA.

me.Value = adhCalendar(Me.Value)

It would look something like this (well, in my dreams, anyway!)

Private Sub Command8_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
If ctl.Format = "Short Date" Then
'---This is the line I'm having a problem with!
ctl.OnDblClick = "adhCalendar(" & ctl.Value & ")"
End If
End If
Next ctl
End Sub

is there a method to add event code to an object? So I could loop
through all the controls as above and add the same code to the Enter
and Double-Click events of each one?

the Enter code would check
if ctl.IsNull(ctl.Value) then
ctl.Value = adhCalendar()
end if

in the double-click

ctl.Value=adhCalendar(ctl.Value)

I could do it manually, sure. I was just wondering if there were a
way to do it programmatically. <g> I know, it sounds like I'm being
really lazy. but I'm trying to learn something, so I guess I'll play
with it in the meantime.

Thanks,

Pieter


I think you have to do it through the Module object of each form.
Example:

Dim mdl as Module
Set mdl= FormObject.Module

You can then use the different methods and properties of the module
object to modify the code lines inside as well as creating events and
other subs.. This can be a bit tricky though, so peruse the help files
before really starting - and try it on a BACKUP FIRST!
 
M

Marshall Barton

I have a database that someone else wrote that I have been contracted
to work on to add a few features.

One feature I have been tasked to add is to implement something like
the adhCalendar from the Developer's Handbook to all the textboxes
formatted as not general date. Finding these programmatically is
fine. I wrote some code to open the forms in design view and loop
through the controls and log them to a table.

For each of these controls, I wanted to add the adhCalendar code call.
(Can't quote the VBA exactly, as my newer copy of the book is still in
the mail, but I do know it's in the ADH Desktop volume.) Just in case
I'm not making myself clear (and I'm tired so there's a good chance!),
I'll give an example.

Each of the controls the code finds need exactly the same VBA.

me.Value = adhCalendar(Me.Value)

It would look something like this (well, in my dreams, anyway!)

Private Sub Command8_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
If ctl.Format = "Short Date" Then
'---This is the line I'm having a problem with!
ctl.OnDblClick = "adhCalendar(" & ctl.Value & ")"
End If
End If
Next ctl
End Sub

is there a method to add event code to an object? So I could loop
through all the controls as above and add the same code to the Enter
and Double-Click events of each one?

the Enter code would check
if ctl.IsNull(ctl.Value) then
ctl.Value = adhCalendar()
end if

in the double-click

ctl.Value=adhCalendar(ctl.Value)

I could do it manually, sure. I was just wondering if there were a
way to do it programmatically. <g> I know, it sounds like I'm being
really lazy. but I'm trying to learn something, so I guess I'll play
with it in the meantime.


Without trying to understand the code you want to insert,
you can use the Module object's methods. Check Help for:

Module Object
Methods:
Find
CreateEventProc
InsertLines
InsertText

Be sure to remember to set the corresponding event property
to [Event Procedure]. E.g.
frm.controlname.OnEnter = "[Event Procedure]"

OTOH, if the code really is identical (except for the form),
you do not need to create the all those event procedures.
Instead you could create a generic Public Function in a
standard module that does the work and just set the event
property to something like =thefunction(Form)
 
P

pietlinden

I have a database that someone else wrote that I have been contracted
to work on to add a few features.
One feature I have been tasked to add is to implement something like
the adhCalendar from the Developer's Handbook to all the textboxes
formatted as not general date. Finding these programmatically is
fine. I wrote some code to open the forms in design view and loop
through the controls and log them to a table.
For each of these controls, I wanted to add the adhCalendar code call.
(Can't quote the VBA exactly, as my newer copy of the book is still in
the mail, but I do know it's in the ADH Desktop volume.) Just in case
I'm not making myself clear (and I'm tired so there's a good chance!),
I'll give an example.
Each of the controls the code finds need exactly the same VBA.
me.Value = adhCalendar(Me.Value)
It would look something like this (well, in my dreams, anyway!)
Private Sub Command8_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
If ctl.Format = "Short Date" Then
'---This is the line I'm having a problem with!
ctl.OnDblClick = "adhCalendar(" & ctl.Value & ")"
End If
End If
Next ctl
End Sub
is there a method to add event code to an object? So I could loop
through all the controls as above and add the same code to the Enter
and Double-Click events of each one?
the Enter code would check
if ctl.IsNull(ctl.Value) then
ctl.Value = adhCalendar()
end if
in the double-click

I could do it manually, sure. I was just wondering if there were a
way to do it programmatically. <g> I know, it sounds like I'm being
really lazy. but I'm trying to learn something, so I guess I'll play
with it in the meantime.

Without trying to understand the code you want to insert,
you can use the Module object's methods. Check Help for:

Module Object
Methods:
Find
CreateEventProc
InsertLines
InsertText

Be sure to remember to set the corresponding event property
to [Event Procedure]. E.g.
frm.controlname.OnEnter = "[Event Procedure]"

OTOH, if the code really is identical (except for the form),
you do not need to create the all those event procedures.
Instead you could create a generic Public Function in a
standard module that does the work and just set the event
property to something like =thefunction(Form)

--
Marsh
MVP [MS Access]- Hide quoted text -

- Show quoted text -

If I were doing it manually, I'd pretty much just paste in something
like:

Me.ControlName.Value=adhCalendar(me.ControlName.Value)

I guess I'll see when the database arrives how much work this will be
and what else has to be done...

Thanks,
Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top