LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Appending to a file with run-time error '54' Bad file mode

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_Pa ram(4)),
Val(V_Param(6))))
L_year =
Year(ThisWorkbook.Worksheets("DATA").Cells(Val(V_P aram(4)),
Val(V_Param(6))))
L_hour =
Hour(ThisWorkbook.Worksheets("DATA").Cells(Val(V_P aram(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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
appending one Excel file to another ? George Lewycky Excel Worksheet Functions 2 November 14th 08 07:37 PM
Appending an Excel File to another Wisconsin Excel Discussion (Misc queries) 0 April 14th 08 07:38 PM
appending to excel file [email protected] Excel Programming 0 May 29th 06 09:25 PM
Appending a second csv file Rick Excel Discussion (Misc queries) 3 March 9th 05 06:21 PM
Appending to File Progster Excel Programming 1 September 22nd 03 02:24 PM


All times are GMT +1. The time now is 08:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"