Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim stid As String
Dim lc As Integer
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Student.mdb;"
cmd.ActiveConnection = cn
stid = InputBox("Enter Student ID")
If Trim(stid) <> "" Then
'Method 1 - Open a recordset based on a table
rs.Open "studenttable", cn, adOpenKeyset, adLockPessimistic, adCmdTable
Do While Not rs.EOF
If rs.Fields(0).Value = CInt(stid) Then
MsgBox "Student ID : " & rs.Fields(0).Value & " Name : " & rs.Fields(1).Value
Exit Do
End If
lc = lc + 1
rs.MoveNext
Loop
If lc = rs.RecordCount Then MsgBox "Match not found"
Set rs = Nothing
'Method 2 - Open a recordset based on a command object
cmd.CommandType = adCmdText
cmd.CommandText = "select * from studenttable where StudentID = " & CInt(stid)
Set rs = cmd.Execute
While Not rs.EOF
MsgBox "Student ID : " & rs.Fields(0).Value & " Name : " & rs.Fields(1).Value
rs.MoveNext
Wend
Set rs = Nothing
'Method 3 - Open based on SQL statement
rs.Open "select * from studenttable where StudentID = " & CInt(stid), cn, , , adCmdText
While Not rs.EOF
MsgBox "Student ID : " & rs.Fields(0).Value & " Name : " & rs.Fields(1).Value
rs.MoveNext
Wend
End If
Set rs = Nothing
Set cmd = Nothing
cn.Close
================================================================================================
You can also refer to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdobjodbrec.asp for more information about Recordset.
Hope this helps