DoCmd.Close-Access Shuts Down

D

Deb Smith

When I close a form using a command button on the form Access shuts
down saying "....Access encountered a problem and needs to close......".
This error does not occur if I use the close in the toolbar or on the menu.


My form is comprised of a main form and a subform. What am I doing
wrong or what would be causing this problem to occur? I have tried using the
following code to close the form with my command button.

DoCmd.Close

I have also tried DoCmd.Close acForm, "formname"

I really need help with this. The ability to have command buttons on
my main form that control both the main and subform actions are quite
important to me.

Thanks in advance.
 
M

MikeC

Deb,

You can try putting the below code in the click event for
your command button. You'll need to replace each of the 5
instances of "YourCommandButton" in the below code with
the actual name of your command button.

I've included some error handling that should capture any
useful error number/text info in the event the error
recurs. I'm assuming that you are placing the code in the
module for the form that has the command button and not
some other module. I'm also assuming that you are not
running additional code in the form's close, unload or
other post-close type event.

'---------------------------
Private Sub YourCommandButton_Click()
On Error GoTo Err_YourCommandButton_Click

DoCmd.Close acForm, Me.Name

Exit_YourCommandButton_Click:
Exit Sub

Err_YourCommandButton_Click:
MsgBox "Module: " & vbTab & vbTab & Me.Name & vbCrLf _
& "Procedure #: " & vbTab & "1" & vbCrLf _
& "Error #: " & vbTab & vbTab & Err.Number & vbCrLf _
& "Description: " & vbTab & Err.Description
Resume Exit_YourCommandButton_Click

End Sub
'---------------------------
In the event the error does recur, you should also
consider setting a break point in your code so that you
can step through each line of code leading up to the error
and then you may get a better idea about the cause.
During this process, you can check the values of any
variables, etc. that are being used the code as it is
being executed.
 
A

Allen Browne

Hi Deb

I'm not sure if there is a problem with your installation of Access, or with
the database being partially corrupt. Either way, this sequence should solve
it and give you a stable working database.

1. Visit http://support.micorosoft.com, the Downloads section, and check
that you have the latest service pack for your version of Office (SP3 for
Access 2000 or 2002, or SP1 for Access 2003).

2. Very important, check you have the JET 4 service pack as well. Locate
msjet40.dll on your computer (typically in windows\system32). Right-click,
and choose Properties. On the Version tab, you should see 4.0.8xxx.0. If you
don't see the 8 there, download and apply the JET 4 Service Pack 8.

3. After applying the service packs, decompile the database by entering
something like this at the command prompt while Access is not running. It is
all one line, and include the quotes:
"c:\Program Files\Microsoft office\office\msaccess.exe" /decompile
"c:\MyPath\MyDatabase.mdb"

4. Open the database, and uncheck the boxes under:
Tools | Options | General | Name AutoCorrect.
Explanation:
http://members.iinet.net.au/~allenbrowne/bug-03.html

5. Compact the database to get rid of the decompiled stuff and the name
autocorrect stuff:
Tools | Database Utilities | Compact.
 
D

Deb Smith

Thanks you for the suggestions, I tried all suggestions including
decompiling and compiling and nothing worked. I finally recreated a whole
new form replacing the subform that was problematic, this seems to have
worked.

Something must have been corrupt but for the life of me I can't figure out
what. Thanks again for the suggestions.

Now that I can get my form and subform to close via a command button on my
form, I do have another question.

When I attempt to add a delete and undo button to the main, they do not
work. However, if I use the menu/toolbar to accomplish this task it works.

In the case of the delete button I get an error message #3200 "The record
cannot be deleted or changed because table 'tblGift' includes related
records.(tblGift is the table used as the record source for the subform.) I
obviouisly need to put some code behind the button that identifies I want to
delete the selected record in the subform. I am not sure how to do this.

In the case of undo, once again I can complete the task from the
menu/taskbar however from a command button on my form I get message telling
me to complete the required data entry for the record (This is an error
message I have set up in the subform). If I want to undo a record, how do I
get the focus moved to the main form so I can complete the undo task?

Thanks again for your help to date. I really would appreciate some
suggestions on how to deal with the additional command buttons.
 
A

Allen Browne

These buttons won't work, because of the order of the events.

If you are editing a text box in the subform, and click your Undo button on
the main form, these events fire:
1. The form's Error event if the value in the text box is not appropriate.

2. The events of the text box being edited, e.g. Change, BeforeUpdate,
AfterUpdate, Exit, Lost Focus.

3. The events of the subform, e.g. Form_BeforeUpdate, Form_AfterUpdate.

4. The Click event of the button on the main form.

Clearly, any code you put in the Click event will not be able to undo the
values in the subform.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top