Appending to a file with run-time error '54' Bad file mode

B

blisspikle

Hello,

I am getting a run-time error '54' Bad file mode when I run the
following code, I will comment where it happens. What I do is save two
rows of data out to a .csv file with the file in the format of...
FILE_2006_11_03.CSV . This code works just fine on a Windows 2000
running Excel 2000. I am using a Windows XP with Excel 2003 now.

It happens on a line that says "Write #L_column, V_data;". The
L_Column would be "2" in this particular case, and V_data would be data
like "1" or "0". If I debug I can pull the cursor up a couple lines
onto the line "Open V_Param(1) & L_filename For Append Access Read
Write Lock Write As #L_column" and it gives me an error saying the file
is already open. If I put in a line to "Close #L_Column" in before the
line where I open the file, then everything works okay again.

Why do I have to close the file and reopen it? It worked just fine
before?

Thank you,

Sub Run_Part(L_column As Integer)
Dim i As Integer
Dim L_filename As String
Dim L_month As String
Dim L_day As String
Dim L_year As String
Dim L_hour As String
Dim L_minute As String
Dim L_second As String
Dim L_filename_old As String
L_filename = ""
On Error Resume Next
If Right(ThisWorkbook.Worksheets("MAIN").Cells(7, L_column), 1) <>
"\" Then
ThisWorkbook.Worksheets("MAIN").Cells(7, L_column) = _
ThisWorkbook.Worksheets("MAIN").Cells(7, L_column) & "\"
End If
For i = 1 To 12
V_Param(i) = ThisWorkbook.Worksheets("MAIN").Cells(6 + i,
L_column)
Next i
ThisWorkbook.UpdateLink Name:=ThisWorkbook.LinkSources '
ThisWorkbook.RefreshAll
L_filename_old = V_Param(10)
L_month =
Month(ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
Val(V_Param(6))))
L_day = Day(ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
Val(V_Param(6))))
L_year =
Year(ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
Val(V_Param(6))))
L_hour =
Hour(ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
Val(V_Param(7))))
L_minute =
Minute(ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
Val(V_Param(7))))
L_second =
Second(ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
Val(V_Param(7))))
If Len(L_month) = 1 Then
L_month = "0" & L_month
End If
If Len(L_day) = 1 Then
L_day = "0" & L_day
End If
If Len(L_year) = 1 Then
L_year = "0" & L_year
End If
If Len(L_hour) = 1 Then
L_hour = "0" & L_hour
End If
If Len(L_minute) = 1 Then
L_minute = "0" & L_minute
End If
If Len(L_second) = 1 Then
L_second = "0" & L_second
End If
L_filename = V_Param(2) & L_year & "_" & L_month & "_" & L_day &
".CSV"
If (L_filename <> L_filename_old) Then
If (File_Already_Open(V_Param(1) & L_filename_old)) Then
Close #L_column
End If
If (File_Already_Open(V_Param(1) & L_filename_old)) Then
GoTo Error_NewFile:
End If
Call File_Copy(L_column)
ThisWorkbook.Worksheets("MAIN").Cells(16, L_column) =
L_filename
'ThisWorkbook.Save
On Error Resume Next
If Not (File_Already_Open(V_Param(1) & L_filename)) Then
'Open V_Param(1) & L_filename For Append Access Read Write
Lock Read Write As #L_column
Open V_Param(1) & L_filename For Append Access Read Write
Lock Write As #L_column
End If
Dim GPC As Integer
Dim V_data As String
For GPC = 1 To Val(V_Param(5))
V_data =
ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(3)), GPC)
Write #L_column, V_data;
Next GPC
Print #L_column, ""
End If
If Not File_Already_Open(V_Param(1) & L_filename) Then
Open V_Param(1) & L_filename For Append Access Read Write Lock
Write As #L_column
End If
For GPC = 1 To Val(V_Param(5))
V_data = ThisWorkbook.Worksheets("DATA").Cells(Val(V_Param(4)),
GPC).Value
' This is where the error occurs.
Write #L_column, V_data;
Next GPC
Print #L_column, ""
On Error Resume Next
Close #L_column
FileCopy V_Param(1) & L_filename, V_Param(12) & L_filename
Open V_Param(1) & L_filename For Append Access Read Write Lock
Write As #L_column
Exit Sub
Error_NewFile:
MsgBox "The New File: " & V_Param(1) & L_filename & " Can Not be
Opened. Please Close" & _
" and then reopen this worksheet!"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top