X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Using Code From One Control In Another One, Access 2013    
post Mar 25 2020, 10:14 AM

Posts: 313
Joined: 3-July 07
From: Richboro PA

Here's the deal, I have this rather long and drawn out piece of VB code in the OnLoad event of a Form that works PERFECTLY.

But I want the same code on the OnActivate event. That way when a user clicks off the form and back to the main form, when they come back the VB program will run

Yes, I can always cut and paste it in, but IMHO that is just sloppy. And when it comes time to debug it'll get messy.

And I could make a subroutine and call it. But there are a lot of references to controls on subforms and such and it'll get messy using absolute addresses (the long name for controls on forms). Plus , there are 7 different forms so I would need to make (7) different subroutines. I'd rather have as much code as I can embedded in the form.

So what can I do?
Go to the top of the page
post Mar 25 2020, 10:33 AM

UA Admin
Posts: 37,007
Joined: 20-June 02
From: Newcastle, WA

Create a PUBLIC function in a stand-alone module. Put your code in that.

You will have to modify it to be usable in different forms, no doubt. Otherwise it's a fairly common "next step" for most of us as we expand our skills.

You may have to pass arguments into the stand-alone, PUBLIC function to make it work across forms, so invest some time learning about that. One thing that comes to mind, for example, is that code in a form module might refer to Me.ControlName or something. That would have to be changed so that "Me" is replaced by the appropriate form name, passed into the function when it's called.

It'll pay off once you get the principle.

My Real Name Is George. Grover Park Consulting is where I did business for 20 years.
How to Ask a Good Question
Beginning SQL Server
Go to the top of the page
Peter Hibbs
post Mar 25 2020, 02:21 PM

UtterAccess VIP
Posts: 1,790
Joined: 17-June 10
From: Dorset. UK.

Hi Dominic,

You can do this easily. As George says, create a PUBLIC function in a stand-alone module and copy your code into it. You then need to pass a reference for the calling form to the function like this :

Public Sub YourRoutineName(frm As Object)

'In the routine you reference any controls (or subforms) on the calling form by using the frm variable instead of Me. For example :
    frm.DateField = Date    'or whatever
    frm.txtName = "Dominic"
   frm.SubformName!txtControl = 123
    and so on

    ...other code

End Sub

In the calling form you would just use :-

    YourRoutineName Me

If the other 7 forms all use the same control names, etc then you could use the sub-routine for them as well (but if you have 7 identical forms I suspect you need to re-design the forms a bit).


Peter Hibbs.
Go to the top of the page
post Mar 25 2020, 03:02 PM

UtterAccess VIP
Posts: 2,247
Joined: 4-June 18
From: Somerset, UK

Whilst I agree with the previous two replies, if you want ALL the form_load code to run from Form_Activate then you can just do this:

Private Sub Form_Activate()
End Sub

Alternatively you could just reopen your form from the Form_Activate event

Colin (Mendip Data Systems)
Website, email
Go to the top of the page
post Mar 26 2020, 08:28 AM

Posts: 313
Joined: 3-July 07
From: Richboro PA

Sorry for the delay getting back to you all.

That Public Sub trick worked out perfectly! Now I have the "update" code in one spot and can trouble shoot it a lot easier

Thanks for all the replies. I may use some of those other techniques along the way
This post has been edited by DominicG: Mar 26 2020, 08:29 AM
Go to the top of the page

Custom Search

RSSSearch   Top   Lo-Fi    9th April 2020 - 12:14 AM