Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Button for a database ..

Status
Not open for further replies.

tjbvo

Technical User
Nov 8, 2002
4
0
0
ZA
I have created an ADO database (VB6).
Everything is working fine, but I need to add a Search Button, to search for existing records, and then to display (move to ) the relevant record using the same database.

Attached please find the code I am using, but it has faults ??

Private Sub cmdSearch_Click()
'The % means all Records that starts with example an A must be Find.
datPrimaryRS.Recordset.Close
datPrimaryRS.Recordset.Source = "Select * from [HRM] where Persal No like " & txtSearch.Text & " order by Persal No"
datPrimaryRS.Recordset.Open

If datPrimaryRS.MaxRecords = 0 Then
datPrimaryRS.Recordset.Close
datPrimaryRS.RecordSource = "Select * from [HRM] order by Persal No"
datPrimaryRS.Recordset.Open
If MsgBox("No Records were Found. Find Again...", vbYesNo + vbExclamation, "No Records...") = vbYes Then
chkSearch.Value = Checked
txtSearch.SetFocus

Else
chkSearch.Value = Unchecked
txtSearch.Text = &quot;<<Type Name Here>>&quot;
End If

Else: MsgBox &quot;Records Found...&quot;, vbInformation, &quot;Found...&quot;
chkSearch.Value = Unchecked
txtSearch.Text = &quot;<<Type Name Here>>&quot;
End If

End Sub

Thanks
tjbvo (Nexes)
 
The only thing that leaps out at me is that if your field name has a space in it, you must enclose it in brackets, like this: [Persal No].

But yes, what is the exact problem?

Regards,

z.
 

Use the rs.Find method instead of opening and closing a recordset several times...

It may be best to use a clone recordset and then set the main recordset's bookmark property to the clone's bookmark if a record was found.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top