Best way to Use Repeatable code

Posted on 2005-05-06
Medium Priority
Last Modified: 2012-05-05
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
Question by:tobey1
  • 2
LVL 22

Accepted Solution

rspahitz earned 150 total points
ID: 13946790
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.

Author Comment

ID: 13946828
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
LVL 22

Expert Comment

ID: 13946949
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.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question