[RESOLVED]have declare variable in module but cant recognize in form
i have declare variabel for connection, recordset and connection string in module and make procedure for initialize database but the variabels aren't recognize in form.
so now if i have 5 forms then i must declare the variabels 5 times.
here are my code :
VB Code:
'in module
Public Sub init_database()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim constring As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\toko.mdb;Mode=ReadWrite;Persist Security Info=False"
con.Open constring
End Sub
'in form
Private Sub txt_departemen_LostFocus()
Call init_database
rs.Open "select * from stock", con, adOpenKeyset, adLockReadOnly
End Sub
the error message appear is "variabel not defined" for object con.
thanks in advance for your help :)
Re: have declare variable in module but cant recognize in form
Welcome to the forums. :)
Take your variables out of your sub routine, and put them in the declarations selection of the module and make them public
VB Code:
Option Explicit
Public con As ADODB.Connection
Public rs As ADODB.Recordset
Public constring As String
When declaring variables, follow these guidelines:
Public: For use through out the project. Declare them in declarations section of a module.
Private: For use through a specific form. Declare them in the declarations section of the form.
Dim: For use only within a specifc sub/function/control event.
Re: have declare variable in module but cant recognize in form
One way to do it using globals would be:
VB Code:
Option Explicit
Public con As ADODB.Connection
Public rs As ADODB.Recordset
Public Sub init_database()
dim constring As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\toko.mdb;Mode=ReadWrite;Persist Security Info=False"
con.Open constring
End Sub
HTH
Re: [RESOLVED]have declare variable in module but cant recognize in form
thanks hack and space_monkey :p
very appreciate for your quick reply...