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

    global function (Access 2000)

    I have a code for a remote function and i want to open the database in a single module that could
    be used repeatedly.However i am forced to open the database again in the single modules.
    For example,i am opening the database as well as in my function initializedb as well as in DoSomething.
    I want to open the database only in my function initializedb.is it itpossible ?


    Public Function DoIt()
    InitializeDb
    DoSomething
    End Function

    Public Function InitializeDb()
    Dim BEpath As String
    BEpath = "C:bestorebe.mdb"
    Set wsp = DAO.DBEngine.Workspaces(0)
    Set db = wsp.OpenDatabase("C:BEstoreBE.mdb", False, False, ";PWD=" & strPassWord)
    End Function

    Public Function DoSomething()
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim prp As DAO.Property
    Set wsp = DAO.DBEngine.Workspaces(0)
    Set dbs = wsp.OpenDatabase("C:BEstoreBE.mdb", False, False, ";PWD=" & strPassWord)
    Set tdf = dbs.TableDefs("orders")
    tdf.Fields.Refresh
    Set fld = tdf.Fields("OrderDate")
    fld.Properties("DefaultValue") = "=Date()"
    'Enable error handling.
    On Error Resume Next
    Set prp = fld.CreateProperty("Format", dbText, "Short Date")
    fld.Properties.Append prp
    'Check if an error occurred.
    If Err.Number = 3191 Then '3191 = Can't defin field more than once.
    'Log the error, or show messagebox or something... or nothing. Whatever!.
    End If
    'DIsable error handling (if you want to)
    On Error GoTo 0
    dbs.Close
    Set prp = Nothing
    Set fld = Nothing
    Set tdf = Nothing
    Set dbs = Nothing
    Set wsp = Nothing
    End Function

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

    Re: global function (Access 2000)

    In InitializeDb, you set a variable named <big>db</big>
    In DoSomething, you use a variable named <big>dbs</big>. Since this is a different name, you must set it in this function. You must declare ONE public variable and use the name of that variable consistently.

  3. #3
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: global function (Access 2000)

    I find 2 things confusing. Why do you even call InitializeDB when you do the same thing again anyway in DoSomething? And where (and how) did you declare db? (And as Hans pointed out, you are using both db and dbs).
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

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

    Re: global function (Access 2000)

    Thank you to you both !! It works super !
    How can i make public constants from the following : ?

    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim prp As DAO.Property

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

    Re: global function (Access 2000)

    You can't make those into constants. You can make them public variables by placing the declarations at the top of a standard module, below the line(s) beginning with Option, but above all Subs and Functions. A standard module is the type of module you create by clicking New in the Modules section of the database window.

Posting Permissions

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