Triscuit
Registered User.
- Local time
- Today, 13:52
- Joined
- Jul 20, 2010
- Messages
- 27
Hello,
I'm trying to export a query of my database structure to an excel file:
I have a Query based on form control and a form with a combobox and Command button to execute export.
I'm having problems opening the SQL statement or query in the OpenRecordset command.
I get two different errors:
1. When using the query as the variable:
Run-time error 3061
Too few Parameters expected 1.
2. When using the vba string as the variable:
Run-time error 3141:
The SELECT statement includes a use of a reserved word, punctuation error or misspelling.
My SQL code is as follows:
I have also tried to save the SQL statement in the function itself
by doing:
The export function is called in the On Click property of the command button and the variable are passed through by
to the function:
I'm trying to export a query of my database structure to an excel file:
I have a Query based on form control and a form with a combobox and Command button to execute export.
I'm having problems opening the SQL statement or query in the OpenRecordset command.
I get two different errors:
1. When using the query as the variable:
Run-time error 3061
Too few Parameters expected 1.
2. When using the vba string as the variable:
Run-time error 3141:
The SELECT statement includes a use of a reserved word, punctuation error or misspelling.
My SQL code is as follows:
Code:
SELECT tblAnalysisInformation.txtClientNumID AS Sys_Sample_Code, "lab_lea" AS Lab_Name_Code, tblAnalysisInformation.txtClientNumID AS Lab_Sample_Id, IIf(tblAnalysisInformation.txtQC="Normal","N","IB") AS Sample_Type_Code, "SW 3810" AS Lab_Anl_Method_Name, tblChemicalsTested.txtCAS AS Cas_Rn, tblChemicalsTested.txtName AS Chemical_Name, IIf(tblSampleInformation.txtMatrix="Solid","ug/kg","mg/m3") AS Result_Unit, "TRG" AS Result_Type_Code, IIf(tblAnalysisResult.txtDetect="Y","Y","N") AS Detect_Flag, tblSampleInformation.numInitDilut AS Dilution_Factor, tblSampleInformation.txtMatrix AS Sample_Matrix_Code, "N" AS Total_or_Dissovled, tblAnalysisInformation.dateAnalyzedOn AS Analysis_Date, tblAnalysisInformation.timeAnalyzed AS Analysis_Time, tblSampleInformation.txtCaseNum AS Lab_Batch_Number
FROM tblSampleInformation INNER JOIN (tblChemicalsTested INNER JOIN (tblAnalysisInformation INNER JOIN tblAnalysisResult ON tblAnalysisInformation.txtLabNum = tblAnalysisResult.txtLabnum) ON tblChemicalsTested.txtCAS = tblAnalysisResult.txtCas) ON tblSampleInformation.txtClientNumID = tblAnalysisInformation.txtClientNumID
WHERE (((tblSampleInformation.txtCaseNum)=[Forms]![frmEZEDD]![cboBatchGroup]));
I have also tried to save the SQL statement in the function itself
by doing:
Code:
Dim strSQL As String
strSQL = "SELECT tblAnalysisInformation.txtClientNumID AS Sys_Sample_Code, 'lab_lea' AS Lab_Name_Code, tblAnalysisInformation.txtClientNumID AS Lab_Sample_Id, IIf(tblAnalysisInformation.txtQC='Normal','N','IB') AS Sample_Type_Code, 'SW 3810' AS Lab_Anl_Method_Name, tblChemicalsTested.txtCAS AS Cas_Rn, tblChemicalsTested.txtName AS Chemical_Name, IIf(tblSampleInformation.txtMatrix='Solid','ug/kg','mg/m3') AS Result_Unit, 'TRG' AS Result_Type_Code, IIf(tblAnalysisResult.txtDetect='Y','Y','N') AS Detect_Flag, tblSampleInformation.numInitDilut AS Dilution_Factor, tblSampleInformation.txtMatrix AS Sample_Matrix_Code, 'N' AS Total_or_Dissovled, tblAnalysisInformation.dateAnalyzedOn AS Analysis_Date, tblAnalysisInformation.timeAnalyzed AS Analysis_Time, tblSampleInformation.txtCaseNum AS Lab_Batch_Number" & _
"FROM tblSampleInformation INNER JOIN (tblChemicalsTested INNER JOIN (tblAnalysisInformation INNER JOIN tblAnalysisResult ON tblAnalysisInformation.txtLabNum = tblAnalysisResult.txtLabnum) ON tblChemicalsTested.txtCAS = tblAnalysisResult.txtCas) ON tblSampleInformation.txtClientNumID = tblAnalysisInformation.txtClientNumID" & _
"WHERE (((tblSampleInformation.txtCaseNum)=[Forms]![frmEZEDD]![cboBatchGroup]))"
The export function is called in the On Click property of the command button and the variable are passed through by
Code:
Call SendTQ2XLWbSheet(strSQL, "sheetName", "C:\Documents and Settings\akoerner\My Documents\EZEDD Output\'" & Forms!frmEZEDD!cboBatchGroup & "'")
to the function:
Code:
Public Function SendTQ2XLWbSheet(strTQName As String, strSheetName As String, strFilePath As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
' strFilePath is the name and path of the file you want to send this data into.
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo err_handler
strPath = strFilePath
Set rst = CurrentDb.OpenRecordset(strTQName)