[RESOLVED] Help with opening a text file
Here is my code:
Code:
Private Sub Form_Load()
Dim Channel As Integer
Channel = FreeFile(0)
Open App.Path & "\HELPTEXT.txt" For Input As #Channel
Text1.Text = Input$(LOF(Channel), 1)
Close #Channel
End Sub
When I open the form it comes up with an error saying "Bad file mode". As far as I'm aware, you open for Input if you want to do what I'm doing. Are there any other factors that might be affecting this, or have I just got the wrong mode?
Thanks
Re: Help with opening a text file
I created a new project with 1 textbox and copy-pasted your code as shown into my form. Created the HelpText.txt file at the required location and all worked perfectly.
Is the code you posted the exact code you are using ?
Can you step through your code line by line and post which line it fails on ?
Can you also check in debug mode/immediate window what the expected location is of App.Path & "\HELPTEXT.txt" and check the file exists there.
Re: Help with opening a text file
Right. This code is all that's in the form. It fails on the line "Text1.Text = Input$(LOF(Channel), 1)" and the error message comes up "Bad File Mode". The file also exists and it's in the right place.
I also have to say that this was working earlier :$ Although this is the only code in the form, it's only one of the forms in my project. I have been tinkering with the rest of the project for a while, so I have no idea when this stopped working. The thing is, I didn't think that changing other parts of the program would lead to "Bad file mode" error - it's either the correct mode or it's not. What I've said probably hasnt been much help, sorry. It's so frustrating since it was working a-ok earlier :$.
Edit: I've tried printing the file with Line Input into a PictureBox, and it works. I think I might just use this method instead, although the previous *did* work and still should but just doesn't for some reason :(.
Re: Help with opening a text file
Have you created a function or form or something else and named it INPUT? If so, that may be the problem.
Re: Help with opening a text file
Quote:
Originally Posted by
LaVolpe
Have you created a function or form or something else and named it INPUT? If so, that may be the problem.
Nope. Nothing else in the code for that form. Thanks anyway. I'll just mark this resolved.
Re: Help with opening a text file
Does it make a difference if you use the #channel in the input$ command instead of the 1 ?
Code:
Private Sub Form_Load()
Dim Channel As Integer
Channel = FreeFile(0)
Open App.Path & "\HELPTEXT.txt" For Input As #Channel
Text1.Text = Input$(LOF(Channel), #Channel)
Close #Channel
End Sub
Because you said that it used to work and now it doesn't. Did you add code anywhere, even on another form or module, which uses that same file but opens it for binary or append,etc... ?
Re: Help with opening a text file
Quote:
Originally Posted by
Optional
Does it make a difference if you use the #channel in the input$ command instead of the 1 ?
Code:
Private Sub Form_Load()
Dim Channel As Integer
Channel = FreeFile(0)
Open App.Path & "\HELPTEXT.txt" For Input As #Channel
Text1.Text = Input$(LOF(Channel), #Channel)
Close #Channel
End Sub
Because you said that it used to work and now it doesn't. Did you add code anywhere, even on another form or module, which uses that same file but opens it for binary or append,etc... ?
"Channel" doesnt seem to make a difference (tried it using 3 instead, but nothing changed). Nope, this form is the only place where that file is needed. It's just so confusing :S