Friday, April 27, 2012 2:42 PM
I have a VB.Net interface that communicates with an Access 2k3 Database using this module:
Module MOD_ENGINE 'Declare Database connections controls Dim con As New OleDb.OleDbConnection Dim dbProvider, dbSource As String Dim AccessCommand As OleDb.OleDbCommand 'Connect to Access Database and activate the Engine Public Sub EngineConnect() dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;" dbSource = "..................................;" con.ConnectionString = dbProvider & dbSource End Sub End Module
and another module that has queries procedures:
'Populate ComboBox Public Sub popCboBox() sql = "........." da = New OleDb.OleDbDataAdapter(sql, con) da.Fill(ds, "Tab") form.CboBox.DataSource = ds.Tables(0) form.CboBox.DisplayMember = "Data1" form.CboBox.ValueMember = "Data1" End Sub
I want to call EngineConnect procedure from MOD_ENGINE module into popCboBox module so I can ignate the engine!
How can I do it, please?
Friday, April 27, 2012 3:18 PMModerator
While generally global module objects are not a good idea, the reason you can't access your 'con' connection object from MOD_ENGINE in your other module is because you used DIM to declare them. DIM is the equivalent of declaring them private. If you were to change Dim to Friend or Public, you would be able to call your con variable from the module. If you really want to do it this way, how about making the connection object more of a singleton?
Something along these lines:
Module MOD_ENGINE 'Declare Database connections controls Dim con As OleDb.OleDbConnection = Nothing Public Function GetConnection() As OleDb.OleDbConnection Try If con Is Nothing Then con = New OleDb.OleDbConnection("CONNECTION STRING HERE") End If If con.State <> ConnectionState.Open Then con.Open() End If Return con Catch ex As Exception 'HANDLE ERROR Return Nothing End Try End Function End Module Module OtherModule Public Sub DoDatabaseStuff() Dim myCon As OleDb.OleDbConnection = MOD_ENGINE.GetConnection If myCon IsNot Nothing Then 'WE HAVE A VALID OPEN CONNECTION TO DB Else 'ERROR OCCURED IN GetConnection ROUTINE End If End Sub End Module
Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
Tuesday, May 01, 2012 12:03 PMModerator
Do you have any update?
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Tuesday, May 01, 2012 5:37 PM
I tried many alternatives solutions, sidewalk solution... but kleinma answer is the final