John Tipton
John Tipton

Reputation: 253

Access not closed when added DoCmd.Close line

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.

Upvotes: 0

Views: 740

Answers (3)

Steve W
Steve W

Reputation: 476

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

Upvotes: 0

SunKnight0
SunKnight0

Reputation: 3351

If you are looking to close Access completely, use:

Application.Quit

Upvotes: 0

Sergey S.
Sergey S.

Reputation: 6336

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

Upvotes: 2

Related Questions