please i need a code that can help me search records from a table in vb.

please i need a code that can help me search records from a table in vb.

check out this code. this code used the followings :-

DATABASE : student.mdb
TABLE : details
FIELDS : roll, name
DB LOCATION : same as the project folder
OUTPUT CONTROLS : txtrollno (for holding roll no) and txtname (for holding name)

replace those in the above with your own stuffs

Dim db As Database
Dim rs As Recordset
Dim str As String, confirm As Integer

Set db = OpenDatabase(App.Path & "\student.mdb")
Set rs = db.OpenRecordset("details", dbOpenTable)
If rs.RecordCount > 0 Then rs.MoveFirst

begin:
str = InputBox("Enter student roll no :", "Search Student")
If Trim(str) = "" Then
    confirm = MsgBox("No roll no. has been supplied." & vbCrLf & "Do you wish to abort searching?", _
    vbQuestion + vbYesNo, "Search")
    If confirm = vbNo Then
        GoTo begin
    Else
        Set rs = Nothing
        Exit Sub
    End If
Else
    Set rs = db.OpenRecordset("select * from details where roll='" & Trim(str) & "'", dbOpenDynaset)
    If rs.RecordCount > 0 Then
        txtrollno.Text = rs!roll
        txtname.Text = rs!Name
    Else
        confirm = MsgBox("No student with RollNo. " & str & " has been found." & vbCrLf & "Would you like to re-search?", _
            vbQuestion + vbYesNo, "Result")
        If confirm = vbNo Then
            Set rs = Nothing
            Exit Sub
        Else
            GoTo begin
        End If
    End If
End If

let me know if this helps you.

regards
Shouvik

Nice one...

For searching it is always better to use LIKE than "="

i want to search command from excel to vb, unfortunately still not found, kindly help me. i'm using excel workbook as my database. i don't know how to create


workbook database= MasterTrackDB.xls
sheet1=ProRegister

Please start your own thread if you have a new question

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.