Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simplify code (Access 2000)

    I need to carry out commands to change a macro in 9 databases. The paths are called FETa,FESz, etc. i want ti simplify the code and write the paths as argumnets for example
    PrivateFunction ChangeMacro(FETa,FESz..)
    How can i do it and how can i achieve that the code executes each time whenever a path if found given in the argument ?

    Private Function ChangeMacro()
    Dim strPassword As String
    Dim strDatabase As String
    Dim app As New Access.Application
    Dim dbs As DAO.Database
    strDatabase = (FEVa)
    strPassword = "Secret"
    Set dbs = app.DBEngine.OpenDatabase(strDatabase, , , ";PWD=" & strPassword)
    app.OpenCurrentDatabase strDatabase
    app.DoCmd.Rename "Macro2", acMacro, "Macro1"
    End Function

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Simplify code (Access 2000)

    Change the function as follows:

    Private Function ChangeMacro(strDatabase As String, strPassword As String)
    Dim app As New Access.Application
    Dim dbs As DAO.Database
    Set dbs = app.DBEngine.OpenDatabase(strDatabase, , , ";PWD=" & strPassword)
    app.OpenCurrentDatabase strDatabase
    app.DoCmd.Rename "Macro2", acMacro, "Macro1"
    dbs.Close
    Set dbs = Nothing
    app.Quit
    Set app = Nothing
    End Function

    (You removed the lines to close dbs, to quit the instance of Access and to set the object variables to Nothing. That was a very bad idea - without those lines, you run the risk that invisible instances of Access remain in memory.)

    Call the function like this:

    ChangeMacro FETa, "ThisPassword"
    ChangeMacro FESz, "ThatPassword"
    ...

  3. #3
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Simplify code (Access 2000)

    Thank you Hans, thats really great !

Posting Permissions

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