PC Review


Reply
Thread Tools Rate Thread

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

 
 
blisspikle
Guest
Posts: n/a
 
      3rd Nov 2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: .chm file all links say unavailable when not in run time mode Arne Vajhøj Microsoft ASP .NET 0 24th Jun 2011 12:53 AM
Appending a file Bythsx-Addagio Microsoft Excel Programming 3 12th Mar 2009 03:23 PM
al.exe appending extra null bytes to file manifest file version catphive@gmail.com Microsoft Dot NET 0 1st Aug 2006 07:38 AM
APPENDING DATE AND TIME TO FILE =?Utf-8?B?anJwNDQ0?= Microsoft Access External Data 2 14th Mar 2006 05:46 PM
Upload form - appending date/time to file name =?Utf-8?B?c3VyZnd0aHNoYXJrcw==?= Microsoft Frontpage 3 27th Feb 2004 01:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:59 PM.