Ok here is the required feature:

To send data gathered from a database inside a UserControl to the main form which houses the UserControl.

Hierarchy at present:


The UserControls are generated on the fly by a client list in a database. This all works fine.
When you click on the UserControl, the UserControl gathers the information associated with the client and stores them in to strings.

What should happen is the strings are then passed and set on screen to the main form, but this doesn't happen.

There is no real point in me posting code from the main window as the code there is working and does exactly what it should be, however below is the code that shows what should happen when the UserControl is clicked on.

Public Function get_ind_data(indclid As String)
        Dim firstname, lastname, middlename, postcode, address1, address2, county, country, email, compname, phone, clientid, pass As String
            Dim ind_fetcher As New MySqlConnection("REMOVED")
            Using ind_command As New MySqlCommand("SELECT * FROM clients WHERE clid='" + indclid + "'", ind_fetcher)
                Using ind_reader As MySqlDataReader = ind_command.ExecuteReader()
                    While ind_reader.Read()
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_firstname")) Is DBNull.Value) Then
                            firstname = ind_reader.GetString(ind_reader.GetOrdinal("cl_firstname"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_lastname")) Is DBNull.Value) Then
                            lastname = ind_reader.GetString(ind_reader.GetOrdinal("cl_lastname"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_middlenames")) Is DBNull.Value) Then
                            middlename = ind_reader.GetString(ind_reader.GetOrdinal("cl_middlenames"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_postcode")) Is DBNull.Value) Then
                            postcode = ind_reader.GetString(ind_reader.GetOrdinal("cl_postcode"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_address_1")) Is DBNull.Value) Then
                            address1 = ind_reader.GetString(ind_reader.GetOrdinal("cl_address_1"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_address_2")) Is DBNull.Value) Then
                            address2 = ind_reader.GetString(ind_reader.GetOrdinal("cl_address_2"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_county")) Is DBNull.Value) Then
                            county = ind_reader.GetString(ind_reader.GetOrdinal("cl_county"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_country")) Is DBNull.Value) Then
                            country = ind_reader.GetString(ind_reader.GetOrdinal("cl_country"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_email")) Is DBNull.Value) Then
                            email = ind_reader.GetString(ind_reader.GetOrdinal("cl_email"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_compname")) Is DBNull.Value) Then
                            compname = ind_reader.GetString(ind_reader.GetOrdinal("cl_compname"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("cl_telephone")) Is DBNull.Value) Then
                            phone = ind_reader.GetString(ind_reader.GetOrdinal("cl_telephone"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("clientid")) Is DBNull.Value) Then
                            clientid = ind_reader.GetString(ind_reader.GetOrdinal("clientid"))
                        End If
                        If (Not ind_reader.GetString(ind_reader.GetOrdinal("clpass")) Is DBNull.Value) Then
                            pass = ind_reader.GetString(ind_reader.GetOrdinal("clpass"))
                        End If
                    End While
                    ims_submodule_directory.lbl_title_clname.Text = firstname + " " + lastname
                    MessageBox.Show(firstname + " " + lastname, "DEBUG globalclasses.vb On line 120")
                End Using
            End Using
        Catch ex As MySqlException
        End Try
    End Function

I would very much be thankful for any advice, I have been racking my brains out for the past two weeks now.

Thanks a bunch

First piece of advice turn on Option Eplicit andOption Strict. You're calling the sub routine a Function but it has no return type nor is it returning anything.

To access a field or property inside a class it must be declared as Public. Use that instead of Dim. Now the form can access the property through the instance of the class(UserControl1)

As said before, the issue is not with this function, the function although it doesn't return a value, it works as intended. The problem is with line 49, when it trys to update the text on screen on the main form (ims_submodule_directory). The form is not allowing changes by the user control.

As said before, the process is as follows:

Database query runs and creates usercontrols for each value. In the usercontrols once clicked, it should update text on the main form with the rest of the data that was brought across when the query ran. This is what fails. The userControl seems to have no access to the main form. What I am asking is if anyone can simply explain the way of accessing the main form to allow changes whilst in runtime.


Does the messagebox show?

Yes it does.


Is the Click event handler for the control, part of the control or is it in the form class?

Hi, yes the click event is triggered from and is in the UserControl. For reference, here is the UserControl code.

Imports MySql.Data.MySqlClient
Public Class clientlist_individual
    Dim ind As New globalclasses

    Private Sub clientlist_individual_Click(sender As Object, e As EventArgs) Handles MyBase.Click
        Dim clid As String = Me.Name
    End Sub

    Private Sub btn_arrow_Click(sender As Object, e As EventArgs) Handles btn_arrow.Click
        Dim clid As String = Me.Name
        ind.get_ind_data(Me.Name) **'THIS IS WHERE THE FUNCTION IS TRIGGERED**
    End Sub

    Private Sub btn_arrow_MouseHover(sender As Object, e As EventArgs) Handles btn_arrow.MouseHover
        btn_arrow.BackColor = Color.DarkBlue
        btn_arrow.ForeColor = Color.White
    End Sub

    Private Sub btn_arrow_MouseLeave(sender As Object, e As EventArgs) Handles btn_arrow.MouseLeave
        btn_arrow.BackColor = Color.Transparent
        btn_arrow.ForeColor = Color.SteelBlue
    End Sub

    Private Sub clientlist_individual_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub
End Class

As you can see, in the click event, the function is run, the function works as it should but cannot set the data in the form ims_submodule_directory.


Solved, I didn't realise that when I was creating the submodule form from the dashboard, it was doing it "As New FORMCLASSNAME" instead of "FORMCLASSNAME", therefore creating a new private instance only visible to the dashboard each time, which is why I couldn't change anything. This has been solved, thanks for your contributions.

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.