Append to a TXT document using variable file name-problem

hall95213

Registered User.
Local time
Today, 16:42
Joined
Mar 18, 2013
Messages
12
When I execute the below function I get an error. If I use the commented out "Open" line it works fine. I get Run-time error '52'. Bad file name or number.

Private Sub GenerateSSA_Click()
Dim strRA As Variant, strFileNew As String, strFileBlank As String
Dim intFileOut As Integer
Dim rstWork As DAO.Recordset
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef


intFileOut = FreeFile()

strFileBlank = Me![MPathSSABlank]
strFileNew = Me![MPathSSANew]

If FileExists(strFile) = False Then
FileCopy strFileBlank, strFileNew
End If


' Open "c:\users\john employee\documents\access\ssatsi2013.txt" For Append As intFileOut

Open strFileNew For Append As intFileOut
 
What is the value of the variable strFileNew?
Because if the below line works it could indicate that the value of strFileNew is not equal to:
"c:\users\john employee\documents\access\ssatsi2013.txt"

Code:
Open "c:\users\john employee\documents\access\ssatsi2013.txt" For Append As intFileOut
 
Do you mean ...

Code:
If FileExists(strFile) = False Then
FileCopy strFileBlank, strFileNew
End If

as you haven't defined strFile, or

Code:
If FileExists(strFile[b]New[/b]) = False Then
FileCopy strFileBlank, strFileNew
End If
 
What is the value of the variable strFileNew?
Because if the below line works it could indicate that the value of strFileNew is not equal to:
"c:\users\john employee\documents\access\ssatsi2013.txt"

Code:
Open "c:\users\john employee\documents\access\ssatsi2013.txt" For Append As intFileOut

The value of strFileNew is "c:\users\john employee\documents\access\ssatsi2013.txt"
I displayed the value using a msg just to make sure.
 
Do you mean ...

Code:
If FileExists(strFile) = False Then
FileCopy strFileBlank, strFileNew
End If

as you haven't defined strFile, or

Code:
If FileExists(strFile[b]New[/b]) = False Then
FileCopy strFileBlank, strFileNew
End If

I have a blank txt file on hard drive, and just copy it to a new file name
specified on a form. This part works. It's the "open" line. It doesn't like
the use of the variable??
 
I've just made a small example and it works ok, but I think you're overlooking what nanscombe writes.
You haven't defined strFile and you have not assign it a value. Check if the file exist in the path.

Code:
Dim strRA As Variant, strFileNew As String, strFileBlank As String
Dim intFileOut As Integer
Dim rstWork As DAO.Recordset
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef


intFileOut = FreeFile()

'strFileBlank = Me![MPathSSABlank]
strFileNew = "C:\Dokumenter\ssatsi2013.txt"

'If FileExists(strFile) = False Then
'FileCopy strFileBlank, strFileNew
'End If


' Open "c:\users\john employee\documents\access\ssatsi2013.txt" For Append As intFileOut

Open strFileNew For Append As intFileOut
Close intFileOut
 
Nanscombe was absolitely correct. I did not assign anything to strFile.
StrFile should have been named strFileNew.

Can you see if you can take the filename from a form and get the same
Result? I believe thats where I was getting the error.

I REALLY appreciate your help!!
 
I have just tried to recreate the problem above, including creating the user "john employee" and using the code below, but was unable to recreate the problem. :(

Code:
[COLOR="Red"]Private Sub Form_Current()
  Me.MPathSSABlank = "c:\users\john employee\documents\access\ssatsi2013.txt"
  Me.MPathSSANew = "c:\users\john employee\documents\access\Test01.txt"
End Sub[/COLOR]

Private Sub GenerateSSA_Click()
Dim strRA As Variant, strFileNew As String, strFileBlank As String
Dim intFileOut As Integer
'Dim rstWork As DAO.Recordset
'Dim dbs As DAO.Database
'Dim qdf As DAO.QueryDef


  intFileOut = FreeFile()

  strFileBlank = Me![MPathSSABlank]
  strFileNew = Nz(Me![MPathSSANew])

  If FileExists(strFileNew) = False Then
    FileCopy strFileBlank, strFileNew
  End If


' Open "c:\users\john employee\documents\access\ssatsi2013.txt" For Append As intFileOut

  Open strFileNew For Append As intFileOut
      [COLOR="red"]Print #intFileOut, Now()
  Close #intFileOut[/COLOR]
End Sub

[COLOR="red"]Private Function FileExists(ByVal theFile As String)
On Error Resume Next
  FileExists = -1
  FileExists = FileLen(theFile)
  FileExists = (FileExists >= 0)
End Function[/COLOR]

I've added the bits in red for testing purposes.
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom