run subs/functions in another form

smig

Registered User.
Local time
Today, 07:41
Joined
Nov 25, 2009
Messages
2,209
is it possible to run subs/functions that are in another form ?
 
Sure, though often they should be in a standard module if you want to run them from various places. Basically:

Forms!FormName.Form.FunctionName

It should be Public rather than Private.
 
Hi,

remember that it can only be called if the form is open otherwise, its not available.


Cheers

N
 
Code:
[color=green]' In this Form...[/color]
Private Sub cmdTestIt_Click()

    [color=green]' Call function FunctionInForm1() in Form Form1
    ' Form1 does not need to be open.[/color]
    MsgBox Form_Form1.FunctionInForm1()
    
End Sub





[color=green]' In Form1[/color]
Public Function FunctionInForm1() As String

    FunctionInForm1 = "Here in Form1"
    
End Function
 
Code:
[color=green]' In this Form...[/color]
Private Sub cmdTestIt_Click()

    [color=green]' Call function FunctionInForm1() in Form Form1
    ' Form1 does not need to be open.[/color]
    MsgBox Form_Form1.FunctionInForm1()
    
End Sub





[color=green]' In Form1[/color]
Public Function FunctionInForm1() As String

    FunctionInForm1 = "Here in Form1"
    
End Function

I'm sorry, I always thought a form had to be open for code to be available. I'm sure I read that on here!

Cheers

Nidge
 
It is both true and false…

Form1 does not need to be Open; ‘Form_Form1’ will instantiate a copy, call the Function and then close Form1.

Technical point but Form1 does not need to be Open because a copy of it will be opened and then closed.

Another technical point; Form1 needs to have its HasModule property set to Yes. In this case Form1 must have a Class Module because it contains a Function. But if we tried to do this: -

Msgbox Form_Form1.Detail.BackColor

It would fail if it did not have a Class Module because Forms without Class Modules can’t be instantiated.

So, it’s both true and false depending on the circumstances.

Chris.
 
Thank you all :)

what realy made me happy is that I can have the same function (same name) as public on many forms with no conflict :D

this is a date picker called from many forms, so the calling form is open when I need to run the code (update field + sometimes more code to run).
 
A date picker?

I recently put a revised version of Allen Browne’s nice little date picker here:-

http://www.access-programmers.co.uk/forums/showthread.php?t=219428

It differs from Allen’s original in that you can also pass minimum and maximum date limits. The idea is that if the user is searching a dataset by date then there seems to be little point letting them choose a date that is out side the range. If the user wants the entire date range they can clear the minimum and/or maximum dates and they will default to (Min) and (Max) for the dataset. It also saves the last selected date range.

I did this years ago for a project which opened external databases and allowed the user to search for dates without the user having to know the precise date range in those databases.

Written in Access 2003 and not tested with any other version.

Chris.
 
I'm not sure if the one I'm using is Allen’s original or not. It is the small picture I attached.
I don't need to limit the dates though it might be good in some cases.
I did made some chnages. The first one was to reorder it to be right to left. I hate it when people use left to right calendars on Hebrew applications :D
The other change I made was to color it based on the colors of the calling form. It create a real nice look. I also made it borderless and added the white in red X to close it.

I used the same date picker to create a bigger calendar (the big picture) that also show events in it. clicking on any date open a form to add/remove events for this date

Text in pictures is in Hebrew but I guess you can get the point

This thread was for the small date picker. I tried to use DoEvents to hold it from updating the opening the form, but users reported it causing them problems.
The Using Eval() thread ( http://www.access-programmers.co.uk/forums/showthread.php?p=1119958 ) was for the big calendar
 

Attachments

  • Image0.jpg
    Image0.jpg
    18 KB · Views: 158
  • Image1.jpg
    Image1.jpg
    73.6 KB · Views: 144

Users who are viewing this thread

Back
Top Bottom