Link to home
Start Free TrialLog in
Avatar of metalfreak
metalfreak

asked on

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.

Thanx.

- MetalfreaK -
Avatar of cindy_k
cindy_k
Flag of United States of America image

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:
C:\Cynthia_File\Database.mdb
C:\Cynthia_File\Application.exe
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
C:\Cynthia_File\Application.exe
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,
Cynthia
Avatar of metalfreak
metalfreak

ASKER

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.
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.




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.UpdateRecord
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".

ASKER CERTIFIED SOLUTION
Avatar of fantasy1001
fantasy1001

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
Data1.Recordset.AddNew
Text1.Enabled = True
Text2.Enabled = True

Save:
Data1.UpdateRecord
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
  data1.Recordset.AddNew

  Exit Sub
AddErr:
  MsgBox Err.Description
End Sub

Save:
Private Sub cmdUpdate_Click()
  On Error GoTo UpdateErr

  data1.Recordset.UpdateBatch adAffectAll
  Exit Sub
UpdateErr:
  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.