Add OnGotFocus vba WITH vba

oneidalake

New member
Local time
Today, 09:43
Joined
Mar 9, 2017
Messages
10
I have a form with a button that adds a field to another form using vba CreateControl.

I want to set the OnGotFocus Event Procedure of that new field.

For example, the new field that was created is called test123 within Form "ABC".

I want to use vba to create a vba statement for test123 so when I view the code for form ABC, there is a newly created line that says...

Private Sub Test123_GotFocus()
MsgBox "Hello"
End Sub

Any ideas?

Thank you,
Jim
 
something like this

Code:
Dim strFormOpenCode As String
Dim mdl As Module

Set mdl = Forms!Form1.Module     'reference your form module

strFormOpenCode = "Private Sub Test123_GotFocus()" & vbCrLf & "MsgBox" & """Hello""" & vbCrLf & "End Sub"
 
With mdl
 .InsertText strFormOpenCode
 End With
 
Personally I'd do a hide/disable OR unhide/enable rather than create. This lets you deal with the control at all times, especially if you need to know what its value is, without testing first to see if it has been created.
 
not to mention lifetime limits with form controls.
 
Thanks for all the responses.

I ended up going with Moke123's suggestion that worked flawlessly!

Also, this will be rarely used and will have definite limits. Will only be used in case they need an extra field or two.

Thanks again!!!
Jim
 
Thanks for all the responses.

I ended up going with Moke123's suggestion that worked flawlessly!

Also, this will be rarely used and will have definite limits. Will only be used in case they need an extra field or two.

Thanks again!!!
Jim

I'd test rather extensively. As I understand, creating a control at run time counts against the number of controls that can be created on a single form (something like 750ish), so if it stops working suddenly you may know the cause.
 
what mark said, and you may also have a problem in an mde or accde.
 
I'd test rather extensively. As I understand, creating a control at run time counts against the number of controls that can be created on a single form (something like 750ish), so if it stops working suddenly you may know the cause.

Thanks. I was just toying around with something and this one part was driving me crazy. I'll test it out. Although this particular form has quite a few controls (150 or so), I can't imagine adding more than a dozen with this method.

Thanks to you both.
Jim
 

Users who are viewing this thread

Back
Top Bottom