I just realized that the code I listed here does not actually show the
import function, but a call to it. It is a really simple function so I will
just change it below to match.
I am not using CreateQuerydef. The query already exists, I am running
through a list of users and exporting the query using the OutputTo method to
put the query into a spreadsheet. Here is the code:
Public Sub AdminExport(sPath As String, sQueryName As String, bEmail As
Boolean)
Dim cnConn As ADODB.Connection
Dim rsUsers As ADODB.Recordset
Dim sSql As String, sFile As String
Dim lPrgCnt As Long
Set cnConn = CurrentProject.Connection
Set rsUsers = New ADODB.Recordset
sSql = "SELECT AgentId FROM tblUser ORDER BY AgentId"
rsUsers.Open sSql, cnConn, adOpenStatic, adLockOptimistic
DoCmd.OpenForm "frmExporting"
Form_frmExporting.prgExport.Max = rsUsers.RecordCount
On Error GoTo ErrorHandler
Do Until rsUsers.EOF
' handle exporting functions
msAgentId = rsUsers(0)
Form_frmAdmin.txtAgentId = msAgentId
sFile = msAgentId & " - " & sQueryName & ".xls"
'***************************************
'original function call
'***************************************
'ExportFile sPath, sFile, sQueryName
'***************************************
' Code from the ExportFile function
'***************************************
DoCmd.OutputTo AcOutputQuery, _
sQueryName, _
"MicrosoftExcel 97-2002(*.xls)", _
sPath & sFile
' handle email functions
If bEmail Then
If Not IsNull(DLookup("EmailAddress", "tblUser", "AgentId = '" &
msAgentId & "'")) Then
EmailReport DLookup("EmailAddress", "tblUser", "AgentId = '"
& msAgentId & "'"), _
sPath & sFile, _
Form_frmAdmin.lstReports.Column(1)
Else
InsertExportError "User does not have email.", msAgentId, _
Form_frmAdmin.lstReports.Column(1)
mlErrCnt = mlErrCnt + 1
End If
End If
ResumePoint:
lPrgCnt = lPrgCnt + 1
Form_frmExporting.prgExport = lPrgCnt
rsUsers.MoveNext
Loop
rsUsers.Close
cnConn.Close
Set rsUsers = Nothing
Set cnConn = Nothing
If mlErrCnt > 0 Then Form_frmAdmin.cmdViewErrors.Visible = True
DoCmd.Close
Exit Sub
ErrorHandler:
mlErrCnt = mlErrCnt + 1
' report error to error table
InsertExportError Err.Description, msAgentId,
Form_frmAdmin.lstReports.Column(1)
GoTo ResumePoint
End Sub
I have several queries that I run multiple times from some VBA code. I run
the same query multiple times. On the majority of my queries, they will run
the first time and every time after that, I receive a message that says
"Query must have at least one destination field." When I look at the SQL in
the query, it is all gone. I have searched all over Technet and I haven't
been able to find anything on this problem. Can someone please help?
Care to post your code? Are you in fact using a CreateQuerydef with a
named query?