stephaniem
Registered User.
- Local time
- Today, 09:21
- Joined
- Dec 26, 2012
- Messages
- 12
Hey all. I am trying to use a form to search/add records. Using a combobox notinlist event, if a user enters a division that does not currently have a record in the database, the code should ask if they want to add it, and then take the data from the form and create a new record. I get an "Object Required" error after confirming the request to add a record. I've been looking at this and can't find the error. Maybe some new eyes can help?
Table(DivisionsandLocations) and Form(frmCompanyAddUpdate) have fields called "Company" and "Division").
Private Sub Division_NotInList(NewData As String, Response As Integer)
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim Msg As String
Dim NewID As String
On Error GoTo Err_Division_NotInList
' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub
' Confirm that the user wants to add the new customer.
Msg = "'" & NewData & "' is not an existing Division." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox "Please try again."
Else
' If the user chose to add a new divison, open a recordset
' using the DivisionsandLocations table.
Set Db = CurrentDb
Set Rs = Db.OpenRecordset("DivisionsandLocations", dbOpenDynaset)
' Create a new record.
Rs.AddNew
Rs("Company").Value = frmCompanyAddUpdate.Company.Value
Rs("Division").Value = frmCompanyAddUpdate.Division.Value
' Save the record.
Rs.Update
' Set Response argument to indicate that new data is being added.
Response = acDataErrAdded
End If
Exit_Division_NotInList:
Exit Sub
Err_Division_NotInList:
' An unexpected error occurred, display the normal error message.
MsgBox Err.Description
' Set the Response argument to suppress an error message and undo
' changes.
Response = acDataErrContinue
End Sub
Table(DivisionsandLocations) and Form(frmCompanyAddUpdate) have fields called "Company" and "Division").
Private Sub Division_NotInList(NewData As String, Response As Integer)
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim Msg As String
Dim NewID As String
On Error GoTo Err_Division_NotInList
' Exit this subroutine if the combo box was cleared.
If NewData = "" Then Exit Sub
' Confirm that the user wants to add the new customer.
Msg = "'" & NewData & "' is not an existing Division." & vbCr & vbCr
Msg = Msg & "Do you want to add it?"
If MsgBox(Msg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add a customer, set the Response
' argument to suppress an error message and undo changes.
Response = acDataErrContinue
' Display a customized message.
MsgBox "Please try again."
Else
' If the user chose to add a new divison, open a recordset
' using the DivisionsandLocations table.
Set Db = CurrentDb
Set Rs = Db.OpenRecordset("DivisionsandLocations", dbOpenDynaset)
' Create a new record.
Rs.AddNew
Rs("Company").Value = frmCompanyAddUpdate.Company.Value
Rs("Division").Value = frmCompanyAddUpdate.Division.Value
' Save the record.
Rs.Update
' Set Response argument to indicate that new data is being added.
Response = acDataErrAdded
End If
Exit_Division_NotInList:
Exit Sub
Err_Division_NotInList:
' An unexpected error occurred, display the normal error message.
MsgBox Err.Description
' Set the Response argument to suppress an error message and undo
' changes.
Response = acDataErrContinue
End Sub