While a form is open...... (1 Viewer)

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
I am trying to program access to keep a form1 invisible, while form2 is open. What sort of code should I use to state:

Dim isopen as String
isopen = "form2"

If isopen is open then
Forms![form1].Visible = False
Else
Forms![form1].visible = True
End If

Can someone help me finish off this code? I believe that I would put in in the on_current event of form2?

Thnx
 

D-Fresh

Registered User.
Local time
Today, 09:14
Joined
Jun 6, 2000
Messages
225
Your easiest way to do this is to hide Form1 in the OnOpen event of Form2. Then unhide Form1 in the OnClose event of Form2. Hope this helps.

Doug
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Doug,

I am currently using the solution you suggested, but it doesn't work properly. As I have a main form which opens two other forms. A search form and a record form. The thing is that the search form also opens up the record form.
So my problem is, if I open up the Searchform from the mainform, the mainform hides. Thats good. Then I open up the recordform from the searchform. But when I close the recordform to go back to the searchform, it unhides the mainform.
Thus the mainform comes out on top of the search form, when it still needs to be hidden.
Does anyone have any suggestions to how I can get around this?
Thnx
 

Drew

Registered User.
Local time
Today, 09:14
Joined
Aug 18, 2001
Messages
41
Hi Irie,

Dislaimer: I don't actually use the below anymore but i did a while back, hence the bad error handling amongst other things. Basicaly it uses the tag of the open form to remember where it was called from so it can go back to it when this one is closed. You want to use the OpenAForm code on the button you call the next form with and the BackAForm code on either another a close button or, more likely, on the Close event of the second form. Please take this as an example of some code, and not the general std of my code


Code:
Public Function OpenAForm(strFormName As String) As Integer
On Error GoTo OpeningError
 Dim strHide As String
   strHide = Screen.ActiveForm.Name
   Let errTag = Screen.ActiveForm.Name
   Screen.ActiveForm.Visible = False
   DoCmd.OpenForm strFormName, acNormal, , , , acWindowNormal
  'DoCmd.Maximize
   Screen.ActiveForm.Tag = strHide
   Let errTag = Empty
Exit Function

OpeningError:
  MsgBox ("OpenAForm!!! Unable to open form " & strFormName & Chr$(13) _ 
& "Please advise admin of the below message and the function you are trying to access: " & Chr$(13) _
& Chr$(13) & "Error Number: " & Err.Number & Chr$(13) & Chr$(13) & "Description : " & Err.Description)
  
End Function

Public Function BackAForm(strFormName As String) As Integer
On Error GoTo DisplayErr
Dim strForm As String
Dim strUnHide As String

Let strForm = Screen.ActiveForm.Name
Let strUnHide = Screen.ActiveForm.Tag
DoCmd.Close acForm, strForm
DoCmd.SelectObject acForm, strUnHide
'DoCmd.Maximize
Exit Function
DisplayErr:
      MsgBox ("Unable to close the current screen or open next screen " & Chr$(13) _
 & "Please advise admin of the below message and the function you are trying to access: " _ 
& Chr$(13) & Chr$(13) & "Error Number: " & Err.Number & Chr$(13) & Chr$(13) & "Description : " & Err.Description)
      DoCmd.OpenForm strForm
      
End Function

HTH

Drew

[This message has been edited by Drew (edited 08-20-2001).]
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Thanks Doug for the code,
I am trying to incorporate the idea of using the tag into my code.

If anyone else has a suggestion or comment please reply,

Thnx
 

DJBummy

Registered User.
Local time
Today, 09:14
Joined
Jun 22, 2001
Messages
90
Seems to me you should only have to use the

DoCmd.SelectObject acForm, "SearchForm"

before going back to the SearchForm

[This message has been edited by DJBummy (edited 08-21-2001).]
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
DjBummy,

YOur suggestion works if I have originally arrived at the record form from the searchform. But if I arrive at the recordform directly from the mainform.
Then I get an access error stating that the searchform is not open.

So my question is....

Is it possible to create an IF statement that declares....

If searchform is open Then
mainform.visible = false
Else
mainform.visible = true
End If

Any suggestions would be greatly appreciated,
thnx
 

D-Fresh

Registered User.
Local time
Today, 09:14
Joined
Jun 6, 2000
Messages
225
Here is a function that will check if a form is open and loaded. If so, the function will return true.

Function fIsLoaded(ByVal strFormName As String) As Integer
'Returns a 0 if form is not open or a -1 if Open
If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
If Forms(strFormName).CurrentView <> 0 Then
fIsLoaded = True
End If
End If
End Function


You should be able to use the syntax...

Dim isopen as String
isopen = "form2"

If fIsLoaded(isopen) then
Forms![form1].Visible = False
Else
Forms![form1].visible = True
End If

You could probably use a while...wend statment as well in form1 like so

me.visible = false
docmd.openform(isopen)

