Can't a file be written in the Unload event of a Form? I am trying to populate a text file with some text in the Form's Unload event function:

Code:
Private Sub Form_Unload(Cancel As Integer)
    Dim FileName As String
    Dim iFile    As Integer

    If (Form2.chkLoad.Value = 0) Then
        FileName = App.Path & "\MyFile.txt"

        'the file is a hidden file; so changing it to a normal file for editing
        SetAttr FileName, vbNormal
        Open FileName For Output As #iFile
            Write #iFile, "Windows"
        Close #iFile
        SetAttr FileName, vbHidden
    End If

    Unload Me
End Sub
But the above code generates the

Bad file name or number.

error pointing to the red line in the above code. I also tried the above code in the Form's QueryUnload event function but it generates the same error.

Is there any way to edit a file when the Form is unloaded?