ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error message: "Bad file name or number" (https://www.excelbanter.com/excel-programming/454290-error-message-bad-file-name-number.html)

RG III

Error message: "Bad file name or number"
 
I know this code has worked for me before on other machines, but does anyone see a problem with this code:

'------------------------
Public Sub MyFileReader()

Dim f As Integer
Dim sFile As String

sFile = "C:\data.txt"

Open sFile For Input As f

Close f

End Sub
'-----------------------

The error message that I get is: "Bad file name or number".
And yes, the data.txt file exists on the root C: drive and the file is correctly named.

- Rob

RG III

Error message: "Bad file name or number"
 
Never mind... I was missing the darn FreeFile() line. Duh!

RG III

Error message: "Bad file name or number"
 
But the code doesn't even work when I use FreeFile(). What's wrong he

Public Sub ReadFile_OpenMethod()

Dim f As Integer
Dim sFile As String
Dim sTxt As String

f = FreeFile()

sFile = "C:\data.txt"

Open sFile For Input As f

While Not EOF(f)
Line Input #f, sTxt
Wend

Close f

End Sub



The error message is "File not found", but it's in the correct directory?

GS[_6_]

Error message: "Bad file name or number"
 
You should implement reusable procedures for file I/O operations so you don't
have to code for it every time. Here's what I use; - it just requires a
filename to read/write text, optionally whether to add to the file when
writing.

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Sub WriteTextFile(TextOut$, Filename$, _
Optional AppendMode As Boolean = False)
' Reusable procedure that Writes/Overwrites or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & TextOut;
Else
Open Filename For Output As #iNum: Print #iNum, TextOut;
End If

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFile()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

GS[_6_]

Error message: "Bad file name or number"
 
You should implement reusable procedures for file I/O operations so you don't
have to code for it every time. Here's what I use; - it just requires a
filename to read/write text, optionally whether to add to the file when
writing.

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Sub WriteTextFile(TextOut$, Filename$, _
Optional AppendMode As Boolean = False)
' Reusable procedure that Writes/Overwrites or Appends
' large amounts of data to a Text file in one single step.
' **Does not create a blank line at the end of the file**
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & TextOut;
Else
Open Filename For Output As #iNum: Print #iNum, TextOut;
End If

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFile()


Note that WriteTextFile does not insert a blank line at the end of the file and
so is why a new line is prepended to the text when adding to an existing file.
Otherwise the file is overwritten!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

GS[_6_]

Error message: "Bad file name or number"
 
Usage example:

Sub DoThis()
Dim sTextIn$, sFileIn$, sTextOut$, sFileOut$
sFileIn = Get_FileToOpen(): If sFileIn = "" Then Exit Sub

sTextIn = ReadTextFile(sFileIn)
'Do something with sTextIn
sTextOut = DoThat(sTextIn)

'Overwrite the same file
WriteTextFile(sTextOut, sFileIn)
'-OR-
'Add to the same file
WriteTextFile(sTextOut, sFileIn, True)

'-OR- write to a different file
sFileOut = Get_FileToSave(): If sFileOut = "" Then Exit Sub
WriteTextFile(sTextOut, sFileOut)
'-OR- add to a different file
WriteTextFile(sTextOut, sFileOut, True)
End Sub


Function Get_FileToOpen$(Optional FileTypes$)
Dim vFile
If FileTypes = "" Then FileTypes = "All Files ""*.*"", *.*"
vFile = Application.GetOpenFileName(FileTypes)
Get_FileToOpen = IIf(vFile = False, "", vFile)
End Function

Function Get_FileToSave$(Optional FileOut$)
Dim vFile
vFile = Application.GetSaveAsFilename(FileOut)
Get_FileToSave = IIf(vFile = False, "", vFile)
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

RG III

Error message: "Bad file name or number"
 
As always, thank you Gary!


All times are GMT +1. The time now is 11:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com