By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,392 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,392 IT Pros & Developers. It's quick & easy.

Is it possible to excute function from one form to another ?

P: 59
I have a function on one form, and I want when the other form is closed to excute the function on the second form...

this is what I'm trying

Private Function MainFormIsOpen()
MainFormIsOpen = (SysCmd(acSysCmdGetObjectState, acForm, "MainFrm") And acObjStateOpen) <> False
End Function


Private Sub ExitFrm_Click()
On Error GoTo Err_ExitFrm_Click
DoCmd.Close
If MainFormIsOpen() Then
MainFrm.ArrangeDays ("Current")
End If
Exit_ExitFrm_Click:
Exit Sub

Err_ExitFrm_Click:
MsgBox Err.Description
Resume Exit_ExitFrm_Click
End Sub


it gives me an error of:

"Object required"
Sep 2 '07 #1
Share this Question
Share on Google+
5 Replies


puppydogbuddy
Expert 100+
P: 1,923
I have a function on one form, and I want when the other form is closed to excute the function on the second form...

this is what I'm trying

Private Function MainFormIsOpen()
MainFormIsOpen = (SysCmd(acSysCmdGetObjectState, acForm, "MainFrm") And acObjStateOpen) <> False
End Function


Private Sub ExitFrm_Click()
On Error GoTo Err_ExitFrm_Click
DoCmd.Close
If MainFormIsOpen() Then
MainFrm.ArrangeDays ("Current")
End If
Exit_ExitFrm_Click:
Exit Sub

Err_ExitFrm_Click:
MsgBox Err.Description
Resume Exit_ExitFrm_Click
End Sub


it gives me an error of:

"Object required"
You need to leave the parentheses off when you call your function, like this:
If MainFormIsOpen Then

However, in order to call your function from a second form, it needs to be placed in a standard module instead of a form module, and made it Public instead of Private. Then you could call the function from any of your forms.
Sep 2 '07 #2

ADezii
Expert 5K+
P: 8,669
I have a function on one form, and I want when the other form is closed to excute the function on the second form...

this is what I'm trying

Private Function MainFormIsOpen()
MainFormIsOpen = (SysCmd(acSysCmdGetObjectState, acForm, "MainFrm") And acObjStateOpen) <> False
End Function


Private Sub ExitFrm_Click()
On Error GoTo Err_ExitFrm_Click
DoCmd.Close
If MainFormIsOpen() Then
MainFrm.ArrangeDays ("Current")
End If
Exit_ExitFrm_Click:
Exit Sub

Err_ExitFrm_Click:
MsgBox Err.Description
Resume Exit_ExitFrm_Click
End Sub


it gives me an error of:

"Object required"
To expand on puppydogbuddy's explanation:
Expand|Select|Wrap|Line Numbers
  1. Public Function MainFormIsOpen(strFormName As String) As Boolean
  2.   MainFormIsOpen = (SysCmd(acSysCmdGetObjectState, acForm, strFormName) And acObjStateOpen) <> False
  3. End Function
To call this Function:
Expand|Select|Wrap|Line Numbers
  1. If MainFormIsOpen("frmPivot") Then
  2.   'specified Form is Open, process code
  3. Else
  4.   'specified Form is not Open, process code
  5. End If
Sep 2 '07 #3

P: 59
I think you missunderstood me a bit..

the function I'm trying to call is
'ArrangeDays' not 'MainFormIsOpen' (this one works just fine..)

which is already a public function, on another form called main..
thats why I tried main.ArrangeDays() - here I get an error of - "Object required"
and also only ArrangeDays() - and here I get an error of - "compile error: sub or function not defined"

"However, in order to call your function from a second form, it needs to be placed in a standard module instead of a form module, and made it Public instead of Private. Then you could call the function from any of your forms."

its have to be on the form module because it effecting labels on the form itself.. : \

if its a public function why it doesn't work ?
Sep 2 '07 #4

puppydogbuddy
Expert 100+
P: 1,923
I think you missunderstood me a bit..

the function I'm trying to call is
'ArrangeDays' not 'MainFormIsOpen' (this one works just fine..)

which is already a public function, on another form called main..
thats why I tried main.ArrangeDays() - here I get an error of - "Object required"
and also only ArrangeDays() - and here I get an error of - "compile error: sub or function not defined"

"However, in order to call your function from a second form, it needs to be placed in a standard module instead of a form module, and made it Public instead of Private. Then you could call the function from any of your forms."

its have to be on the form module because it effecting labels on the form itself.. : \

if its a public function why it doesn't work ?
You confused us because you posted the function code for 'MainFormIsOpen, not ArrangeDays'. Making a function public in a form module just means it can be called from any procedure on that form. You need to make it public in a standard module to make it callable from any form.
Sep 2 '07 #5

FishVal
Expert 2.5K+
P: 2,653
Hi, zivon.

Just another solution.
Let us say you have form "frm1" being opened on button click in form "frm2".
Button click is not a crucial point here, you may obtain reference to frm1 object in frm2 global variable in any other way.

Variant 1. via declaring event

Form_frm1 module:
Expand|Select|Wrap|Line Numbers
  1. Event FormClose(ByVal strString As String)
  2.  
  3. Private Sub Form_Close()
  4.     RaiseEvent FormClose("Current")
  5. End Sub
  6.  
Form_frm2 module:
Expand|Select|Wrap|Line Numbers
  1. Dim WithEvents frmForm As Form_frm1
  2.  
  3. Private Sub Command11_Click()
  4.     DoCmd.OpenForm "frm1"
  5.     Set frmForm = Forms!frm1
  6. End Sub
  7.  
  8. Private Sub frmForm_FormClose(ByVal strString As String)
  9.     Set frmForm = Nothing
  10.     MsgBox strString
  11. End Sub
  12.  

Variant 2. via declaring property

Form_frm1 module:
Expand|Select|Wrap|Line Numbers
  1. Public Property Get MyProperty() As Variant
  2.     MyProperty = "Current"
  3. End Property
  4.  
Form_frm2 module:
Expand|Select|Wrap|Line Numbers
  1. Dim WithEvents frmForm As Access.Form
  2.  
  3. Private Sub Command11_Click()
  4.     DoCmd.OpenForm "frm1"
  5.     Set frmForm = Forms!frm1
  6. End Sub
  7.  
  8. Private Sub frmForm_Close()
  9.     MsgBox frmForm.MyProperty
  10.     Set frmForm = Nothing
  11. End Sub
  12.  
  13.  
Sep 2 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.