Close form on record delete

M

Mike Revis

Hi Group
Access 2000 Win XPpro

I am trying to get my form to close when the current record is deleted.

I have this code in the cmdDeleteRecord_Click
Dim stDocName As String
stDocName = "frmSubPackage"

DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close acForm, stDocName

The delete part works as expected but the form doesn't close.

As always any advice or suggestion is welcome.

Mike
 
T

tina

is frmSubPackage a subform? if so, you can't "close" it while the main form
stays open. you could "hide" it, though, by moving the focus back to the
main form and then setting the subform control's Visible property to False.

if frmSubPackage is open by itself (not as a subform), and you're running
the code from its' module (not from another form's module), then try just
using

DoCmd.Close

that should automatically close the active form.

hth
 
V

Van T. Dinh

1. If the code is in (the context of) the Form you are trying to close,
use:

DoCmd.Close acForm, Me.Name, acSaveNo

This way, you don't need to know the Form's name and it is still correct if
you need to change the Form's name.

2. Do you have any code in the Form_Unload Event that could have cancelled
the unloading?

3. Try adding:

DoEvents between the 2 statements also.
 
M

Mike Revis

Thanks Tina,
The button is on a main form that has a subform.
I also have an exit button that uses DoCmd.Close and it works as expected.
 
M

Mike Revis

Thanks Van,
I have tried both of your suggestions.
The form still won't close.

I don't have any code in unload, deactivate or any other closing events.

I have an exit button that uses DoCmd.Close that works as expected.

Mike
 
T

tina

well, you didn't really answer the question: is frmSubPackage the name of
the main form, or the name of the subform? if it's the name of the main
form, you don't actually need to put the object type or name in the code -
when those arguments are left blank, the active object is closed by default
(see Access Help). i tested my own code

Private Sub Command53_Click()

DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close

End Sub

and it worked - deleted the current record on the active form with the usual
warning message, and then closed the active form. all i can suggest is that
you put a break on your code and step through it to see if you can determine
what is actually happening when you click the button on your form.

hth
 
M

Mike Revis

Sorry about that Tina.
frmSubPackage is the main form.
I have the code exactly as you do below.
It deletes the record but doen't close the form.
I have even commented out all other code in the form so the only code is the
delete code just to see if something else was interfering.
I will read up on putting a break in my code to see if I can figure out how
to do that.

Thanks for your help.

Mike
 
T

tina

an easy way to add a breakpoint: open the form's module, go to the line of
code you want to "break" on, put your cursor anywhere in that line, and
press F9 or on the menu click Debug, Toggle Breakpoint.

hth
 
J

John Vinson

an easy way to add a breakpoint: open the form's module, go to the line of
code you want to "break" on, put your cursor anywhere in that line, and
press F9 or on the menu click Debug, Toggle Breakpoint.

Even easier: mouseclick in the grey bar along the left edge of the
code window.

John W. Vinson[MVP]
 
M

Mike Revis

Hello again,
Thank you all for your patience.
As much as I hate to display my lack of understanding I can't find out what
I'm supposed to do after I have set a break point. Searching vba help for
breakpoint or break point or break yielded some results but I don't
understand what they are telling me.

I set a break point on the line DoCmd.RunCommand acCmdDeleteRecord.
Save
Compile
Close code window
Close form design
With one record showing on the form I click the delete button and the code
window opens with the DoCmd.RunCommand acCmdDeleteRecord line highlighted.

Now What??

One other thing I did was to place the curser on the DoCmd of the
DoCmd.Close line and select quick watch. I got an "Out of Context" Value.

Does that mean anything?


This should probably go in a different thread but I would like to keep it in
the context of my current problem.

Just to check things out I made a new blank database.
I made one table. tblTest with two fields TestID (AutoNumber) and TestNumber
(text).

I then made a form with the wizard.

I then made a button with the following code.
Private Sub cmdDeleteRecord_Click()
On Error GoTo Err_cmdDeleteRecord_Click

DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close

Exit_cmdDeleteRecord_Click:
Exit Sub

Err_cmdDeleteRecord_Click:
If Err.Number = 3021 Then

Resume Exit_cmdDeleteRecord_Click

Else
MsgBox Err.Description
End If
End Sub

I then entered one record and saved it.
I then clicked the delete button.

I got the usual warning and clicked yes and the record was deleted but the
form did not close.

I then entered 8 records.
When I clicked delete on the last record the record deleted but the form did
not close.

I then went to the first record and clicked delete.
The record deleted and the form closed!!

After spending some time playing with this it seems that as long as I am not
on the last, or only, record I get the result I desire. It is only when I am
on the last record that the form doesn't close.

Sorry to be such a pain and I do appreciate all the help I have gotten here
but this doesn't make any sense to me.

Thank you again.

Mike
 
T

tina

i tested my code with two records in the underlying table. i was able to
delete/close the 2nd (last) record, and then the single remaining record,
with no problems. at this point i don't have any bright ideas to offer,
since i can't reproduce the problem you're having. hopefully a wiser soul
than me will step in with an idea that works for you.

btw, when you set a breakpoint to step through code, the next step is to
trigger the code which in your case means clicking the command button. when
the system takes you to the code window, you step through the code a line at
a time by pressing F8 or from the menu bar clicking Debug, Step Into.
according to the last code you posted, if your code generates a 3021 error,
it will exit the sub without showing an error message, any other error
should generate a message box. write down any error code/description you
get, so you can give details in your post.

hth
 
M

Mike Revis

Tina,
Thank you for your efforts.
If nothing else at least I learned something about breakpoints.

Mike
 

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