PDA

View Full Version : Is there vba code to search for a specific record on a form?



wedd
01-05-2012, 09:51 AM
Hi, experts!

Is there part of my code I can change to search for a specific record on my form? At the moment when the user enters the a number it takes through a series of next steps; I would prefer if the user just enters in the ID number and it automatically displays the record rather than going through several steps to get to that record. Can this be done, if so please advise me? My code is listed below. I am a beginner/intermediate user of vba access.


Thanks for your contributions :friends:


Private Sub Text313_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[CustomerID]=" & txt313
If rs.NoMatch Then
MsgBox "Sorry, no such record exists '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.Recordset.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub

HiTechCoach
01-05-2012, 11:54 PM
The code you posted is exactly how you should do it. I use that exact method.

TIP: Give your controls a more meaningful name than Text313.

Looks like you did not finish editing the code from the control txtGoTo to your control Text313.

Try:

Private Sub Text313_AfterUpdate()

If (Text313 & vbNullString) = vbNullString Then Exit Sub

Dim rs As DAO.Recordset

Set rs = Me.RecordsetClone

rs.FindFirst "[CustomerID]=" & txt313

If rs.NoMatch Then
MsgBox "Sorry, no such record exists '" & Text313 & "' was found.", _
vbOKOnly + vbInformation
Else
Me.Recordset.Bookmark = rs.Bookmark
End If

rs.Close

Text313 = Null

End Sub