instructorTek
Instructor
- Jun 5, 2006
- 61
Hi all I have a database created in Access 2002 with a table called tblStudent with the fields, FirstName, LastName , Address and Class. I'm creating a search form so that the user can enter the student's LastName and the database will pull up the student's info.
I tried to do this but although there may be several students with the same last name only the first matching record in the table is displayed. The form comes up with only one record as the result. Can you help with this?
Code:
Option Compare Database
Option Explicit
Dim dbCurr As ADODB.Connection
Dim rstUser As ADODB.Recordset
Dim strConnect As String
Private Sub cmdFind_Click()
'Set and open the recordsets for users and user groups
Set rstUser = New ADODB.Recordset
Set dbCurr = New ADODB.Connection
'Connection to the database
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentDb.Name & ";Persist Security Info=False"
'Open the DB connection
dbCurr.Open strConnect
'Open the recordset
rstUser.Open "SELECT * FROM tblStudent WHERE LastName = '" & txtSearch.Value & "' ", dbCurr, adOpenDynamic, , -1
'assign values to textboxes as a result of textbox choice
If Not rstUser.EOF Then
If txtSearch.Value = rstUser.Fields("LastName").Value Then
txtFirstName.Value = rstUser.Fields("FirstName").Value
txtLastName.Value = rstUser.Fields("LastName").Value
txtClass.Value = rstUser.Fields("Class").Value
txtAddress.Value = rstUser.Fields("Address").Value
End If
End If
If rstUser.State = adStateOpen Then rstUser.Close
End Sub
I tried to do this but although there may be several students with the same last name only the first matching record in the table is displayed. The form comes up with only one record as the result. Can you help with this?
Code:
Option Compare Database
Option Explicit
Dim dbCurr As ADODB.Connection
Dim rstUser As ADODB.Recordset
Dim strConnect As String
Private Sub cmdFind_Click()
'Set and open the recordsets for users and user groups
Set rstUser = New ADODB.Recordset
Set dbCurr = New ADODB.Connection
'Connection to the database
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentDb.Name & ";Persist Security Info=False"
'Open the DB connection
dbCurr.Open strConnect
'Open the recordset
rstUser.Open "SELECT * FROM tblStudent WHERE LastName = '" & txtSearch.Value & "' ", dbCurr, adOpenDynamic, , -1
'assign values to textboxes as a result of textbox choice
If Not rstUser.EOF Then
If txtSearch.Value = rstUser.Fields("LastName").Value Then
txtFirstName.Value = rstUser.Fields("FirstName").Value
txtLastName.Value = rstUser.Fields("LastName").Value
txtClass.Value = rstUser.Fields("Class").Value
txtAddress.Value = rstUser.Fields("Address").Value
End If
End If
If rstUser.State = adStateOpen Then rstUser.Close
End Sub