View Single Post
  #10  
Old June 5th, 2010, 08:38 PM posted to microsoft.public.access.forms
Douglas J. Steele[_3_]
external usenet poster
 
Posts: 3,143
Default this code doesn't work fully

"Bob H" wrote in message
...

Update:
I have used this line to create a text file with given information:

Open "C:\Accounts\Users\" + username + ".txt" For Output As #1
Print #1, Text23
Print #1, Text25


I guess I should have highlighted in my previous reply that you should never
refer to file handles by hard-coded numbers. While it may work fine for you,
if other applications are reading or writing to files at the same time, you
can run into problems. You should ALWAYS use the FreeFile function, even if
you're positive no other applications will ever be running concurrent with
yours.

Dim intFile As Integer

intFile = FreeFile()
Open "C:\Accounts\Users\" & username & ".txt" For Output As #intFile
Print #intFile, Text23
Print #intFile, Text25

You should also use & for concatenating text, not +.

But now on another cmd button I want access to read that said information
in that text file, and grant access.
With this line:

Open "C:\Accounts\Users\" + username + ".txt" For Output As #2
Input #2, openfile
If username.Text = openfile Then
Input #2, datafile


Try using

Open "C:\Accounts\Users\" & username & ".txt" For Output Shared As #2

Access throws up a runtime error 54 'Bad File mode' at Input #2 , openfile
openfile

Also the information in the previously created text file has been deleted.


As you've found, opening a file For Output deletes the previous file if it
exists. use For Append. That will create the file if it doesn't already
exist, and append to the file if it does.

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele
Co-author: "Access 2010 Solutions", published by Wiley
(no e-mails, please!)