views:

102

answers:

1

I have a form set up where I want to run the function PopulateGrid on it's Form_Load event. I have initialized the DB connection as follows:

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    LoadConfigFile()
    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & config("DatabasePath") & ";")
    cn.Open()

End Sub

Now, I want to run the PopulateGrid function directly after I connect to the database as shown above. I'm confused as to how I'd use a database query in a module when the database is initialized in my main form. Would simple setting the variable cn to public work? Or do I have to do something more complex?

Thanks.

A: 

You should modify your module to accept the OleDbConnection as a parameter and then just pass the cn variable to the query method. Setting cn to public would not be a good design because it would be introducing a dependency on the form/ui in the module that is unnecessary.

Ryan