This really is my function:

Public Function DBConnection(ByVal path As String)
    ' This function makes the database connection and returns the object
    ' to reference it.
    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + ";")
    Return cn
End Function

As you can tell, I wish to initialize a database connection and give it back in order to utilize it during my forms. This function is within a module and my variables are the following:

Public cn As OleDbConnection
Public cmd As OleDbCommand
Public dr As OleDbDataReader

But I am unsure the way i may use this during my forms, will i just call the function DBConnection after which proceed with my SQL claims? Or must i make a move else? Help could be greatly appreciated, cheers.

Also, I want some opinions. My application uses MS Access database. Is it more beneficial to initialize the bond on Form_Load after which close the bond once the user shuts this program, or open and shut the connections because the queries are run? I am likely to apply certain database queries on multiple forms hence the main reason I had been putting it right into a module, but I am not 100% how I ought to proceed with this particular.


From: [cde]

How to bind Microsoft Access forms to ADO recordsets

A few things. That function will open an association each time it's known as. You best make certain you're closing the database too, or which will start eating up memory.

You may take a look at another options (for example using NHibernate or any other ORM.)

However, should you stick with this model, you can then make use of the cn that's came back to gain access to your database.