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

You need to create object of that form first before accessing its controls.

You need to create object of that form first before accessing its controls.

I tried

Dim formCustomers As New formCustomers

But that doesn't seem to work...

is it because the class name and the instance name are the same.

how about

dim frmCustomers as New formCustomers

is it because the class name and the instance name are the same.

how about

dim frmCustomers as New formCustomers

I gave it a try. It's still not working :(

Is ur class public? formCustomers?
If yes then u should be able to access its behaviours.

Is ur class public? formCustomers?
If yes then u should be able to access its behaviours.

The class is public, yeah, but it's still not working. I'll play around with it a little more and see if I can get it to work...

Try to Pass value before Close the form Search.

Try to Pass value before Close the form Search.

Could you show me how to do that please?

formCustomers.txtFirstName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(2)
formCustomers.txtLastName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(3)
Me.Close()

Actually you can pass value if form search still active.

formCustomers.txtFirstName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(2)
formCustomers.txtLastName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(3)
Me.Close()

Actually you can pass value if form search still active.

Oh, okay. I'm mot currently in front of a computer, but I'll give it a try when I can and get back to you as soon as possible :)

formCustomers.txtFirstName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(2)
formCustomers.txtLastName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(3)
Me.Close()

Actually you can pass value if form search still active.

I tried this, and it didn't work either :(. Any other suggestions?

Try to hide the form. don't close it. Me.Hide()

Try to hide the form. don't close it. Me.Hide()

No luck..

Well..its strange. it always works for me.
did you have made the object of form customer?

Dim frmCustomers as New formCustomers

frmCustomers.txtFirstName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(2)
frmCustomers.txtLastName.Text = dSet.Tables("Customers").Rows(incr + 1).Item(3)
Me.Hide()

I don't quite get your question, but if you're asking whether I used

Dim frmCustomers as New formCustomers

Then yes, I did, but it still refuses to work..

I don't really know why it doesn't work for you.
This a simple test of passing text.

I don't really know why it doesn't work for you.
This a simple test of passing text.

Thank you, I'll take a look at it as soon as possible.

I don't really know why it doesn't work for you.
This a simple test of passing text.

I took a look at your test and compared it to mine...they're exactly the same but don't seem to do the same thing. I've also ensured that all classes and subs are Public, but still no result...

hello!
may be this will help you
Regards

Thanks everyone, for all your help, I've managed to sort the problem out. :D

Thanks everyone, for all your help, I've managed to sort the problem out. :D

Great Job collin_ola. So, Would you like to share the answer..?
It will help another member who has the same problem like you. :D

Thank you :)

Yeah, the problem was that I had the form that I was trying to pass data into open (not hidden, actually open). So the data was actually there; it just wasn't showing. I solved this by ensuring that the form I wanted to display data into wasn't open, and that it was re-opened when I clicked the search button and a record was found :)

Thanks again, for all your help.

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.