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

MS Access Create external File

I have the below code.  I am trying to create a file and write to it and its not working.  Any ideas?
Dim fns As String
fns = "c:\List.txt"

Dim fso As FileSystemObject
Dim a As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set a = fso.CreateTextFile(fns, True)
a.WriteLine ("This is a test.")
a.Close

  Open fns For Output As #3
  Write #3, "TEST"
  Close #3

Open in new window

0
CipherIS
Asked:
CipherIS
  • 7
  • 5
  • 2
2 Solutions
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Although I have been known to read minds, this is not one of those times.  Can you define "It's not working in detail" ?

mx
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
And actually, it seems to work for me. It created that text file and when I open it, I see TEXT.

mx
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
btw ... this can be simplified to this:

    Dim fns As String
    fns = "c:\List.txt"
   
    Dim fso As FileSystemObject
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    With fso.CreateTextFile(fns, True)
       .WriteLine ("This is a test.")
       .Close
    End With
  Open fns For Output As #3
  Write #3, "TEST this is working"
  Close #3
 
... with the added advantage that you get Intellisense after typing the DOTS ...

btw ... when I say working, it writes the 'This is a test" first ... then TEST this is working ...

mx
Capture1.gif
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
CipherISAuthor Commented:
When I say its not working I mean that C:\List.txt is not being created and as a result no text is in the file.

That is the problem I'm experiencing.  I am using MS Access 2007 on Win7 computer.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Well ... it is certainly working for me.  I have noticed however that if ... some error occurs (like the file is already open) ... the error seems to be 'silent'. So, apparently some error is occurring on your system.  Not sure why it's not being rendered (the error that is).  Maybe some permissions / UAC issue with Win 7 ... or, is your db in a Trusted Location ?

mx
0
 
CipherISAuthor Commented:
db is on my client.

I put set warnings = true but no errors are being generated and error handling is not catching anytghing.,
0
 
CipherISAuthor Commented:
ok - it is creating the file but it is storing it at

C:\Users\Admin\AppData\Local\VirtualStore

Any idea how to get it to create on the "C:\" Drive?
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Actually, it appears there is no error if the file is open ... it actually replaces the text (I changed the text). So far, I cannot really force an error.

mx
0
 
CipherISAuthor Commented:
So it seems if I have a dir "C:\Test\" it will create it there but not on C:\ drive

any ideas?
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
wow.  let me try this on my Win 7 machine ...

mx
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Well, on my Win 7 System (A2010), it puts the file in C:\ .... so ...

mx
0
 
peter57rCommented:
The default position for Win7 (as it was for Vista) is that you can't save into c:\.  End-users would normally be running machines with this restriction in place.


Developers tend to disable any security settings, or run with sufficient admin rights to overcome things that stop them doing what they want so we don't see the problem.

To broaden the issue you will find that in WIn 7 there are lots of folders that could be accessed directly in previous versions of windows which are now inaccessible and map to virtual locations.

0
 
CipherISAuthor Commented:
ok - so I will save to another location can you tell me how to modify the code so it opens the file once its done writing to it?
0
 
peter57rCommented:
Assuming fns contains the full path then you can just do..

Application.Followhyperlink fns

This will open the file in whatever is the associated applicaton for .txt files (usually Notepad)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now