Results 1 to 4 of 4

Thread: [RESOLVED]have declare variable in module but cant recognize in form

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    33

    Resolved [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:
    1. 'in module
    2. Public Sub init_database()
    3. Dim con As ADODB.Connection
    4. Dim rs As ADODB.Recordset
    5. Dim constring As String
    6. Set con = New ADODB.Connection
    7. Set rs = New ADODB.Recordset
    8. constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\toko.mdb;Mode=ReadWrite;Persist Security Info=False"
    9. con.Open constring
    10. End Sub
    11.  
    12. 'in form
    13. Private Sub txt_departemen_LostFocus()
    14.         Call init_database
    15.         rs.Open "select * from stock", con, adOpenKeyset, adLockReadOnly
    16. 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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. Option Explicit
    2.  
    3. Public con As ADODB.Connection
    4. Public rs As ADODB.Recordset
    5. 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.

  3. #3
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: have declare variable in module but cant recognize in form

    One way to do it using globals would be:

    VB Code:
    1. Option Explicit
    2.  
    3. Public con As ADODB.Connection
    4. Public rs As ADODB.Recordset
    5.  
    6. Public Sub init_database()
    7. dim constring As String
    8. Set con = New ADODB.Connection
    9. Set rs = New ADODB.Recordset
    10. constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\toko.mdb;Mode=ReadWrite;Persist Security Info=False"
    11. con.Open constring
    12. 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

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    33

    Re: [RESOLVED]have declare variable in module but cant recognize in form

    thanks hack and space_monkey
    very appreciate for your quick reply...
    everything is possible

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width