CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2001
    Posts
    3

    Problem with directory

    I need to connect to a local access97 db with vb6.
    but the program should work on serveal different machines.
    so how do i refer to my database.

    dim db as Database
    set db=OpenDatabase("mydb.mdb")

    doesnt work even if the file mydb.mdb is in the current directory.
    it works with the compiled exe file
    but gives me an error saying that it couldnt find the file.
    Does anybody now a soulution for this?





  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Problem with directory

    The syntax that you use looks for the file in the current directory. And don't assume that the current directory is the same as the one with the application. To make sure that it is, you can use

    chdir App.Path

    before using OpenDatabase. This makes the application path as the current directory and will look for the file here.

    Otherwise, you can use

    Set db = OpenDatabase(App.Path & "\mydb.mdb")

    This also works, and it doesn't change the current directory.


  3. #3
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Problem with directory

    If you are going to use separate db for each user, then you can use App.Path or hard coded path or write and read path to and from Registry or *.INI file, if the same db is going to be used by all users, then Map drive with that database on each user computer, let's say as "Z:" and then refer to it, as it was on the user computer. For example if db is on each computer, then it must be in the same location, like "C:\Program Files\MyProgram\Data\MyDB.mdb" (for hard coded scenario). Then in VB you use

    private strDBName as string
    strDBName = "C:\Program Files\MyProgram\Data\MyDB.mdb"
    Dim db as Database
    set db = OpenDatabase(strDBName)



    You can use App object and its Path property, if database will be located in your program directory:

    strDBName = App.Path & "\Data\MyDB.mdb"
    set db =OpenDatabase(strDBName)



    In case if db is on Mapped drive, use:

    strDBName = "Z:\MyDatabaseFolder\MyDB.mdb"
    set db = OpenDataBase(strDBName)



    In case you read Database location from Registry, you write it to the Registry during the installation using either API functions (better, but more complicated) or VB functions (worse, but easier), then you read it into strDBName and use as above.

    HTH
    Vlad




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

Featured