+ Reply to Thread
Results 1 to 12 of 12

Open mdb file from another computer

  1. #1
    Registered User
    Join Date
    08-25-2004
    Posts
    15

    Open mdb file from another computer

    I want to open and work (read only) with an Access database from another computer. How can I do this?

  2. #2
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    You have a number of options depending on how you want to handle it.

    1. you can open the database in access and then save a table or query as a spreadsheet

    2. In Excel use Data ... Get External Data .. New Database Query ... Microft Access database. If you use the record new macro feature this will give you enough to understand most of the VBA.

    3. You can create a reference (in VBA tools ... references) to either a DAO or ActiveX Data Object library which will then give you access to the programming objects to open and select data. A useful method in this is the "copyfromrecordset" method. You may get more help on this from an Access site than from this Excel site

    Hope this points you in the right direction. When you know which you want to do then update this and I will find some code.

    regards

  3. #3
    Registered User
    Join Date
    08-25-2004
    Posts
    15
    I need to use the third option. Can you give me some code example?

  4. #4
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    This code opens the database gets the data and pastes it onto the spreadsheet. It is referencing the Microsoft DAO 3.6 Object Library although others will work and you may prefer to use ActiveX Data Objects.

    Sub a()
    Dim db As DAO.Database
    Dim rst As Recordset

    Dim strSQL As String

    Dim rng As Range

    Set db = DAO.OpenDatabase("C:\_pms\dbforum.mdb")

    strSQL = "select CLNAM1,CLNAM2 from _client where CLSORC<>"""";"
    Set rst = db.OpenRecordset(strSQL)

    Set rng = ThisWorkbook.Worksheets(1).Range("A1")
    rng.CopyFromRecordset rst

    Set rst = Nothing
    db.Close
    Set db = Nothing
    MsgBox "done : " & strSQL
    End Sub

  5. #5
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    Sorry forgot to add that you can use the query builder in access to model the SQL or set up queries in the access database to simplify the code

    Have fun

  6. #6
    Registered User
    Join Date
    08-25-2004
    Posts
    15
    And if BD has a password?

  7. #7
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    Set db = DAO.OpenDatabase(Name:="C:\_pms\dbforum.mdb", _
    Connect:="MS Access;PWD=mypassword")

  8. #8
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    Actually I think you may need to list the other "optional" parameters as in :

    Set db = DAO.OpenDatabase("C:\_pms\dbforum.mdb", false,true,"MS Access;PWD=mypassword")


    Sorry havn't got time to test it. I seem to remember that the optional parameters become mandatory when using connect.

    Well that's what keeps life interesting.

  9. #9
    Registered User
    Join Date
    08-25-2004
    Posts
    15
    Before passwording the database I need to declare a function connect() that opens the database and than db will be recognized outside the function. I want to use the function in every commandbutton code or just once, when opening the workbook that contains those buttons. But I don't know exactly how to do that. Can you help me?

  10. #10
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    If the buttons are on a userform then declaring the db outside the main structure should keep the object persistant.

    If the buttons are on a worksheet then when the code execution stops persistance will be lost.

    There is a way to get apparant persistance and that is to declare a global class module using "as new". This means that any reference to the class module will create an instance if one does not already exist.

    Then you open the database in the intiialise event and return a reference to the database object. This also gives a rather nice feature that you can close the database in the terminate event thus ensuring references to the database are tidied up in the event of unexpected code ending.

    I will post some code later.

  11. #11
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    AS with all these sort of things Error handling is necessary and should be used.


    PART1====================== goes in a standard module
    Option Explicit

    Public glb As New myGlobalsClass

    ==========================

    PART2 ============ this can go anywhere ==========
    Sub a()
    Dim DB As DAO.Database
    Dim rst As Recordset

    Dim strSQL As String

    Dim rng As Range

    'Set db = DAO.OpenDatabase("C:\_pms\dbforum.mdb")
    Set DB = glb.DB
    strSQL = "select CLNAM1,CLNAM2 from _client where CLSORC<>"""""
    Set rst = DB.OpenRecordset(strSQL)

    Set rng = ThisWorkbook.Worksheets(1).Range("A1")
    rng.CopyFromRecordset rst

    Set rst = Nothing
    ''' DB.Close
    ''' Set DB = Nothing

    MsgBox "done : " & strSQL
    End Sub


    PART3 ============= This as a Class module called myGlobalClass
    Option Explicit

    Dim DB1 As DAO.Database

    Private Sub Class_Initialize()
    Set DB1 = DAO.OpenDatabase("C:\_pms\dbforum.mdb")
    End Sub



    Public Function DB() As Database
    Set DB = DB1
    End Function

    Private Sub Class_Terminate()
    DB1.Close
    Set DB1 = Nothing
    End Sub

  12. #12
    Registered User
    Join Date
    08-25-2004
    Posts
    15
    It works. Thanks a lot.
    Now I will test to password the data base.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1