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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime error '52'

Status
Not open for further replies.

JohnKIT05

Technical User
Feb 18, 2005
19
US
I am new to VB and I am enjoying learning this new programming. Anyway I am getting a runtime error '52' message on my screen which it says "Runtime error '52' Bad file name or number". I am not sure what it the problem is. I copied and paste my program which is shown below.

Sub Openfile()
Const ForReading = 1, ForWriting = 2
Dim fs, f, r, fread, Maxsize, Nextchar, Mychar
Set fs = CreateObject("Scripting.FileSystemObject")
Set r = fs.OpenTextFile("c:\850whsetest.tmp", ForReading)
'Set w = fs.OpenTextFile("c:\850whsetest.tmp", ForWriting)
fread = r.readline
Input #1, fread
'Maxsize = LOF(1)
Do While i < 14
Seek #1, i
Mychar = Input(1, #1)
Select Case i
Case 8
If Mychar >= 0 Then
r.Write ("A")
End If
Case 9
If Mychar >= 0 Then
r.Write ("M")
End If
Case 10
If Mychar >= 0 Then
r.Write ("P")
End If
Case 11
If Mychar >= 0 Then
r.Write ("H")
End If
End Select
Loop
r.Close
End Sub
 
You're mixing different methods for reading the file. You can't use Input #1, fread with the file scripting object.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
MM. I kinda understand what you are saying but how I am supposed to....position the seek command in the line of the file for me to check if that position is a number or not for the program to replace with a character in the file. I though the Input# command will tell the program that #1 is the fread which is the file I am trying to read and write to the file.

Do I make sense or no?

Thanks for the response

John

This site is great.
 
If you're going to use seek, then you have to use the VB Open statement, not the scripting object. Peruse help for Open.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Sorry to bother here but now I am getting a new runtime error. I am learning I must admit. The error is 'Runtime error 63 Bad record number'. Here is the new changes I amde shown below:

Sub Openfile()
Close #1
Dim fs, f, r, fread, Maxsize, Nextchar, Mychar
Open "c:\850whsetest.tmp" For Input As #1
Input #1, r
Do While i < 14
Seek #1, i
Mychar = Input(1, #1)
Select Case i
Case 8
If Mychar >= 0 Then
r.Write ("A")
End If
Case 9
If Mychar >= 0 Then
r.Write ("M")
End If
Case 10
If Mychar >= 0 Then
r.Write ("P")
End If
Case 11
If Mychar >= 0 Then
r.Write ("H")
End If
End Select
Loop
Close #1
End Sub
 
I relaize I made a mistake in the loop but still the same runtime error. Latest update shown below:

Sub Openfile()
Close #1
Dim fs, f, r, fread, Maxsize, Nextchar, Mychar
Dim i as Single
i = 0
Open "c:\850whsetest.tmp" For Input As #1
Input #1, r
Do While i < 14
Seek #1, i
Mychar = Input(1, #1)
Select Case i
Case 8
If Mychar >= 0 Then
r.Write ("A")
End If
Case 9
If Mychar >= 0 Then
r.Write ("M")
End If
Case 10
If Mychar >= 0 Then
r.Write ("P")
End If
Case 11
If Mychar >= 0 Then
r.Write ("H")
End If
End Select
i = 1 + i
Loop
Close #1
End Sub
 
What is it you are trying to accomplish? Is this a binary or text file? If it's text, than you can just read in the whole line and use the mid statement to test each character.

What is "r.write"? You defined it as a variant, so I don't know why it would have the "write" property associated to it - doh!

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Artie,

Sorry if I am driving you crazy haha. The file is a text file. What I am trying to accomplish on this VB program is to replace numeric characters starting on the 8th position to alpha. Any numbers starting on the 8th position to be replace with "AMPHIRE".

John
 
Let's forget about the file input for a sec. Do you want the following scenario:

abcdefg12345678 --> abcdefgamphire

or

abcdefg12345678 --> abcdefgamphireamphireamphireamphireamphireamphireamphireamphire

or does it depend on the numeric value or something else?

Post what incoming text is and what you want it to look like, then we can hep ya betta. :)

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Artie,

Last night, I decided to buy a book on VB 6 so I can understand the inout/write statements.

The first line of the edi files looks like this:

^^BEGVN5665

I want the VB program to change the first line to this:

^^BEGVNAMPHIRE

John


 
Ah, EDI, I know it well, unfortunately! ;-)

Let's assume the field positions are fixed (which they should be), that the id value is the last field, you want to translate the exact number and you've read the "^^BEGVN5665" value into a variable called line, then this should do the trick:

Code:
if mid(line,8) = "5665" then line = mid(line,1,7) + "AMPHIRE"

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Artie,

I read the Mid statement in the new book I bought last night and it makes sense. This time no errors but the change is not working. The 5665 is still there. Here is the new code:

Sub Openfile()
Close #1
Dim fs, f, line, fread, Maxsize, Nextchar, Mychar
Dim i As Single
Open "c:\850whsetest.tmp" For Input As #1
Input #1, line
Do Until (EOF(1) = True)
Input #1, line
If Mid(line, 8) = "5665" Then line = Mid(line, 1, 7) + "AMPHIRE"
Loop
Close #1
End Sub
 
I assume you want to replace that value in the file. The only way to do that within VB is read in the file a line at a time and write it out to a new file. Here's what it would look like:
Code:
Sub Openfile()

'   declare variables as the appropriate type

Dim line as string
dim infile as integer, outfile as integer

'   get file handles

infile = freefile
outfile = freefile

Open "c:\850whsetest.tmp" For Input As #infile
Open "c:\850whsetest.new" For ouput As #outfile

Do while not EOF(infile)   ' no need for = true test
   Line Input #infile, line   ' read in the whole line
   If Mid(line, 8) = "5665" Then line = Mid(line, 1, 7)+ "AMPHIRE"
   Print #outfile, line
Loop

Close #inflie
close #outfile

End Sub

Of course, there's no error checking in this example. I've taken the liberty to add good coding practices to it.

Notice the do while loop. That lets you do the read in the loop, as the EOF is true after the last line is read, so you will process the last line, then fall out of the loop.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
ArtieChoke,

With respect your infile will be the same as your outfile.

outfile = freefile

should follow

Open "c:\850whsetest.tmp" For Input As #infile

regards Hugh,
 
Doh! Thx for spotting that, HughLerwill.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top