while fIsLoaded(isopen)
DoEvents
wend

me.visible = true

'This last code will hide your main form, open the secondary form, and then loop until the secondary form is closed. When closed, it will break out of the loop and show the form.

Hope one of these solutions works out for you. Let me know if you need anything else.


Doug

[This message has been edited by D-Fresh (edited 08-22-2001).]
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Doug,

THANK YOU, THANK YOU....

Absolutely Brilliant!!!!

One less problem thats been buggin me for weeks.

Thnx
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Doug,

One last question?

How can I duplicate your code so that it will check to see if two different forms are open?
Do I need to create a new function for the other form?

Say:

Function fIsLoaded2(ByVal strFormName2 As String) As Integer
'Returns a 0 if form is not open or a -1 if Open
If SysCmd(acSysCmdGetObjectState, acForm, strFormName2) <> 0 Then
If Forms(strFormName2).CurrentView <> 0 Then
fIsLoaded = True
End If
End If
End Function

The reason I ask, is that the records form can be opened from three different places... And like I explained earlier about the searchform, the same goes for another for called renewals.

Thnx
 

D-Fresh

Registered User.
Local time
Today, 09:14
Joined
Jun 6, 2000
Messages
225
nah, you don't have to recreate the function. All you have to do is incorporate each form into the while statement...


dim Form1 as string, Form2 as string, Form3 as string
Form1=Form1Name
Form2=Form2Name
Form3=Form3Name

while fisloaded(Form1) or fisloaded(Form2) or fisloaded(Form3)
DoEvents
Wend

This will check to see if any of the three forms are open... If any one of the three are open, it will continue looping. I think I understood you correctly, so I hope this helps you. If I misunderstood you, just let me know.

Doug
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Doug,

I think I have done the code correctly, but could you check what I have, as I am getting errors....

Dim form1 As String, form2 As String
form1 = "search"
form2 = "renewalform"

While fIsLoaded(form1) Or fIsLoaded(form2)
DoEvents
Wend

If fIsLoaded(form1) Then
Forms![entryform].Visible = False
Forms![Search].Visible = True
Else
Forms![entryform].Visible = True
End If

If fIsLoaded(form2) Then
Forms![entryform].Visible = False
Forms![Renewalform].Visible = True
Else
Forms![entryform].Visible = True
End If


When I try to exit from the recordsform after having come from the searchform I get the message:

"The Close Action Was Canceled:
You used a Method of DoCmd Object to carry out an action in Visual basic, but then clicked Cancel in a dialog box."

I am then stuck in that form without the ability to close it, and if I try to change to design view I get a ton or errors.

Thnx
 

Drew

Registered User.
Local time
Today, 09:14
Joined
Aug 18, 2001
Messages
41
sorry to chip back in again, but <soapbox> this is why it's better to use the tag, that way it doesn't matter what form calls another, it'll just go back to the last one. You don't need any more code to cope with it and you can change the name of your forms and only have to change the open statement. If you're hard coding form names to return to you're making a rod for your own back</soapbox>

thanks for reading!

Drew
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Drew,
I have attempted to use what you suggested, but I am unclear as to how I should call the function.
Could you show me the code for this...
Thnx
 

Drew

Registered User.
Local time
Today, 09:14
Joined
Aug 18, 2001
Messages
41
Hi Irie,

sorry for the delay in responding, i thought i'd tidy the code a little before i came back to you. Here's the slightly better version

Code:
Public Sub OpenAForm(strFormName As String)
On Error GoTo OpeningError

 Dim strHide As String
 
   strHide = Screen.ActiveForm.Name
   Screen.ActiveForm.Visible = False
   DoCmd.OpenForm strFormName, acNormal, , , , acWindowNormal
   Screen.ActiveForm.Tag = strHide
   
Exit Sub

OpeningError:
        'something appropriate
  
End Sub

Public Sub BackAForm()
On Error GoTo DisplayErr
Dim strForm As String
Dim strUnHide As String

    strForm = Screen.ActiveForm.Name
    strUnHide = Screen.ActiveForm.Tag
    DoCmd.Close acForm, strForm
    DoCmd.SelectObject acForm, strUnHide

Exit Function
DisplayErr:
      'something appropriate followed by back to the current form
      DoCmd.OpenForm strForm
      
End Function

OpenAForm replaces the DoCmd.OpenForm ... and you call it as - OpenAForm "FormName". Then on your button to go back to the previous form you use BackAForm. You don't need to pass anything into it as it will get the previous form name from the tag on the active form,

hope that makes it a bit clearer, post back if you have any problems

Drew

[This message has been edited by Drew (edited 08-24-2001).]
 

Irie

Registered User.
Local time
Today, 09:14
Joined
Jul 12, 2001
Messages
27
Thanks Drew for the Code...
I managed to get it partially working, I am now trying to get it to work for all my forms.
Thnx Again,
Irie
 

Users who are viewing this thread

Top Bottom