Results 1 to 13 of 13

Thread: Best code for form load event..

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Best code for form load event..

    Hi,

    Kindly let me know the best way to start coding for ADO:
    How the form load event should be now?

    ------------------------------------------------
    (GENERAL) ---- (DECLARATIONS)
    ------------------------------------------------
    Option Explicit
    Dim cn As ADODB.Connection
    Dim rs As adbodb.Recordset
    Dim scn As String
    Dim sql As String

    --------------------------------------------------

    Private Sub Form_Load()


    End Sub


    ---------------------------------------------------
    Regards,

    sweetie_2005

  2. #2
    Addicted Member
    Join Date
    May 2004
    Location
    Nagpur, India
    Posts
    228

    Re: Best code for form load event..

    What exactly you need?

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

    Re: Best code for form load event..

    Dim should be used to declare variables at the event level.

    Private should be used to declare variables at the Form level.

    Public should be used to declare variables at the Module level.

    So, the variable declarations in your code example should use Private not Dim and I would add the following to those variables.
    VB Code:
    1. Private cn As ADODB.Connection
    2. Private rs As adbodb.Recordset
    3. Private strConnString As String 'add this
    4. Private scn As String
    5. Private sql As String

    The best code for the Form Load event will vary for each project and will be/should be dictated by the needs of that project. A general rule of thumb would be to use the Form Load to connect to your database.
    VB Code:
    1. Private Sub ConnectDb()
    2. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    3.         "Data Source=c:\yourMdb.mdb;" & _
    4.         "Persist Security Info=False"
    5.  
    6. Set cn = New ADODB.Connection
    7. cn.ConnectionString = strConnString
    8. cn.Open strConnString
    9. End Sub
    10.  
    11. Private Sub Form_Load()
    12. ConnectDb
    13. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: Best code for form load event..

    Quote Originally Posted by mayurvb
    What exactly you need?
    Mayur,

    Could you please write code for the form load event by looking at the general declarations.

    Sweetie_2005

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: Best code for form load event..

    Quote Originally Posted by Hack
    Dim should be used to declare variables at the event level.

    Private should be used to declare variables at the Form level.

    Public should be used to declare variables at the Module level.

    End Sub[/Highlight]
    Wow, Hack, you have written a lot of code for me. Thanks.
    Suppose I have a database called "database.mdb"
    Two tables called "table1" & table2" and I need both tables to be accesseble for me then how exactly should be the code?

    Can you explain, Hack?

    Sweetie_2005

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

    Re: Best code for form load event..

    Quote Originally Posted by sweetie_2005
    Wow, Hack, you have written a lot of code for me. Thanks.
    Suppose I have a database called "database.mdb"
    Two tables called "table1" & table2" and I need both tables to be accesseble for me then how exactly should be the code?

    Can you explain, Hack?

    Sweetie_2005
    Exactly the way I've already posted. Once you open the database, it doesn't matter how many tables are there. They are all available.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: Best code for form load event..

    Quote Originally Posted by Hack
    Exactly the way I've already posted. Once you open the database, it doesn't matter how many tables are there. They are all available.
    Wow! like that. Can I access the both tables at the same time.

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

    Re: Best code for form load event..

    Quote Originally Posted by sweetie_2005
    Wow! like that. Can I access the both tables at the same time.
    Sure. I have databases with 50 and 60 tables, sometimes more.

    All you need to do is open the database, and they are there for you to do whatever you need to do.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: Best code for form load event..

    Quote Originally Posted by Hack
    Sure. I have databases with 50 and 60 tables, sometimes more.

    All you need to do is open the database, and they are there for you to do whatever you need to do.
    Oh! I see.

    Thanks

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Best code for form load event..

    yuck ADO!! lol... as I have said before.. Access was designed to use DAO and I think it works better with it not to mention its MUCH easier to use!

    VB Code:
    1. 'General Declaraitons
    2. Public ws As Workspace
    3. Public db As Database
    4.  
    5. 'Form Load
    6. Set ws = DBEngine.Workspaces(0)
    7. Set db = ws.OpenDatabase("C:\database.mdb")
    8.  
    9. 'run a query...
    10. Dim rs as recordset
    11. Set rs = db.OpenRecordset("SELECT * FROM TABLE1 WHERE ID = 1")
    12.  
    13. 'edit?
    14. rs.Edit
    15. rs!CustomerName = "New Name"
    16. rs.update
    17.  
    18. 'or u can set the rs right to a table
    19.  
    20. Set rs = db.openrecordset("Table1")
    21.  
    22. 'add?
    23. rs.AddNew
    24. rs!CustomerName = "New Customer"
    25. rs!SomeOtherField = "Othe Data"
    26. rs.update
    27.  
    28. 'other stuff:
    29. 'if u have a query in access u want to run?
    30. access.DoCmd.OpenQuery "QryName"
    31. 'plus lots of other goodies easily available...
    32. db.Execute "SQL"
    33. access.SysCmd [Option]
    34. 'how about Dlookup!??
    35. 'quick way to get the value of one field without a whole SQL statement
    36. str = dlookup("FieldName","TableName","ID = 3")
    ok.. enough pushing from me but its so easy!! lol.. but of course this is my opinion.. and most here will say to use ADO since DAO is a dying code.. but hey.. so is VB6 right ?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Best code for form load event..

    Just to let you know, it's best to use the Form_Activate() event rather than Form_Load() because the Form_Load event fires before the form and all of its objects are even loaded! And as a result, you may sometimes find odd results depending on what you do. Form_Activate fires after everything has loaded, and everything should work fine with that.

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Best code for form load event..

    I thought that form_activate can fire again..?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Best code for form load event..

    Sweet, as an addendum to hack's code, place the connection object in a module (*.bas) so its available for use in the entire project and not just in one form.

    yup, the activate event can fire again... I guess jacob was referring to problems with controls placed on the form.. eg you can't use their setfocus method during form load.

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