Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DoCmd.Close doesn't work. Form reopens. 1

Status
Not open for further replies.

xicana

Technical User
Dec 11, 2003
100
US
I have a database that saves customer price lists. On the main form (ListMaintenance), i have a button to add a new item to the list. A blank bound (to pricelist_detail) form (AddNewItem) pops up that contains all the fields associated with an item and the user fills in the appropriate information. Once they're done, there is a save button (see below code) that saves the info and should close the pop-up form, but the form seems to close and reopen, becausese I see a quick flash of the main form and then I get the add new item form open and ready to recieve info.

there must be something else that is keeping the form from closing or is causing it to reopen - is there some sort of form property that can cause this?

This is what happens when the AddItem button is clicked on the main form:
Code:
Private Sub cmdAddItem_Click()
    DoCmd.Save
    DoCmd.OpenForm "PriceList_AddNewItem"
    Forms!PriceList_AddNewItem!PriceListID = Forms!PriceList_Maintenance!PriceListID
    
End Sub

This is the code behind the Save button in the AddNewItem pop up form:

Code:
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

    CheckBreaks   'blank out the breaks with "[Break]" as heading
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.OpenQuery "3_1_CreateRecordInTable3"
    DoCmd.OpenQuery "3_2_UpdateBreakNamesInBreakTable"
    Forms!PriceList_Maintenance.Refresh
    DoCmd.Close acForm, "PriceList_AddNewItem"
    
Exit_cmdSave_Click:
    Exit Sub

Err_cmdSave_Click:
    MsgBox Err.Description
    Resume Exit_cmdSave_Click
    
End Sub

I must say that I don't mind it reopening (and neither do the users), since it allows the them to keep on adding new items without having to keep on clicking on add new item button. The reason it's a problem now is because after they click on save and they get a blank form again, the focus is set on a field in the middle of the form instead of up at the top where it should be. i thought it was going to be a simple SetFocus issue, but when I realized that the form would automatically reopen, I tried setting the focus to the first field on form load but that didn't make a difference. Does this mean that form never actually closes? if not, why not if I tell it to close in my code?

This was a little difficult for me to explain, if I need be a little more clear, please let me know.



Sandy
 
I created a new "Save" button copied over the code from the old one that seemed to bug out added a few lines that would reopen the form to add a new item and voila! it works the way it should now.
Code:
Private Sub cmdSaveNew_Click()
On Error GoTo Err_cmdSaveNew_Click

    CheckBreaks   'blank out the breaks with "[Break]" as heading
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.OpenQuery "3_1_CreateRecordInTable3"
    DoCmd.OpenQuery "3_2_UpdateBreakNamesInBreakTable"
    Forms!PriceList_Maintenance.Refresh
    DoCmd.Close acForm, "PriceList_AddNewItem"
    DoCmd.OpenForm "PriceList_AddNewItem"
    Forms!PriceList_AddNewItem!PriceListID = Forms!PriceList_Maintenance!PriceListID
    
    
Exit_cmdSaveNew_Click:
    Exit Sub

Err_cmdSaveNew_Click:
    MsgBox Err.Description
    Resume Exit_cmdSaveNew_Click
    
End Sub

I'd still like to know why my old save button would follow my code...



Sandy
 
If anyone is interested...

Looks like I found why it was acting strangely, at one point this customer had one of their employees modify the DB app I had created and apparently he put in a macro in the On Lost Focus and On Got Focus of the Save Button:

Macro:
SetWarnings = No
Sendkeys = {Enter}
Sendkeys = {Enter}
Sendkeys = {Enter}
Sendkeys = {Enter}

I'm guessing maybe once the pop up form closed, the focus was set on AddNewItem on the main form and when the macro used the SendKeys function, it actually similuted a click on the button, thus the pop-up reopening. The extra Sendkeys made the focus move to the third field on the pop up form (this is why I couldn't set the focus to the first field when the form loaded).

Just FYI

Sandy
 
EEK! Get rid of the send keys and also replace this line:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

with

DoCmd.RunCommand acCmdSaveRecord

The DoMenuItem syntax has been deprecated and is only in there for backwards compatibility, but you should use the current syntax.


Bob Larson
A2K,A2K3,A2K7,SQL Server 2000/2005,Crystal Reports 10/XI,VB6, WinXP, and Vista
Free Quick Tutorials and Samples:
 
Thanks boblarson,

I did get rid of the sendkeys - I'll replace the other line like you suggested.

Thanks again

Sandy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top