Hi everyone
I have a form for adding a new agency (ageny, address, city, programinfo, etc) and the combo box lists all subsidiaries (child component of main agency). For now users can select one and the record will update, however if a new subsidiary and its info (address, city, email, etc) needs to be added, I thought I'd try experimenting with the Not In List property of the combo box. This is the code I have so far:
Not In List combo box property:
Code:
Private Sub cboSubs_NotInList(NewData As String, Response As Integer)
Response = AddNewToList(NewData, "tblSubsidiaryINFO2", "Subsidiary", "Address", "City", "Prov", "PostalCode", "FirstName", "LastName", "Position", "ContactNotes", "OriginalEmail", "PhoneNumber", "PhoneNotes", "frmAddSubsidiary")
End Sub
Module in DB
Code:
Option Compare Database
Public Function AddNewToList(NewData As String, stTable As String, _
stFieldName As String, strPlural As String, _
Optional strNewForm As String) As Integer
On Error GoTo err_proc
'Adds a new record to a drop down box list
'If form name passed, then open this form to the newly created record
'Declare variables
Dim rst As DAO.Recordset
Dim IntNewID As Long
Dim strPKField As String
Dim strMessage As String
' Display message box asking if user wants to add the new item
strMessage = "'" & NewData & "' is not in the current list. " & Chr(13) & Chr(13) & _
"Do you want to add it to the list of " & strPlural & "?" & Chr(13) & Chr(13) & _
"(Please check the entry before proceeding)."
If MsgBox(strMessage, vbYesNo + vbQuestion + vbDefaultButton2, "Add New Data") = vbYes Then
Set rst = CurrentDb.OpenRecordset(stTable, , dbAppendOnly)
rst.AddNew
rst(stFieldName) = NewData 'Add new data from combo box
strPKField = rst(0).Name 'Find name of Primary Key (ID) Field
rst.Update
rst.Move 0, rst.LastModified
IntNewID = rst(strPKField)
'if a form specified, then open the form with the primary key equal to the new record ID as the criteria
If strNewForm <> "" Then DoCmd.OpenForm strNewForm, , , strPKField & "=" & IntNewID, , acDialog
AddNewToList = acDataErrAdded 'Set response 'Data added'
Else
AddNewToList = acDataErrContinue 'Set response 'Data NOT added'
End If
exit_proc:
On Error Resume Next
rst.Close
Set rst = Nothing
Exit Function
err_proc:
MsgBox "Error in Function: 'AddNewToList'" & Chr(13) & Err.Description, , "Function Error"
Resume exit_proc
End Function
Note: the following code I borrowed from examples online. When I go back to the form and I start typing something in that isn't in the list, I get this error
And the following code is highlighted yellow:
Private Sub cboSubs_NotInList(NewData As String, Response As Integer)
Response = AddNewToList(NewData, "tblSubsidiaryINFO2", "Subsidiary", "Address", "City", "Prov", "PostalCode", "FirstName", "LastName", "Position", "ContactNotes", "OriginalEmail", "PhoneNumber", "PhoneNotes", "frmAddSubsidiary")
End Sub
Also I've set data entry and allow additions on the addsubisidaryform to No. How can I make sure what I've done actually did anything? Thoughts?