The example below shows how to build the code to go into the command button
on your form. It assumes there are 2 unbound text boxes where the user
enters the part number and the revision. These unbound boxes are named
txtFindPartNumber and txtFindRevision. I like to put these and the command
button (cmdFindPart) into the Form Header section, so they are visibly
separated from the bound controls on the form.
The code assumes the fields in your table are named PartNumber and Revision,
and that both are Text fields. If they are Number fields, lose the extra
quotes, e.g.:
strWhere = strWhere & "([Revision] = " & Me.txtFindRevision & ") AND "
The code is crafted so that you can easily extend the technique to include
more search boxes if desired.
It saves the record first. Then it builds the search string from whichever
boxes the user typed in. It locates the record in the RecordsetClone of the
form. If found, it sets the form to that bookmark which shows the record. If
not found, you get a MsgBox.
Private Sub cmdFindPart_Click()
Dim strWhere As String
Dim lngLen As Long
If Me.Dirty Then 'Save first
Me.Dirty = False
End If
If Not IsNull(Me.txtFindPartNumber) Then
strWhere = strWhere & "([PartNumber] = """ & Me.txtFindPartNumber &
""") AND "
End If
If Not IsNull(Me.txtFindRevision) Then
strWhere = strWhere & "([Revision] = """ & Me.txtFindRevision & """)
AND "
End If
'Chop off the trailing " AND ".
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "Find what?"
Else
strWhere = Left$(strWhere, lngLen)
'Find the record in the form's clone set
With Me.RecordsetClone
.FindFirst strWhere
If .NoMatch Then
MsgBox "Not found"
Else
'Show the record in the form
Me.Bookmark = .Bookmark
End If
End With
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
Sheila said:
I am using Access 97. I have a form that has a part number field and a
revision field that are populated from a table where I enter the data. The
part number could be listed several times with different revisions. I
would
like a commad button to ask "Part Number" and "Revision" and then find
that
record in the form. Sorry if this isn't detailed enough, but I'm not sure
what else I would need to tell you. Thanks in advance for any and all
help.