I am really hoping someone can help me because I have spent a whole day trying to resolve this one with no success
I have a simple two table database. The first table is Staff which keeps staff details including which company they work for. The second is a list of companies. The two tables are linked together using a combo containing the unique companyID number.
When adding a new member of staff the user must assign the staff member to a company using the dropdown list on the form. If the company doesn't exist yet then there is an 'Add' button next to the combo box which opens a form to add the new company details. On exiting the company form it auto populates the combo field with the new company name. This all works fine if the new company form is populated by tabbing between fields as eventually the BeforeUpdate code is called which then gives the user the usual save options yes, no and cancel. On choosing yes or no the AfterUpdate is activated which refreshes the combo before the DoCmd.close.... closes the form and returns the use to the Staff form without having to manually close the company form which would be showing a blank record.
The problem I have is that some of the users like to use a mouse and so I have had to put an cmd'Exit' button on the form so that they can exit that way. Unfortunately, when the button is pressed I get an error code of 2501 'The close action was cancelled' and the Debug then shows the DoCmd.Close line in the AfterUpdate sub as being the problem
Is there a way to close the form by using this button whilst still having the option of bypassing the new blank record stage that occurs if I remove the DoCmd.Close statement from the AfterUpdate Sub?
Here is the code for the three subs being used
Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Would you like to save these details?", vbQuestion _
+ vbYesNoCancel + vbDefaultButton1, "Save New Company Details???")
Case vbYes 'saves new record
Case vbNo
Me.Undo 'undo the changes to create blank record
Me.txtCompanyName.SetFocus 'moves focus to top of form
Case vbCancel 'leaves the user to edit their changes
Cancel = True ' returns user to form with data still intact
Me.txtCompanyName.SetFocus 'moves focus to top of form
End Select
End Sub
Private Sub Form_AfterUpdate()
Me.Refresh
With Forms!frmNewStaff.cboCompanyID
.Requery
.SetFocus
.Text = Me.txtCompanyName
End With
DoCmd.Close acForm, "frmNewCompany", acSaveYes
End Sub
Private Sub cmdExit_Click()
DoCmd.Close
Exit Sub