Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with reading/writing to a text file. PLEASE HELP!

Status
Not open for further replies.

nicklad

Programmer
May 19, 2002
29
AU
Hi
here's my problem:
i need to know how to write some code that will allow me to read/write data from a textbox to a .txt file
The hard bit is that the .txt file will need to be read/written to a directory that will be different for each user. Let me explain it in a bit more detail.

My program is linked to a database. When users log on to my program their userid is saved to a .txt file called "login.txt". The "login.txt" file is also loaded into a label on my program called "lblUserId".
Let's say the directory that users save their data in is App.Path & "\Docs\userid\". If userid "Jane" logs in, i want her data to be saved in "\Docs\Jane\", and so forth.
I experimented with some code, but none worked. A sample of the approach i was taking is below:

Private Sub Form_Load()
' Load login.txt into lblUserID
Open App.Path & "\login.txt" For Input As #1
Line Input #1, Data
lblUserID.Caption = lblUserID.Caption & Data
Close #1
' Make a new directory with the userid name
MkDir (App.Path & "\Docs\" & lblUserID.Caption)
End Sub
------------------------------------------------------------
Private Sub cmdSave_Click()
' Save data from Text1 to text1.txt in the user's "userid" directory
Open App.Path & "\Docs\" & lblProgID.Caption & "\text1.txt" For Output As #1
Print #1, Text1.Text
Close #1
End Sub


This approach works FINE, but only if the user logs in only once! If the user logs in twice, the MkDir line of code brings up "Run-time error '75': Path/File Access Error", basically cos the directory already exists. If i take out the MkDir lines, then i get "Run-time error '76': Path not found".

I'm sure i am probably just missing one crucial point, but i am sooo confused!! Could someone PLEASE PLEASE PLEASE help me with this little, yet EXTREAMLY important problem.
Thank you!
 
Hi,

You need to check if the path allready exists:
Private Sub Form_Load()
' Load login.txt into lblUserID
Open App.Path & "\login.txt" For Input As #1
Line Input #1, Data
lblUserID.Caption = lblUserID.Caption & Data
Close #1
if Dir(App.Path & "\Docs\" & lblUserID.Caption, vbDirectory)="" then
' Make a new directory with the userid name
MkDir (App.Path & "\Docs\" & lblUserID.Caption)
end if
End Sub

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hey Nick..

In your form load why don't you use Error Handler something like this



Private Sub Command1_Click()
On Error GoTo ErrHandler

' Load login.txt into lblUserID
Open App.Path & "\login.txt" For Input As #1
Line Input #1, Data
lblUserID.Caption = lblUserID.Caption & Data
Close #1
' Make a new directory with the userid name
MkDir (App.Path & "\Docs\" & lblUserID.Caption)

ErrHandler:
Select Case Err.Number
Case 75: MsgBox "Directory Already Exists"
'...
'...
End Select
End Sub


Hope this will solve your problem to some extent. I would like to know when exactly are you getting the Error 76.?.
 
thank u both for you excellent and speedy replys!
you guys are LIFESAVERS!!!
cheers
~ nick [elephant2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top