Best way to Use Repeatable code

Posted on 2005-05-06
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
    LVL 22

    Accepted Solution

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

    Author Comment

    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now