Typically, you would use an unbound Combo. The Combo control is ideally
suited for doing this because it has the Autoexpand property which, when set
to Yes, does the "type ahead" thing and allows the user to find the value
quickly without having to type in the entire value. It also has the Not In
List property which either prevents or allows users to enter values that are
not in the row source of the combo. This is usually used to allow adding new
records to a table.
The code I posted earlier for the text box will work for the combo as well.
Here is an example of the Not In List event that allows for adding new
records. For this to work, the combo's Limit To List property needs to be
set to Yes so this event will fire
Private Sub cboActivity_NotInList(NewData As String, Response As Integer)
Dim rst As Recordset
If MsgBox(NewData & " Is Not In The Attribute Table " & vbNewLine _
& "Do you want to add it", _
vbInformation + vbYesNo, "Not Found") = vbYes Then
Me.cboActivity = Me.cboActivity.OldValue
'Add the new record with the key field.
CurrentDb.Execute ("INSERT INTO CISAttributeTable (ACTIVITY) " _
& "VALUES ('" & NewData & "');"), dbFailOnError
Me.Requery
Set rst = Me.RecordsetClone
rst.FindFirst "[Activity] = '" & NewData & "'"
Me.Bookmark = rst.Bookmark
Set rst = Nothing
Me.txtDescription.SetFocus
Response = acDataErrAdded
Else
Me.cboActivity.Undo
Response = acDataErrContinue
End If
End Sub
Matt Dawson said:
Klatuu,
How would i usually have a search then? The only way i could get it to work
was to unbound it and have it as a text box. I am sure there was an easier
way e.g. Bound by combo or something but every time i tried to type in the
search it said i was duplicating data.
Sprinks,
Where would i enter this? ONLoad?
:
It sounds like you are using the form's recordset to do the search, so when
nothing is found, it ends up at the last record. It is unusual to use a text
box for a search, but if you must, I would suggest you check for the value is
in the table before you start messing with the form's recordset. I would
suggest the Before Update of your search text box.
Dim rst As Recordset
Set rst = Me.RecordsetClone
rst.FindFirst "[SearchField] = '" & Me.txtSearchFor & "'"
If rst.NoMatch Then
MsgBox "Record Not Found"
Cancel = True
Else
Me.Bookmark = rst.Bookmark
End If
Set rst = Nothing.
:
Access 2003,
I currently have a form which is used to search for and update records. The
field from which i search is a text box and is unbound and the search is by
Request ID.
When i type in a search which is not in the list, instead of keeping the
blank record which isbegins with, it brings up the first record. Hence, the
agents, if they are not paying enough attentino will not realise that the
record is incorrect.
If my table is called Acceptance and search field called Combo24 (not a
combo box), what code would I need to right in order to stop this from
happening.
Matt
(Klatuu, our old convo did not finish, apologies)