UtterAccess.com
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    
 
   
DominicG
post Mar 25 2020, 10:14 AM
Post#1



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
 
GroverParkGeorge
post Mar 25 2020, 10:33 AM
Post#2


UA Admin
Posts: 37,015
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
Post#3


UtterAccess VIP
Posts: 1,791
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 :

CODE
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 :-

CODE
    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).

HTH

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


UtterAccess VIP
Posts: 2,248
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:

CODE
Private Sub Form_Activate()
  Form_Load
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
 
DominicG
post Mar 26 2020, 08:28 AM
Post#5



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


Sorry for the delay getting back to you all.

George,
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 - 07:04 PM