Problem with Default Location for Saving Files

  • Thread starter Yelena Varshal via AccessMonster.com
  • Start date
Y

Yelena Varshal via AccessMonster.com

Hello,

I have some code in a Module that logs things to a file into the subfolder of
the current folder where the mdb file is. This code worked fine last week and
this Monday creating files in this subfolder but would not work starting
Tuesday telling me that path could not be found. When I removed the
subfolder name from the file name this code worked today and when I searched
where the file was created it was created in My Documents instead of the
folder where the MDB file is. This code is very generic:

' This code worked last week creating a file in the subfolder of the
folder with mdb file
' it can not find a subfolder now, the subfolder is there in the folder.
Dim filenumber As Integer
filenumber = FreeFile
Open "MySubFolder\MyFile.txt" For Output As #filenumber

' This code does work but it creates a file in My Documents instead of the
folder where the mdb file is.
Dim filenumber As Integer
filenumber = FreeFile
Open "MyFile.txt" For Output As #filenumber

Any Ideas?
Thanks
 
J

John Spencer

Be more specific in specifying the path to the folder. You are assuming
that it is starting at some location.

Dim strFilePath as String.
Dim filenumber As Integer
filenumber = FreeFile
strFilePath= CurrentProject.Path & "\MySubFolder\MyFile.txt"
Open strFilePath For Output As #filenumber

If you are using Access 97 or earlier then the CurrentProject.Path will
fail - it was added in A2K version. You can still get the path with some
VBA in Access97. Roughly, something like:
Left(CurrentDb().Name,Len(CurrentDb().Name) - Len(Dir(CurrentDb.Name)) )
 
Y

Yelena Varshal via AccessMonster.com

John,

THANKS!!!!
This is exactly what I was looking for and it works.

I still don't know why the location changed to My Documents from the database
folder, nothing really strange because in Access the default database
location is in MyDocuments, but I did not like the relative location too. I
was looking for the way to express the database file path and went as far as
trying to play with Database Properties object, but your solutions is so much
better.

Yelena

John said:
Be more specific in specifying the path to the folder. You are assuming
that it is starting at some location.

Dim strFilePath as String.
Dim filenumber As Integer
filenumber = FreeFile
strFilePath= CurrentProject.Path & "\MySubFolder\MyFile.txt"
Open strFilePath For Output As #filenumber

If you are using Access 97 or earlier then the CurrentProject.Path will
fail - it was added in A2K version. You can still get the path with some
VBA in Access97. Roughly, something like:
Left(CurrentDb().Name,Len(CurrentDb().Name) - Len(Dir(CurrentDb.Name)) )
[quoted text clipped - 24 lines]
Any Ideas?
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top