Access closes on running this function (1 Viewer)

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
Hi guys, something funny here.
I have an application designed in ms access 2000 but running in access 2003.I have designed a toolbar with the buttons to open forms.
I have functions that open the form. Here is a sample function

Code:
Public Function openMyForm()
    On Error Resume Next
    With DoCmd
            .SelectObject acForm, Screen.ActiveForm.Name
            .Close acForm, Screen.ActiveForm.Name
            .openForm "my form"
        End With
End Function
"my Form" being the name of the form i want opened.
But when i click on the button access generates the message
"Sorry access has encountered a problem and needs to close"
This is happening on some machines running the application but others are Ok

All help appreciated.
Cheers
 

Guus2005

AWF VIP
Local time
Today, 16:59
Joined
Jun 26, 2007
Messages
2,641
Remove "On Error Resume Next" and see what error you get.

Ofcourse there are more ways to generate the same result.
You want to close all open forms and open "my form"?

Code:
public function CloseAllForms()
    dim i as integer

    for i = 0 To Forms.Count - 1
        docmd.close acform, Forms(i).Name
    Next i

end function
so your code could look like this:
Code:
Public Function openMyForm()
    CloseAllForms
    DoCmd.openForm "my form"
End Function

Enjoy!
 

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
Thanks Guus I will try your solution and see how it works
Cheers
 

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
Hi Guus2005 I have used your function but an error saying that the number assigned to the form is invalid poped up and access hang.
I had to use Task Manager to exit the application.
Anything wrong with the Function
 

Guus2005

AWF VIP
Local time
Today, 16:59
Joined
Jun 26, 2007
Messages
2,641
Don't believe there is something wrong with the code. But there might be a typo.

Please post a zipped sample database.
 

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
this application is a bit large and split in to two a back end and a front end.
But still I can't know whats wrong here because on my development computer when i use this function
Code:
Public Function openMyForm()
    On Error Resume Next
    With DoCmd
            .SelectObject acForm, Screen.ActiveForm.Name
            .Close acForm, Screen.ActiveForm.Name
            .openForm "my form"
        End With
End Function
every thing works neatly but on my clients machine its generating the error access has encountered a problem and needs to close.
Both machines are running on Windows Xp Professional and have Ms Access 2003 installed on them.
 

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
Oh something else
This is a sample fuction accessed by buttons in a toolbar and the logic close the active form and open the form that the button on the toolbar opens in this sample case "myForm"
Cheers
 

Guus2005

AWF VIP
Local time
Today, 16:59
Joined
Jun 26, 2007
Messages
2,641
Oh something else
This is a sample fuction accessed by buttons in a toolbar and the logic close the active form and open the form that the button on the toolbar opens in this sample case "myForm"
Cheers
Que?

What are you saying?
 

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
Sorry Guus2005 I was not clear on that logic part.
I have a custom toolbar with four buttons.
I also have four main forms which are opened using the buttons in the toolbar.
What am trying to achieve is, when a user clicks a button in the toolbar the active form is closed and the form associated with the button opens.
In my application there is a welcome form that opens an application launch and is always open unless the user clicks a button on the toolbar. After the user is through with the form opened by the toolbar button and closes it the welcome screen opens ,so there is no such time that there will not be an active form hence the need to close the active form first and then open whichever form the user wants through the buttons on the toolbar.
Hope am clear and thanks for your help man
Cheers
 

Guus2005

AWF VIP
Local time
Today, 16:59
Joined
Jun 26, 2007
Messages
2,641
Your code should look like this:
Code:
Public Function openMyForm()
    CloseAllForms
    Docmd.openForm "my form"
End Function
Regardless of what forms are open, they will all be closed.

I feel like i'm repeating myself and am not sure if your problem is solved already.

HTH:D
 

coyote

Registered User.
Local time
Today, 07:59
Joined
Oct 8, 2007
Messages
149
Guus2005 I appreciate all your help.
The function you supplied is working when the database window is showing.
When I run the application with the database window option box unchecked then am getting this message

Runtime Error '2456'
The number you used to refer to the form is invalid

and then access hangs.

This is an application where I have customized the toolbar and menus and hidden the Access toolbars,menus and database window.
 

Guus2005

AWF VIP
Local time
Today, 16:59
Joined
Jun 26, 2007
Messages
2,641
Perhaps you modified Access in such a way that you're not able to see the code?

Enable all access toolbars, menu's and database window until you are done programming.

Run it again.
 

Users who are viewing this thread

Top Bottom