hi. I am working on a project that requires a search button. it goes like this. the I will enter data on a text box. then the record that goes with it will display on the 3 textboxes. My problem is I don't know how to code a search buttons. please help.

Dani AI

Generated

Good call by and to use a SELECT with LIKE. One subtle gotcha that often causes the errors hit: when you query Access from VB6 through ADO (Jet OLE DB 4.0), LIKE uses ANSI-92 wildcards, so use % and _ (not and ?). That is different from the Access UI/DAO default, which uses and ?. In other words, search for names with 'And%' rather than 'And*'. This difference is by design when using the Access Database Engine via the OLE DB provider. (learn.microsoft.com)

If you want to fill three textboxes from the first matching record and avoid problems with quotes in names (e.g., O'Connor), use a parameterized query:

Option Explicit

Private Sub cmdSearch_Click()
    On Error GoTo Fail

    Dim cn As ADODB.Connection, cmd As ADODB.Command, rs As ADODB.Recordset
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\MySearch.mdb;"  ' Jet 4.0 provider

    Set cmd = New ADODB.Command
    Set cmd.ActiveConnection = cn
    cmd.CommandText = "SELECT TOP 1 [Name],[Surname],[Phone] FROM [MySearch] WHERE [Name] LIKE ?"
    cmd.CommandType = adCmdText
    cmd.Parameters.Append cmd.CreateParameter("pName", adVarChar, adParamInput, 50, Trim$(txtSearch.Text) & "%")

    Set rs = cmd.Execute
    If rs.EOF Then
        MsgBox "No match found."
    Else
        txtName.Text = rs!Name & ""
        txtSurname.Text = rs!Surname & ""
        txtPhone.Text = rs!Phone & ""
    End If

CleanUp:
    On Error Resume Next
    If Not rs Is Nothing Then rs.Close
    cn.Close
    Exit Sub
Fail:
    MsgBox "Search failed: " & Err.Description
    Resume CleanUp
End Sub

Notes:

  • Add a reference to Microsoft ActiveX Data Objects 2.x in VB6.
  • Bracket field/table names that might be reserved (e.g., [Name]).
  • Connection uses the Jet OLE DB 4.0 provider syntax shown in Microsoft docs. (learn.microsoft.com)

Recommended Answers

All 13 Replies

What kind of database are you using, access, MySql etc?

Could you give us some sample data on what kind of data you were searching?

-> for example: like typing the Customer ID on a textbox, then perform search and display the customer info. such as last name, first name and middle name on these three texboxes...(Are these is what you are trying to do?)


regarding the coding of your search button, if it has a back-end database, then use the SELECT statement to query results....within your recordset object...

SELECT CustomerID, lastname, firstname FROM tblCustomers WHERE lastname LIKE 'V%'

-> this select statement will show result those customers that have a last name starting with the "V" letter.

I'm sorry I forgot. it's ms access 2003


@poisoned heart the data that will be typed on the text box is a name, then the it would display some selected information of the name that was searched.

Your select statement would be -

SELECT * FROM MyTableName WHERE TheName LIKE " & "'" & "Prob%" & "'"

This will return all names in the database that has prob in itself.

Have a look at the following link -

http://www.techonthenet.com/sql/like.php

@andre are you sure this will work in vb6? because when I use this code there is always an error even when I play around the code.

Do the following -

Option Explicit

Private Sub cmdSearch_Click()

'Declare connection and recordset
Dim cnSearch As ADODB.Connection
Dim rsSearch As ADODB.Recordset

'Create a new instance of connection and recordset
Set cnSearch = New ADODB.Connection
Set rsSearch = New ADODB.Recordset

'Open connection
cnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\MySearch.MDB;Persist Security Info=False"
  
'Open recordset
Dim strSql As String

strSql = "SELECT * FROM MySearch WHERE Name LIKE " & "'" & "And%" & "'"

rsSearch.Open strSql, cnSearch, adOpenStatic, adLockOptimistic

'See if any records exist. If not exit sub and close connection and
'recordset. If yes, display data in text boxes

If rsSearch.BOF = True Or rsSearch.EOF = True Then
    MsgBox "No records exist"
    
    rsSearch.Close
    cnSearch.Close
    
    Exit Sub
        Else
    Text1.Text = rsSearch!Name
    Text2.Text = rsSearch!Surname
    
    rsSearch.Close
    cnSearch.Close
End If
End Sub

This works 100% within VB6 AND Access.

OK thank you! I got it! it works perfectly fine!!!!!!!! THANKS a LOT!!!!!!!!!!!!!

Only a pleasure. Happy coding.

i really like to get information in your web site...it really helps..thanks...im philip,new member..

hi.....im jennylyn,i would like to ask you guys if what would be a code for SEARCH in database....like if we would find the name or gender????pls post a code guys....need it in our activity!!!!!!thanks..............:)

hi.....im jennylyn,i would like to ask you guys if what would be a code for SEARCH in database....like if we would find the name or gender????pls post a code guys....need it in our activity!!!!!!thanks..............

hi.....im jennylyn,i would like to ask you guys if what would be a code for SEARCH in database....like if we would find the name or gender????pls post a code guys....need it in our activity!!!!!!thanks..............

Post your own thread here. Also post the code you working on.

Quoted Text Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.