If someone could tell me if there is a function or method, or some way of finding the directory that the VB project loads to when it is installed on other computers that would be great. What I want it for is so that I can save and restore a database dynamically.

Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0) Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.

Any help is appreciated, some technical info is: VB 2005 Express, Access 2003 Database, and the code I am using right now for restoreDB()

Public Sub restoreDB()

        Dim backupName As String
        Dim portfolioDBName As String = portfolioDBPath + "portfolioDB.mdb"

        Dim fdlg As OpenFileDialog = New OpenFileDialog()

        fdlg.InitialDirectory = portfolioDBPath + "\backup"

        If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            backupName = fdlg.FileName

            If MessageBox.Show("Restoring the database will erase any changes you have made since your last backup. Are you sure you want to do this?", _
            "Confirm Delete", _
            MessageBoxButtons.OKCancel, _
            MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
                'Restore the database from a backup copy.
                FileCopy(backupName, portfolioDBName)

                MsgBox("Database Restoration Successful")
            End If
        End If

    End Sub

Hi Mr Wobbles (good name!)

For VB6 use: App.Path to return the application dir (eg: Open App.Path & "\MyFile.txt" For Input as #1)

For VB2005 use My.Application.Info.DirectoryPath

HTH,

Chris.

That worked well, thanks! I don't know why I didn't see that before. (I'm not being sarcastic if it sounds that way... seriously)

That works when I debug. However when I publish it and try to work on it as an application then i get an error message - it can't find the file. I looked and figured out it was in a different folder than the application is apparently stored in. Don't know why. So if there is a way to get the path of a the database file that would be awesome.

Ah.

This appears to be an installation issue.

I had 2005 Express for a while, got fed up with the installer, bought the full version.

Where is the DB being installed to? Is it as a subdirectory of the application?

Chris

The DB is installed to an alternate folder than the application directory for example:

Directory
Folder 1 = What app.path returns
Folder 2 = Where the database is at

All of the folders repeat some sort of information, but none of them have all of the information, so this one will have the database and maybe the icon or something, and another will have the icon and the forms in it, there is one folder for each of the interops that I used (word, excel and access) and a manifest folder. If none of this helps at all I can get you the real names of the folders tomorrow when I am at work again. Thanks for your help, I appreciate it.

All of the folders repeat some sort of information, but none of them have all of the information, so this one will have the database and maybe the icon or something, and another will have the icon and the forms in it, there is one folder for each of the interops that I used (word, excel and

But what I recommend for dynamically finding the appropriate directory is to either store the information about the data's path in an older INI file (See the Platform SDK for Read/WritePrivateProfile) or to use the registry.

But I would declare a public variable in one of your modules to use as a reference in the program.

Public mpProgPath as String

Retrieve the value on loading:

mpProgPath = GetSetting("My Program", "StartUpValues", "AppPath", cstr(App.path))

And then, you can use that as a basis for your other directories where you have information stored:

strDB_PATH =  mpProgPath & "\db"
strImage_Path = mpProgPath & "\Images"

If someone could tell me if there is a function or method, or some way of finding the directory that the VB project loads to when it is installed on other computers that would be great. What I want it for is so that I can save and restore a database dynamically.

Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0) Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.

Any help is appreciated, some technical info is: VB 2005 Express, Access 2003 Database, and the code I am using right now for restoreDB()

Public Sub restoreDB()

        Dim backupName As String
        Dim portfolioDBName As String = portfolioDBPath + "portfolioDB.mdb"

        Dim fdlg As OpenFileDialog = New OpenFileDialog()

        fdlg.InitialDirectory = portfolioDBPath + "\backup"

        If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            backupName = fdlg.FileName

            If MessageBox.Show("Restoring the database will erase any changes you have made since your last backup. Are you sure you want to do this?", _
            "Confirm Delete", _
            MessageBoxButtons.OKCancel, _
            MessageBoxIcon.Question, _
            MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
                'Restore the database from a backup copy.
                FileCopy(backupName, portfolioDBName)

                MsgBox("Database Restoration Successful")
            End If
        End If

    End Sub

Hi Wobbles,
Try to do this for to find the directory .If you find the directory dynamically then pass the address at the run time .

Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "d:\gb nair"
strFile = "\Summer.txt"
strText = "Book Another Holiday"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
   Set objFolder = objFSO.CreateFolder(strDirectory)
   WScript.echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
   Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
   WScript.echo "Just created " & strDirectory & strFile
End If

Set objFile = Nothing
Set objFolder = Nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)

' Writes strText every time you run this VBScript
objTextFile.WriteLine (strText)
objTextFile.Close

' Bonus or cosmetic section to launch explorer to check file
If Err.Number = vbEmpty Then
   Set objShell = CreateObject("WScript.Shell")
   objShell.Run ("Explorer" & " " & strDirectory & "\")
Else: WScript.echo "VBScript Error: " & Err.Number
End If

WScript.Quit

hkdani - the only potential problem I can see with that is my database is in a different folder than what app.path return, the folders that each are in are in the same location, also I can't really go up a directory and specify the folder name because every time I install them then the folder names change - don't know why. I am going to try this anyway, in case I am reading it incorrectly.

gireeshb22 - Not exactly sure about that - maybe I am not understanding but from what I can tell if the file doesn't exist in the specified directory it is created - only problem with that is it is a database and it has to have tables, etc and if I do create it there then I will also have to figure out how to attach it to the program from wherever i create it at. If I am wrong please let me know because some of the functions I am only guessing at what they do.

Ok, from your post it looks like the Application Folder is in the same folder as the DB Folder.

You can use string manipulation to get the parent folder of your App.Path (use a Do.. ..While Loop and set the string length = to string length until last character = "\" - I think there's a property in FSO that will do this as well, could be GetParentFolder or GetParentDIR or similar)

Once you have the Parent Folder you can use a For.. ..Next Loop to look for your DB:

Private Sub cmdFiles_Click()
MsgBox GetDBLocation
End Sub

Private Function GetDBLocation() As String
Dim strDBLocation As String
Dim fso As New FileSystemObject
Dim fldrs As Folder
Dim fldr As Folder
Dim fle As File
Set fldrs = fso.GetFolder("C:\Program Files")
For Each fldr In fldrs.SubFolders
For Each fle In fldr.Files
If fle.ShortName = "YOUR DB NAME" Then
strDBLocation = fle.Name
Goto ExitHere
End If
Next fle
Next fldr
ExitHere:
Set fldrs = Nothing
Set fso = Nothing
If Len(strDBLocation) > 0 Then
GetDBLocation = strDBLocation
Else
GetDBLocation = "Oops! Can't find the DB"
End If
End Function

Oops! That should be string length = (string length -1)

I was right about the FSO Property though - it's GetParentFolderName - so no need to mess with strings at all!

Chris.

OK, so I've just spotted another mistake. Where I have Set fldrs = fso.GetFolder("C:\Program Files") it should be Set fldrs = fso.GetFolder(YOUR PARENT FOLDER PATH HERE)

It's been a long day..!

hkdani - the only potential problem I can see with that is my database is in a different folder than what app.path return, the folders that each are in are in the same location, also I can't really go up a directory and specify the folder name because every time I install them then the folder names change - don't know why.

You are installing them. You are installing what? You are installing the database files after installing the program? You are installing pictures? Need more information. Are you talking about installing the program?

The installer should install the Program in a default location: usually the <Program Files > folder. You need to follow some good practices on your locations: (1) Data that will be used by All Users, should go in the All Users directory, and (2) Data that will be used for the personal user should go in that User's Directory.

Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0)

That's not a good practice, hard coding locations. You should use the GetEnvironmentVariables with the Environment Class in Visual Basic to find where the Program Files folder is, AllUsers, CurrentUser, etc.

Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.

You simply need to save two locations: (1) Your Backup Location, and (2) Your Active Location--or the location you use while running the program. As I wrote before, you should save these values in the registry or in an INI File. O'Reilly prefers saving that type of info in the older type INI file. But you'll need to use the GetPrivateProfileInt from the SDK to use this function. It's easier to use the registry, but O'Reilly says it's probably better to use the INI file

But once you have those two locations saved, you have to program the code based on those values to restore your database. But the first time the program runs, you should have code running in the initialization phase that will save the Active Location, and then you need some kind of menu that spits out a form where you can save the restore location and probably use the same form for restoring your data.

hkdani - the only potential problem I can see with that is my database is in a different folder than what app.path return, the folders that each are in are in the same location, also I can't really go up a directory and specify the folder name because every time I install them then the folder names change - don't know why.

You are installing them. You are installing what? You are installing the database files after installing the program? You are installing pictures? Need more information. Are you talking about installing the program?

The installer should install the Program in a default location: usually the <Program Files > folder. You need to follow some good practices on your locations: (1) Data that will be used by All Users, should go in the All Users directory, and (2) Data that will be used for the personal user should go in that User's Directory.

Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0)

That's not a good practice, hard coding locations. You should use the GetEnvironmentVariables with the Environment Class in Visual Basic to find where the Program Files folder is, AllUsers, CurrentUser, etc.

Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.

You simply need to save two locations: (1) Your Backup Location, and (2) Your Active Location--or the location you use while running the program. As I wrote before, you should save these values in the registry or in an INI File. O'Reilly prefers saving that type of info in the older type INI file. But you'll need to use the GetPrivateProfileInt from the SDK to use this function. It's easier to use the registry, but O'Reilly says it's probably better to use the INI file

But once you have those two locations saved, you have to program the code based on those values to restore your database. But the first time the program runs, you should have code running in the initialization phase that will save the Active Location, and then you need some kind of menu that spits out a form where you can save the restore location and probably use the same form for restoring your data.

This article has been dead for over six months. Start a new discussion instead.