|
-
Jul 20th, 2005, 07:28 AM
#1
Thread Starter
Member
[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
Last edited by little_mouse; Jul 20th, 2005 at 09:12 AM.
Reason: resolve
-
Jul 20th, 2005, 07:41 AM
#2
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.
-
Jul 20th, 2005, 07:41 AM
#3
Fanatic Member
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
Using VB6 or VB.net 2008 with .net 3.5
"Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad
-
Jul 21st, 2005, 10:14 AM
#4
Thread Starter
Member
Re: [RESOLVED]have declare variable in module but cant recognize in form
thanks hack and space_monkey
very appreciate for your quick reply...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|