docmd.Close won't close... sometimes

mikewood1980

Registered User.
Local time
Today, 11:02
Joined
May 14, 2008
Messages
45
Hi
I have a save button on my form which when clicked adds a record to a form, then closes the form.

I thought the following code would work:

Private Sub save_Click()

AddRecord
DoCmd.close

End Sub

However, it adds the record and doesnt close the form... if I comment out the AddRecord function, it closes fine. WHY?!!

Thanks in advance for your help
MikeW
 
a. Where did 'AddRecord' come from?
b. Why would you add a record and then close the form with out entering in any data?
 
Thanks Ken
a) AddRecord is a function that i have written to append the contents of the form to various tables... (ie data the user has entered). This function does what it is supposed to do and works fine...
b) The idea behind closing the form is to save the data then close so that no additional records can be accidentally duplicated

Thanks
Mike
 
Hum...

Maybe if you threw in a DoEvents in between the AddRecord and the DoCmd.Close it would work.
 
Ok - I've tried that but its still not working!!! I wonder if its some sort of bug?
 
In your AddRecord function, do you have any open error traps? Have you tried stepping through to see what happens in AddRecord that leaves you short of triggering the .Close event?
 
Simple Software Solutions

You could try to move the DoCmd.Close to the end of your AddRecord Function

Such as..

DoCmd.Close "YourFormName"
 
I think you need to post your AddRecord function code so we can take a peek there - :)
 
Dcrake's idea is good. If you explicitly state the form's name in the Close statement, then you can be sure that it is that form that Access is trying to close. I vaguely remember hainv this problem myself and discovered Access was closing something else. The solution was to add the formname to the Close command.

HTH,
Chris
 
I always use this generic close code for forms when the code is on the form you are wanting to close. You don't need to modify it in any way:

DoCmd.Close acForm, Me.Name, acSaveNo

The Me.Name takes care of explicitly naming the form based on its actual name using the Me keyword to refer to the current class. And, the acSaveNo just makes sure I don't get any questions about saving any design changes for the form, which can occur occasionally with filter changes, etc. The acSaveNo has nothing to do with saving records.
 
I think you need to post your AddRecord function code so we can take a peek there - :)

Heres the AddRecord Function:

Public Sub AddRecord()

Dim dbCurr As Database
Set dbCurr = DBEngine.Workspaces(0).Databases(0)
Dim rsTest As Recordset
Set rsTest = dbCurr.OpenRecordset("tblTest")





rsTest.AddNew
rsTest("txtData").Value = Me.OuterBoard1
rsTest("txtData2").Value = Me.InnerBoard1
rsTest.Update

Me.Refresh
rsTest.close

End


End Sub

It is adding the records into the database fine, but I wonder if it is getting stuck somewhere?

I tried the suggestion of adding DoCmd.close "formname", but this didnt make any difference! :(

If anyone has any ideas I would be really pleased!!

Thanks
MikeW
 
I would suggest stepping through the code and see where it hangs up at - :)
 

Users who are viewing this thread

Back
Top Bottom