K
Kevin Sprinkel
In a database tracking our firm's estimates of
construction costs vs. actual contractor bids, we have a
detail table with the following structure:
tblBidDetails
Field Type Example(s)
----------- --------- ----------
BidDetailID AutoNumber 1
JobNumber Text "00003"
Category Text "HVAC", "Electrical", "Demolition"
Amount Currency $2,900,00
Divisor Number 3,500
DivisorUnit Number "SF", "Parking Space", "Apartment"
Note that Category is a text field.
Category is entered through a combo box on a continuous
subform tied to a main form by JobNumber. The lookup
tblCategory List has a Text primary key. To allow for
special values (about 10% of the data), I had set the
Limit to List property to No.
Even though there will only be a few thousand records per
year, it seems inelegant and potentially error-prone to
store this category as text, particularly since we'll be
doing a lot of report selection and grouping by this
field. So I'd like to change the lookup table key to an
Autonumber. Then, if the user enters a value that's not
in the list, I want to use the Not In List event to give
him the option of saving the new value to the lookup table
or just storing it as special text in another field.
I've tested the following code, and it successfully adds a
new record to the lookup table if the user selects "Yes"
from the MsgBox, and writes the value correctly to the
SpecialCategory field if he/she selects "No". However, in
the latter case, Access displays the message on completion
of the procedure "The text you entered is not a value in
the list. Select an item from the list, or enter text the
matches one of the listed items." What I'd like to have
happen is to just move to the next control as if I'd
entered a listed value.
Any ideas?
Thank you for any and all help.
Private Sub cboCategory_NotInList(NewData As String,
Response As Integer)
On Error Resume Next
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strMsg As String
strMsg = "'" & NewData & " is not in the stored Category
list. " & vbCrLf & vbCrLf
strMsg = strMsg & "Add to lookup table? "
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add to List or
Store Special Text?") = vbNo Then
With Me.cboCategory
.Value = Null
End With
With Me.txtSpecialCategory
.Value = NewData
End With
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("tblCategoryList",
dbOpenDynaset)
On Error Resume Next
rs.AddNew
rs!Category = NewData
rs!CSIIndex = 99
rs.Update
If Err Then
MsgBox "An error occurred. Please try again."
Response = acDataErrContinue
Else
Response = acDataErrAdded
End If
' Clean up
rs.Close
Set rs = Nothing
Set db = Nothing
End If
End Sub
construction costs vs. actual contractor bids, we have a
detail table with the following structure:
tblBidDetails
Field Type Example(s)
----------- --------- ----------
BidDetailID AutoNumber 1
JobNumber Text "00003"
Category Text "HVAC", "Electrical", "Demolition"
Amount Currency $2,900,00
Divisor Number 3,500
DivisorUnit Number "SF", "Parking Space", "Apartment"
Note that Category is a text field.
Category is entered through a combo box on a continuous
subform tied to a main form by JobNumber. The lookup
tblCategory List has a Text primary key. To allow for
special values (about 10% of the data), I had set the
Limit to List property to No.
Even though there will only be a few thousand records per
year, it seems inelegant and potentially error-prone to
store this category as text, particularly since we'll be
doing a lot of report selection and grouping by this
field. So I'd like to change the lookup table key to an
Autonumber. Then, if the user enters a value that's not
in the list, I want to use the Not In List event to give
him the option of saving the new value to the lookup table
or just storing it as special text in another field.
I've tested the following code, and it successfully adds a
new record to the lookup table if the user selects "Yes"
from the MsgBox, and writes the value correctly to the
SpecialCategory field if he/she selects "No". However, in
the latter case, Access displays the message on completion
of the procedure "The text you entered is not a value in
the list. Select an item from the list, or enter text the
matches one of the listed items." What I'd like to have
happen is to just move to the next control as if I'd
entered a listed value.
Any ideas?
Thank you for any and all help.
Private Sub cboCategory_NotInList(NewData As String,
Response As Integer)
On Error Resume Next
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strMsg As String
strMsg = "'" & NewData & " is not in the stored Category
list. " & vbCrLf & vbCrLf
strMsg = strMsg & "Add to lookup table? "
If MsgBox(strMsg, vbQuestion + vbYesNo, "Add to List or
Store Special Text?") = vbNo Then
With Me.cboCategory
.Value = Null
End With
With Me.txtSpecialCategory
.Value = NewData
End With
Else
Set db = CurrentDb
Set rs = db.OpenRecordset("tblCategoryList",
dbOpenDynaset)
On Error Resume Next
rs.AddNew
rs!Category = NewData
rs!CSIIndex = 99
rs.Update
If Err Then
MsgBox "An error occurred. Please try again."
Response = acDataErrContinue
Else
Response = acDataErrAdded
End If
' Clean up
rs.Close
Set rs = Nothing
Set db = Nothing
End If
End Sub