Hi,
I seem to be having a problem with displaying data in a second form. I've created a linear search algorithm which works just fine - it locates the records from the database. I now want to be able to display the record in another form, I've attempted this using the code below but it doesn't seem to work:
Public Class formSearch
Dim searchItem As String
Dim currentRecord As Integer
Dim Provider As String
Dim Source As String
Dim connection As New OleDb.OleDbConnection
Dim sqlCode As String
Dim dAdapter As New OleDb.OleDbDataAdapter
Dim dSet As New DataSet
Dim maxRows, incr As Integer
Dim recordFound As Boolean = False
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
If Len(txtSearch.Text) <> 3 Then
MsgBox("Please enter a valid customer ID.")
txtSearch.Clear()
Exit Sub
End If
Provider = "PROVIDER=MICROSOFT.ACE.OLEDB.12.0;"
Source = "Data Source = F:\My Documents\Computing\COMP4\COMP4 CW\WND.accdb"
sqlCode = "SELECT * FROM Customers"
connection.ConnectionString = Provider & Source
connection.Open()
sqlCode = "SELECT * FROM Customers"
dAdapter = New OleDb.OleDbDataAdapter(sqlCode, connection)
dAdapter.Fill(dSet, "Customers")
connection.Close()
searchItem = txtSearch.Text
maxRows = dSet.Tables("Customers").Rows.Count - 1
incr = -1
Do
If searchItem = dSet.Tables("Customers").Rows(incr + 1).Item(0) Then
MsgBox("Record Found.")
recordFound = True
Me.Close()
' formCustomers.txtFirstName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(2)
' formCustomers.txtLastName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(3)
Exit Sub
ElseIf recordFound = False Then
incr = incr + 1
End If
Loop Until maxRows = incr Or recordFound = True
If recordFound = False And maxRows = incr Then
MsgBox("No record found")
End If
End Sub
End Class
The commented out lines are the ones that don't seem to work. The "txtfirstname" and "txtlastname" fields are located in the coding of another form, called "formcustomers". Any help would be greatly appreciated.
ps. (The connection to the database works correctly, so that can't be the error)
Collin