0

I have added DoCmd.Close acQuery, "Import", acSaveNo

And my access window doesn't close even with this line of code.

Option Compare Database

Option Explicit
Public Function Import()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim intFile As Integer
Dim strFilePath As String
Dim intCount As Integer
Dim strHold
strFilePath = "C:\Transfer\FromSynapseTest\TEST.csv"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Import", dbOpenForwardOnly)
intFile = FreeFile
Open strFilePath For Output As #intFile
Do Until rst.EOF
   If CDate(rst(3)) >= Date And rst(98) <> 0 Then
       For intCount = 0 To rst.Fields.Count - 1
           strHold = strHold & rst(intCount).Value & "|"
       Next
       If Right(strHold, 1) = "|" Then
           strHold = Left(strHold, Len(strHold) - 1)
       End If
       Print #intFile, strHold
   End If
   rst.MoveNext
   strHold = vbNullString
Loop
Close intFile
rst.Close
Set rst = Nothing

DoCmd.Close acQuery, "Import", acSaveNo
End Function

Since I'm calling the function by macro, I don't think I can do

Sub subToCloseForm

    DoCmd.Close

End Sub

Also I have tried DoCmd.Close acQuery, " ", acSaveNo based on what I read http://www.blueclaw-db.com/docmd_close_example.htm : If you leave the objecttype and objectname arguments blank (the default constant, acDefault, is assumed for objecttype), Microsoft Access closes the active window

Any help would be greatly appreciated. Thank you.

John Tipton
  • 213
  • 1
  • 2
  • 10
  • Maybe It's impossible to quit MS Access?? Any input please? http://stackoverflow.com/questions/12063404/is-there-an-on-close-event-for-ms-access-vba/12063577#12063577 – John Tipton Jan 12 '17 at 22:52
  • 1
    If all you want to do is close Access, simply use 'Application.Quit' – Craig Gross Jan 13 '17 at 01:58
  • What window? Close entire Access? I am not understanding your overall question. Calling DAO recordsets of queries is a background process and needs no call of `DoCmd.Open` or `DoCmd.Close`. – Parfait Jan 13 '17 at 02:08
  • @Parfait thank you for your comment! I meant the entire Access window! – John Tipton Jan 13 '17 at 15:10

3 Answers3

2

You don't need code DoCmd.Close acQuery, "Import", acSaveNo at all. This command tries to close a query "Import", but you didn't open this query. You opened a recordset, based on this query and you closed the recordset correctly.

If you need to close the form with name "Import", use

DoCmd.Close acForm, "Import", acSaveNo
Sergey S.
  • 6,296
  • 1
  • 14
  • 29
0

If you are looking to close Access completely, use:

Application.Quit
SunKnight0
  • 3,331
  • 1
  • 10
  • 8
0

Your line of code DoCmd.Close acQuery, "Import", acSaveNo is not necessary as you are opening a recordset, not the query. rst.close and set rst = nothing is sufficient for memory management.

On a side note, I would recommend including an if statement for stepping through your recordset. If the recordset is blank, you will receive an error if left unchecked. Try inserting your for loop inside this if statement:

If not rst.eof and not rst.bof then
     'for loop...

end if
Steve W
  • 416
  • 1
  • 3
  • 19