Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1114
  • Last Modified:

How is App.path used??

Ok guys here it goes.... I made a program using VB6.0, but when installing it on a friend's PC.. it didn't work he got a path error.
I was told to use App.path & "\databasename.mdb" .... but i really don't get it.
The person wasn't to specific 'bout how to use app.path.

Note: I use the data control on forms to access the database, the database is name is "directorio.mdb" and it's located in "C:\Mis documentos\Directorio\".
So i would aprecciate an example code on how to use app.path.


- MetalfreaK -
1 Solution
Here's the deal with App.Path.  It returns the Directory where the application is located. If your database is in the Same Directory as your application then App.Path will be a big help.
If the database can be anywhere on the machine, then you will need a way for your users to choose a directory path if the application can't find the database.

Ex 1:
Here App.Path should be C:\Cynthia_File\  which could be used to find the database too, since they are in the same directory.

Ex 2:
C:\My documents\Database.mdb
Here App.Path would be useless since the Database is in My Documents and the path returned would be C:\Cynthia_File.

I hope this helps,
metalfreakAuthor Commented:
Good answer.. but i kinda knew that already Cynthia.
I wanted to get on explanation on how to use it in my program. How to use it in code.
For example u say: "Here App.path should be C:\Cynthia_File\" , but how do i do that?
How do i make app.path take that value.
Explain it a little, and please some code.
you aint getting the pt metalfreak....

if your database is going to be in the same folder as the exe file u run, then use:

strDatabasepath = app.path & "\" & databasename.mdb

or something like that....
now, strDatabasePath can be used as the database filename inclusive of the path.

But, if u are placing ur database in some path which is not at all related to the exe file's location, then you could
include mebbe an common dialog control to let the user select the database location.

However, if the database path happens to be related, as in, lets say ur exe is in a folder called c:\myprog\exe
and ur database will always be in c:\myprog\db, then u cud do a :

strDatabasepath = app.path & "\..\db" & databasename.mdb

PS. App.Path is NOT to be set to any value. It holds the path where the exe is currently running from. Thats all.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

metalfreakAuthor Commented:
Ok, i get what App.path does..I think

So I use this: strdatabasepath = App.Path & "\directorio.mdb"

Both the exe and the database r in the same folder:
"C:\Mis documentos\Directorio\directorio.exe"
"C:\Mis documentos\Directorio\directorio.mdb"

but what do I do with strdatabasepath, i mean should I declare strdatabasepath? and how do i use it?
App.Path returns a string, so you don't really have to declare it but it is advisable:       Dim strdatabasepath as string

You can use it everywhere:      

If you want to open this file then use:

Dim BlaBla As String
BlaBla = App.Path & "\databasename.mdb"

Open BlaBla for input as #1
Close #1

If you have a DirListBox then use:    >>  Dir1 = App.Path   <<    This will change it's path to App.Path
Chdir App.Path will change the current folder to this folder.

metalfreakAuthor Commented:
Ok but i use data controls on my form to connect with the database if i use app.path.. should i remove the data controls??. Here's the code from 1 command of "frmadd" (were you add any new data to the database). This is the actual code from my program. So please modify it to see how should app.path de used.

Data1.Recordset.Bookmark = Data1.Recordset.LastModified
Text1.Enabled = False
Text2.Enabled = False
submenuedit.Enabled = True
submenudelete.Enabled = True
submenusave.Enabled = False
submenucancel.Enabled = False
Data1.Enabled = True
Text7.Text = "Estado actual: Registro almacenado correctamente".

Just use:

'Put this before all your other data1 method
Data1.DatabaseName = App.Path & "directorio.mdb"

~ fantasy ~
metalfreakAuthor Commented:
Doesn't work... i get an data type conversion error when saving the record.

Data1 Properties:
* connect: Access
* databasename: (it gets it from Data1.DatabaseName = App.Path & "\directorio.mdb" in code)
* defaulttype: 2-Use Jet
* recordsetype: 1-Dynaset
* exclusive: false

Here's the code.. pls modify it

Add New:
Text1.Enabled = True
Text2.Enabled = True

Data1.Recordset.Bookmark = Data1.Recordset.LastModified
Data1.Enabled = True
The problem is related to your bounded data control. Try this

Add New:
Private Sub cmdAdd_Click()
  On Error GoTo AddErr

  Exit Sub
  MsgBox Err.Description
End Sub

Private Sub cmdUpdate_Click()
  On Error GoTo UpdateErr

  data1.Recordset.UpdateBatch adAffectAll
  Exit Sub
  MsgBox Err.Description
End Sub
Note:  App.Path does NOT return a trailing \ UNLESS your program is running from the root of a drive.   So, if your app is running from c:\, App.Path will return "C:\", BUT if you're running your app from C:\Blah\ then App.Path will return "C:\Blah".  Note the lack of a trailing \.  Keep that in mind when coding.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now