R
Robert Neville
I have been developing a complex combo box for several weeks. On the
surface, it sounds like a straight forward NotinList event, yet the
combo box is more intricate. The combo box resides on the address
sub-form (sfrmCompAddr), which is on a Company form (frmComp). The
address sub-form's record source links the company and address table
through a relationship table (trelCompAddr).
The combo box should allow the user to select a previous entered
address, thus update the link; it should allow the user to edit the
data rather than update the link; or it should add a new address
record and update the new reference link to the new record. The
complexity rises from several objective. First, I do not open a
separate Edit form, yet one confirmation dialog is fine. Second, I
rather avoid adding toggle buttons and check boxes for the form to
carry out the desired action (this form should look and interact like
a traditional form). Finally, I rather avoid multiple prompts for the
user to press OK/Cancel.
Currently, the NotinLIst code asks "Do you want to add the New Data?"
- Yes or No If you select Yes, the code adds the new data and updates
the link in trelCompAddr. If you select No, then the code prompt the
user again with a message that says "Item not in List and will not be
added" - OK. Ideally, the Msgbox would prompt the user asking whether
he/she wants to ADD or EDIT data; then do the action and shut up. I
primarily use my form to fix inconsistencies in the data by picking
previously entered data and updating the link; basing the edit from
another previously entered address (like copy and paste); or adding a
need record and establishing a link.
The preliminary code lies below. This code only adds a new record and
updates the link; or updates the link. It does not edit the data as
desired.
******************Code Starts**************************
Private Sub cboAddr1_Change()
Dim strResponse As String
Dim strSQL As String
Dim rstAddr1 As DAO.Recordset
Dim lngAddrID As Long
If cboAddr1.Column(1) = "(add new)" Then
strResponse = InputBox("Enter new Address 1", "Create new
address", "")
If strResponse <> "" Then
cboAddr1.Undo
strSQL = "INSERT INTO tblAddr (Addr1) "
strSQL = strSQL & "SELECT '" & strResponse & "' AS Addr1 "
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "SELECT Max(tblAddr.AddrID) AS MaxOfAddrID FROM
tblAddr;"
Set rstAddr1 = CurrentDb.OpenRecordset(strSQL)
lngAddrID = rstAddr1!MaxOfAddrID
rstAddr1.Close
Set rstAddr1 = Nothing
cboAddr1.Requery
cboAddr1 = lngAddrID
End If
End If
End Sub
******************Code End**************************
******************Code Starts************************
Private Sub cboAddr1_Enter()
If Me.Dirty = True Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
End If
End Sub
******************Code End**************************
******************Code Starts************************
Private Sub cboAddr1_NotInList(NewData As String, Response As Integer)
If MsgBox("Do you wish to create a new address """ & NewData &
"""", vbYesNo) = vbYes Then
Dim strSQL As String
Dim rstAddr1 As DAO.Recordset
Dim lngAddrID As Long
Response = 0
cboAddr1.Undo
strSQL = "INSERT INTO tblAddr (Addr1) "
strSQL = strSQL & "SELECT '" & NewData & "' AS Addr1 "
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "SELECT Max(tblAddr.AddrID) AS MaxOfAddrID FROM
tblAddr;"
Set rstAddr1 = CurrentDb.OpenRecordset(strSQL)
lngAddrID = rstAddr1!MaxOfAddrID
rstAddr1.Close
Set rstAddr1 = Nothing
cboAddr1.Requery
cboAddr1 = lngAddrID
End If
End Sub
******************Code End**************************
surface, it sounds like a straight forward NotinList event, yet the
combo box is more intricate. The combo box resides on the address
sub-form (sfrmCompAddr), which is on a Company form (frmComp). The
address sub-form's record source links the company and address table
through a relationship table (trelCompAddr).
The combo box should allow the user to select a previous entered
address, thus update the link; it should allow the user to edit the
data rather than update the link; or it should add a new address
record and update the new reference link to the new record. The
complexity rises from several objective. First, I do not open a
separate Edit form, yet one confirmation dialog is fine. Second, I
rather avoid adding toggle buttons and check boxes for the form to
carry out the desired action (this form should look and interact like
a traditional form). Finally, I rather avoid multiple prompts for the
user to press OK/Cancel.
Currently, the NotinLIst code asks "Do you want to add the New Data?"
- Yes or No If you select Yes, the code adds the new data and updates
the link in trelCompAddr. If you select No, then the code prompt the
user again with a message that says "Item not in List and will not be
added" - OK. Ideally, the Msgbox would prompt the user asking whether
he/she wants to ADD or EDIT data; then do the action and shut up. I
primarily use my form to fix inconsistencies in the data by picking
previously entered data and updating the link; basing the edit from
another previously entered address (like copy and paste); or adding a
need record and establishing a link.
The preliminary code lies below. This code only adds a new record and
updates the link; or updates the link. It does not edit the data as
desired.
******************Code Starts**************************
Private Sub cboAddr1_Change()
Dim strResponse As String
Dim strSQL As String
Dim rstAddr1 As DAO.Recordset
Dim lngAddrID As Long
If cboAddr1.Column(1) = "(add new)" Then
strResponse = InputBox("Enter new Address 1", "Create new
address", "")
If strResponse <> "" Then
cboAddr1.Undo
strSQL = "INSERT INTO tblAddr (Addr1) "
strSQL = strSQL & "SELECT '" & strResponse & "' AS Addr1 "
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "SELECT Max(tblAddr.AddrID) AS MaxOfAddrID FROM
tblAddr;"
Set rstAddr1 = CurrentDb.OpenRecordset(strSQL)
lngAddrID = rstAddr1!MaxOfAddrID
rstAddr1.Close
Set rstAddr1 = Nothing
cboAddr1.Requery
cboAddr1 = lngAddrID
End If
End If
End Sub
******************Code End**************************
******************Code Starts************************
Private Sub cboAddr1_Enter()
If Me.Dirty = True Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
End If
End Sub
******************Code End**************************
******************Code Starts************************
Private Sub cboAddr1_NotInList(NewData As String, Response As Integer)
If MsgBox("Do you wish to create a new address """ & NewData &
"""", vbYesNo) = vbYes Then
Dim strSQL As String
Dim rstAddr1 As DAO.Recordset
Dim lngAddrID As Long
Response = 0
cboAddr1.Undo
strSQL = "INSERT INTO tblAddr (Addr1) "
strSQL = strSQL & "SELECT '" & NewData & "' AS Addr1 "
CurrentDb.Execute strSQL, dbFailOnError
strSQL = "SELECT Max(tblAddr.AddrID) AS MaxOfAddrID FROM
tblAddr;"
Set rstAddr1 = CurrentDb.OpenRecordset(strSQL)
lngAddrID = rstAddr1!MaxOfAddrID
rstAddr1.Close
Set rstAddr1 = Nothing
cboAddr1.Requery
cboAddr1 = lngAddrID
End If
End Sub
******************Code End**************************