954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with using one form to display data from database in another form.

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

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

Pgmer
Master Poster
714 posts since Apr 2008
Reputation Points: 54
Solved Threads: 121
 
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...

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

how about

dim frmCustomers as New formCustomers
ChrisPadgham
Posting Pro in Training
413 posts since Sep 2009
Reputation Points: 102
Solved Threads: 78
 

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 :(

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

Pgmer
Master Poster
714 posts since Apr 2008
Reputation Points: 54
Solved Threads: 121
 
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...

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Try to Pass value before Close the form Search.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 
Try to Pass value before Close the form Search.


Could you show me how to do that please?

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
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.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 
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 :)

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
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?

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 
Try to hide the form. don't close it. Me.Hide()

No luck..

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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()
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

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..

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

Attachments Test.zip (66.25KB)
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 
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.

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
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...

collin_ola
Junior Poster in Training
54 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

hello!
may be this will help you
Regards

Attachments TestingAnswer.zip (74.24KB)
waqasaslammmeo
Posting Pro in Training
472 posts since Aug 2011
Reputation Points: 38
Solved Threads: 82
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You