calling sub from two forms

luca.p

New member
Joined
Oct 2, 2010
Messages
2
Programming Experience
Beginner
Hi to everyone,
I have a little question for this forum, hoping someone may help me.
I have a sub (stored in a module.vb) called from two forms.
Subroutine simply fill a combobox with a database records. I need to fill another combobox (with the same name) in another form.
Code is following:
VB.NET:
Public Sub listNames()
Dim Cmd As New OleDb.OleDbCommand(strSQL, objconn)
        Dim DataReader As OleDb.OleDbDataReader
        Try
            DataReader = Cmd.ExecuteReader
            While DataReader.Read()
                frmAnagrafica.cboElencoNomi.Items.Add(Trim(DataReader("Cognome")) & " " & Trim(DataReader("Nome")) & " - [" & Trim(DataReader("CodiceFiscale")) & "]")
            End While
        Catch
            MsgBox("Si è verificato un errore." & vbNewLine & Err.Description, MsgBoxStyle.Critical, "Errore: " & Err.Number)
        Finally
            Cmd = Nothing
            DataReader.Close()
            DataReader = Nothing
        End Try
End Sub
If you see, this sub fill combobox in form frmAnagrafica; I would fill combobox in form frmSelection.

Is there a simple way to identify form that call subroutine?
Thanks
 
Last edited by a moderator:
Instead of having the method refer to a specific form, have the form pass the ComboBox into the method as an argument. The method can then populate the ComboBox it gets passed without any regard for where it came from.
VB.NET:
Public Sub listNames([COLOR="red"]Dim target As ComboBox[/COLOR])
Dim Cmd As New OleDb.OleDbCommand(strSQL, objconn)
        Dim DataReader As OleDb.OleDbDataReader
        Try
            DataReader = Cmd.ExecuteReader
            While DataReader.Read()
                [COLOR="red"]target[/COLOR].Items.Add(Trim(DataReader("Cognome")) & " " & Trim(DataReader("Nome")) & " - [" & Trim(DataReader("CodiceFiscale")) & "]")
            End While
        Catch
            MsgBox("Si è verificato un errore." & vbNewLine & Err.Description, MsgBoxStyle.Critical, "Errore: " & Err.Number)
        Finally
            Cmd = Nothing
            DataReader.Close()
            DataReader = Nothing
        End Try
End Sub
Even better, make the method a function that simply returns the list of values and then let each caller do with it whatever it wants. The method then doesn't even have to know that any form exists.
VB.NET:
Public Sub GetNameList() As String()
    Dim names As New List(Of String)
    Dim Cmd As New OleDb.OleDbCommand(strSQL, objconn)
        Dim DataReader As OleDb.OleDbDataReader
        Try
            DataReader = Cmd.ExecuteReader
            While DataReader.Read()
                names.Add(Trim(DataReader("Cognome")) & " " & Trim(DataReader("Nome")) & " - [" & Trim(DataReader("CodiceFiscale")) & "]")
            End While
        Catch
            MsgBox("Si è verificato un errore." & vbNewLine & Err.Description, MsgBoxStyle.Critical, "Errore: " & Err.Number)
        Finally
            Cmd = Nothing
            DataReader.Close()
            DataReader = Nothing
        End Try

    Return names.ToArray()
End Sub
Any form can then do this:
VB.NET:
Me.SomeComboBox.Items.AddRange(GetNameList())
That is a much better design.
 
Great.
First solution work correctly, not the second. There are errors but it's difficult for me translate error description from italian to english, so I will use your first advice.
Thanks jmcilhinney
 
Back
Top