Best way to Use Repeatable code

I have this code that I use about 4 times in an application.  What is the best way to store it so that I only have to write it once, and access it may times?

        Dim sqlcom_str As String = "SELECT * " & _
                   "FROM Parent_Sequence " & _
                    "WHERE Sequence = '" & cbox & "' " & _
                    "ORDER BY Sequence"

        ' create a data set and fill it
        Dim myDataAdapter As New SqlDataAdapter(sqlcom_str, SqlCon_str)
        Dim myDataSet As New DataSet
        myDataAdapter.Fill(myDataSet, "Parent_Sequence")

        ' bind the DataSet to the Grid
        ' SeqDataGrid.DataSource = myDataSet
        dg.DataSource = _

        ' Close Connection
        myDataAdapter = Nothing
        myDataSet = Nothing
        sqlcom_str = Nothing
Who is Participating?
rspahitzConnect With a Mentor Commented:
It all depends on the scope of usage.

If it's only used within a single form, put it in as a private subroutine (or function if you want it to return a value.)
Add a parameter list to pass anything that you feel should be customizable through a different procedure.

If it's needed from outside the form, set up the procedure as Friend.
You can also move it to a separate module, but that's best done if the procedure is not really realted to a specific form.

If you want to build a component out of the form, then you can make the procedure Public.
tobey1Author Commented:
I was going to add it to a Module and then call it in from there.

It is being used in 1 form on different TabPages.

So was going to Pass the combobox to the Module and then populate the datagrid
It seems that the procedure is somewhat specific and might be best served in the form where it will be used.

However, if you intend to make it more generic and pass in the SELECT statement, then a separate module is fine, but you can easily do that later when you need that.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.