[VS 2010 + VB.Net] How to call a module into another module
-
Friday, April 27, 2012 2:42 PM
Hi,
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 Moduleand 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 SubI want to call EngineConnect procedure from MOD_ENGINE module into popCboBox module so I can ignate the engine!
How can I do it, please?
Regards
All Replies
-
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 ModuleMatt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
- Proposed As Answer by Mike FengMicrosoft Contingent Staff, Moderator Tuesday, May 01, 2012 12:03 PM
- Marked As Answer by Admin-Dev Tuesday, May 01, 2012 5:35 PM
-
Tuesday, May 01, 2012 12:03 PMModerator
Hi Admin-Dev,
Do you have any update?
Best regards,
Mike Feng
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
Hi Mike,
I tried many alternatives solutions, sidewalk solution... but kleinma answer is the final
Thanks

