Export results from two queries to .xlsm (1 Viewer)

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
Hello,

I’m a beginner Access user. I’m trying to export the results from two queries "TestData" and "ReportDate" to a saved .xlsm file. These queries are generated from tables linked to a SQL server.

The first query “TestData” should be copied to a sheet named ”TestData” and the second query “ReportDate” should be copied to a sheet named “ReportDate” in the same Workbook ”Test.xlsm”.
Both queries have two parametesr passed from Form1 [Start Date] and [End Date].

I’ve found several examples that I’ve tried to imitate but need you help correcting my code.

Thank you so much for your help in advance:

Code:
Public Sub ExportToExcel()

'Step 1: Declare your variables

Dim XL As Excel.Application
Dim wbTarget As Workbook
Dim ReportDate As QueryDef
Dim TestData As QueryDef


Dim rsTestData As Recordset
Dim rsReportDate As Recordset

Dim strSQL As String
Dim strSQL2 As String
Dim i As Integer
Dim j As Integer


'pass SQL to a variable

strSQL = "SELECT dbo_Patient.PatientId, dbo_Patient.HospitalID, dbo_Patient.lastnm, dbo_Patient.firstnm, dbo_Patient.categoryCd, dbo_Patient.IncludeMailInd, dbo_Sample.LogTime, dbo_Sample.SampleDt, dbo_Test.AssignedDt, dbo_Test.TestTypeCd, dbo_Test.TestId, dbo_Test.OrderNbr, dbo_UserTest.SoftTestCodes, dbo_UserTest.Repeats, dbo_Test.ReportableCommentsTxt"
FROM ((dbo_Patient INNER JOIN dbo_Sample ON dbo_Patient.PatientId = dbo_Sample.PatientId) INNER JOIN dbo_Test ON dbo_Sample.SampleID = dbo_Test.SampleId) INNER JOIN dbo_UserTest ON dbo_Test.TestId = dbo_UserTest.TestId
WHERE (((dbo_Patient.lastnm) Not In ("CDR","Research","UCLA","CAP","LABOTXBULK","SOFT-HLA","SOFTBULKIP6","SOFTBULKIP3","SOFT-ASAP-PEDS","TESTPatient")) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ("AT1R","AT1R_RPT","C1Q_I","C1Q_I_RPT","C1Q_II","C1Q_II_RP","FL__PRAI","FL__PRAI_RPT","FL__PRAII","FL__PRAII_RPT","FL_EPC_XM_ALLO","FL_EPC_XM_ALLO_RPT","FL_XM_ALLO","FL_XM_ALLO_RPT","FL_XM_AUTO","FL_XM_AUTO_RPT","GP__I","GP__IDv2","GP__II","GP__MICA","GP_MICARPT","IBEAD_SABI","IBEAD_SABI_RPT","LCTI","LPRI","LPRI_RPT","LPRII","LPRII_RPT","LSM__MICA","LSMI","LSMI_RPT","LSMII","LSMII_RPT","LSMMICA_RPT","MICA_SAB","SABI","SABI_Dilution","SABI_IgM","SABI_RPT","SABII","SABII_Dilution","SABII_IgM","SABII_RPT","Serum_Stored")));


strSQL2 = "SELECT dbo_PatientReport.Patientid, dbo_PatientReport.ReportNm, dbo_PatientReport.SignedOffDt, dbo_PatientReportDetail.ParameterNm, dbo_PatientReportDetail.ValueTxt"
FROM dbo_PatientReport INNER JOIN dbo_PatientReportDetail ON dbo_PatientReport.PatientReportId = dbo_PatientReportDetail.PatientReportId
WHERE (((dbo_PatientReport.SignedOffDt) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_PatientReportDetail.ParameterNm) Like "*Antibody*" Or (dbo_PatientReportDetail.ParameterNm) Like "*Crossmatch*") AND ((dbo_PatientReportDetail.ValueTxt)<>""));


'set up reference to the query to export
Set qdfTestData = CurrentDb.QueryDefs("TestData", strSQL)
Set qdfReportDate = CurrentDb.QueryDefs("ReporDate", strSQL2)


'Execute the query
Set rsTestData = qdfTestData.OpenRecordset()
Set rsReportDate = qdfReportDate.OpenRecordset()
'programatically refence Excel
Set XL = CreateObject("Excel.application")


'Set reference to the export workbook
Set wbTarget = XL.Workbooks.Open("W:\HLA.lab\Mila K\ACCESS\TEST.xlsm")
'clear excel sheet
wbTarget.Worksheets("TestData").Cells.ClearContents
'Use paste from recordset to put in excel sheet
wbTarget.Worksheets("TestData").Cells(1, 1).CopyFromRecordset rsTestData
'clear excel sheet
wbTarget.Worksheets("ReportDate").Cells.ClearContents
'Use paste from recordset to put in excel sheet
wbTarget.Worksheets("ReportDate").Cells(1, 1).CopyFromRecordset rsReportDate

'Add column heading names to both workbooks
        For i = 1 To rsTestData.Fields.Count
            xlApp.ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
        Next i
        xlApp.Cells.EntireColumn.AutoFit
    End With
    
    For i = 1 To rsReportDate.Fields.Count
            xlApp.ActiveSheet.Cells(1, j).Value = MyRecordset.Fields(j - 1).Name
        Next j
        xlApp.Cells.EntireColumn.AutoFit
    End With
    
'save workbook
wbTarget.Save
End Sub
 
Last edited:

GinaWhipp

AWF VIP
Local time
Today, 09:16
Joined
Jun 21, 2011
Messages
5,899
Hmm, well a little help here...

Where exactly is the problem? I hate to start from scratch if I don't have to, so, does the query execute? Does the Workbook get populated? Have you stepped thru the code and found a line causing the problem?
 

richsql

Registered User.
Local time
Today, 14:16
Joined
Feb 9, 2015
Messages
23
First thing that I can see is a problem with your strSQL and strSQL2 variables, there is no line continuation for the string ...

You might want to start by tidying it up to something like


Code:
 'pass SQL to a variable
  
 strSQL = "SELECT dbo_Patient.PatientId," & _
                          " dbo_Patient.HospitalID," & _
                          " dbo_Patient.lastnm,"
  
 etc ...
capture the output using debug.print strSQL, copy the output into a new query object to validate the SQL is syntactically correct...
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
Hello,

I'm not sure how to format after FROM?

Thanks

strSQL = "SELECT dbo_Patient.PatientId," & _
"dbo_Patient.HospitalID," & _
"dbo_Patient.lastnm," & _
"dbo_Patient.firstnm," & _
"dbo_Patient.categoryCd," & _
"dbo_Patient.IncludeMailInd," & _
"dbo_Sample.LogTime," & _
"dbo_Sample.SampleDt," & _
"dbo_Test.AssignedDt," & _
"dbo_Test.TestTypeCd," & _
"dbo_Test.TestId," & _
"dbo_Test.OrderNbr," & _
"dbo_UserTest.SoftTestCodes," & _
"dbo_UserTest.Repeats," & _
"dbo_Test.ReportableCommentsTxt FROM ((dbo_Patient INNER JOIN dbo_Sample ON dbo_Patient.PatientId = dbo_Sample.PatientId) INNER JOIN dbo_Test ON dbo_Sample.SampleID = dbo_Test.SampleId) INNER JOIN dbo_UserTest ON dbo_Test.TestId = dbo_UserTest.TestId"
WHERE (((dbo_Patient.lastnm) Not In ("CDR","Research","UCLA","CAP","LABOTXBULK","SOFT-HLA","SOFTBULKIP6","SOFTBULKIP3","SOFT-ASAP-PEDS","TESTPatient")) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ("AT1R","AT1R_RPT","C1Q_I","C1Q_I_RPT","C1Q_II","C1Q_II_RP","FL__PRAI","FL__PRAI_RPT","FL__PRAII","FL__PRAII_RPT","FL_EPC_XM_ALLO","FL_EPC_XM_ALLO_RPT","FL_XM_ALLO","FL_XM_ALLO_RPT","FL_XM_AUTO","FL_XM_AUTO_RPT","GP__I","GP__IDv2","GP__II","GP__MICA","GP_MICARPT","IBEAD_SABI","IBEAD_SABI_RPT","LCTI","LPRI","LPRI_RPT","LPRII","LPRII_RPT","LSM__MICA","LSMI","LSMI_RPT","LSMII","LSMII_RPT","LSMMICA_RPT","MICA_SAB","SABI","SABI_Dilution","SABI_IgM","SABI_RPT","SABII","SABII_Dilution","SABII_IgM","SABII_RPT","Serum_Stored")));"
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
When I run the code it stops at
Code:
WHERE (((dbo_Patient.lastnm) Not In ("CDR","Research","UCLA","CAP","LABOTXBULK","SOFT-HLA","SOFTBULKIP6","SOFTBULKIP3","SOFT-ASAP-PEDS","TESTPatient")) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ("AT1R","AT1R_RPT","C1Q_I","C1Q_I_RPT","C1Q_II","C1Q_II_RP","FL__PRAI","FL__PRAI_RPT","FL__PRAII","FL__PRAII_RPT","FL_EPC_XM_ALLO","FL_EPC_XM_ALLO_RPT","FL_XM_ALLO","FL_XM_ALLO_RPT","FL_XM_AUTO","FL_XM_AUTO_RPT","GP__I","GP__IDv2","GP__II","GP__MICA","GP_MICARPT","IBEAD_SABI","IBEAD_SABI_RPT","LCTI","LPRI","LPRI_RPT","LPRII","LPRII_RPT","LSM__MICA","LSMI","LSMI_RPT","LSMII","LSMII_RPT","LSMMICA_RPT","MICA_SAB","SABI","SABI_Dilution","SABI_IgM","SABI_RPT","SABII","SABII_Dilution","SABII_IgM","SABII_RPT","Serum_Stored")));"[QUOTE="MilaK, post: 1411132, member: 137748"]Hello,

I'm not sure how to format after FROM?

Thanks

strSQL = "SELECT dbo_Patient.PatientId," & _
"dbo_Patient.HospitalID," & _
"dbo_Patient.lastnm," & _
"dbo_Patient.firstnm," & _
"dbo_Patient.categoryCd," & _
"dbo_Patient.IncludeMailInd," & _
"dbo_Sample.LogTime," & _
"dbo_Sample.SampleDt," & _
"dbo_Test.AssignedDt," & _
"dbo_Test.TestTypeCd," & _
"dbo_Test.TestId," & _
"dbo_Test.OrderNbr," & _
"dbo_UserTest.SoftTestCodes," & _
"dbo_UserTest.Repeats," & _
"dbo_Test.ReportableCommentsTxt FROM ((dbo_Patient INNER JOIN dbo_Sample ON dbo_Patient.PatientId = dbo_Sample.PatientId) INNER JOIN dbo_Test ON dbo_Sample.SampleID = dbo_Test.SampleId) INNER JOIN dbo_UserTest ON dbo_Test.TestId = dbo_UserTest.TestId"
WHERE (((dbo_Patient.lastnm) Not In ("CDR","Research","UCLA","CAP","LABOTXBULK","SOFT-HLA","SOFTBULKIP6","SOFTBULKIP3","SOFT-ASAP-PEDS","TESTPatient")) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ("AT1R","AT1R_RPT","C1Q_I","C1Q_I_RPT","C1Q_II","C1Q_II_RP","FL__PRAI","FL__PRAI_RPT","FL__PRAII","FL__PRAII_RPT","FL_EPC_XM_ALLO","FL_EPC_XM_ALLO_RPT","FL_XM_ALLO","FL_XM_ALLO_RPT","FL_XM_AUTO","FL_XM_AUTO_RPT","GP__I","GP__IDv2","GP__II","GP__MICA","GP_MICARPT","IBEAD_SABI","IBEAD_SABI_RPT","LCTI","LPRI","LPRI_RPT","LPRII","LPRII_RPT","LSM__MICA","LSMI","LSMI_RPT","LSMII","LSMII_RPT","LSMMICA_RPT","MICA_SAB","SABI","SABI_Dilution","SABI_IgM","SABI_RPT","SABII","SABII_Dilution","SABII_IgM","SABII_RPT","Serum_Stored")));"[/QUOTE]
 

Bastard

New member
Local time
Today, 06:16
Joined
Dec 18, 2013
Messages
4
I'm thinking it doesn't like the quotes inside the SQL string... change them to single quotes

Code:
   strSQL = "SELECT dbo_Patient.PatientId," & _
            "dbo_Patient.HospitalID," & _
            "dbo_Patient.lastnm," & _
            "dbo_Patient.firstnm," & _
            "dbo_Patient.categoryCd," & _
            "dbo_Patient.IncludeMailInd," & _
            "dbo_Sample.LogTime," & _
            "dbo_Sample.SampleDt," & _
            "dbo_Test.AssignedDt," & _
            "dbo_Test.TestTypeCd," & _
            "dbo_Test.TestId," & _
            "dbo_Test.OrderNbr," & _
            "dbo_UserTest.SoftTestCodes," & _
            "dbo_UserTest.Repeats," & _
            "dbo_Test.ReportableCommentsTxt " & _
            "FROM ((dbo_Patient INNER JOIN dbo_Sample ON dbo_Patient.PatientId = dbo_Sample.PatientId) " & _
            "INNER JOIN dbo_Test ON dbo_Sample.SampleID = dbo_Test.SampleId) " & _
            "INNER JOIN dbo_UserTest ON dbo_Test.TestId = dbo_UserTest.TestId" & _
            "WHERE (((dbo_Patient.lastnm) " & _
            "Not In ('CDR','Research','UCLA','CAP','LABOTXBULK','SO FT-HLA','SOFTBULKIP6','SOFTBULKIP3','SOFT-ASAP-PEDS','TESTPatient')) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ('AT1R','AT1R_RPT','C1Q_I','C1Q_I_RPT','C1Q_II','C 1Q_II_RP','FL__PRAI','FL__PRAI_RPT','FL__PRAII','F L__PRAII_RPT','FL_EPC_XM_ALLO','FL_EPC_XM_ALLO_RPT ','FL_XM_ALLO','FL_XM_ALLO_RPT','FL_XM_AUTO','FL_X M_AUTO_RPT','GP__I','GP__IDv2','GP__II','GP__MICA' ,'GP_MICARPT','IBEAD_SABI','IBEAD_SABI_RPT','LCTI' ,'LPRI','LPRI_RPT','LPRII','LPRII_RPT','LSM__MICA' ,'LSMI','LSMI_RPT','LSMII','LSMII_RPT','LSMMICA_RP T','MICA_SAB','SABI','SABI_Dilution','SABI_IgM','S ABI_RPT','SABII','SABII_Dilution','SABII_IgM','SAB II_RPT','Serum_Stored')));"
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
Thank you,

I've formatted the second SQL as well. When I run the code I get an error at Like " * Antibody * ""variable not defined". How do I resolve this error?

Code:
"Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_PatientReportDetail.ParameterNm) Like " * Antibody * " Or (dbo_PatientReportDetail.ParameterNm) Like " * Crossmatch * ") AND ((dbo_PatientReportDetail.ValueTxt)<>""));"

Code:
strSQL2 = "SELECT dbo_PatientReport.Patientid," & _
                  "dbo_PatientReport.ReportNm," & _
                  "dbo_PatientReport.SignedOffDt," & _
                  "dbo_PatientReportDetail.ParameterNm," & _
                  "dbo_PatientReportDetail.ValueTxt" & _
                  "FROM (dbo_PatientReport)" & _
                  "INNER JOIN dbo_PatientReportDetail ON dbo_PatientReport.PatientReportId = dbo_PatientReportDetail.PatientReportId" & _
                 "WHERE (((dbo_PatientReport.SignedOffDt) " & _
                 "Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_PatientReportDetail.ParameterNm)[B] Like " * Antibody * "[/B] Or (dbo_PatientReportDetail.ParameterNm) Like " * Crossmatch * ") AND ((dbo_PatientReportDetail.ValueTxt)<>""));"
 
Last edited:

Bastard

New member
Local time
Today, 06:16
Joined
Dec 18, 2013
Messages
4
Again, you have double quotes inside the statement.. change all the double quotes inside the statement to single quotes OR four double quotes """"


EXAMPLE: Your code
Code:
Like " * Antibody * "


The way it should be
Code:
Like ' * Antibody * '

or if single quotes doesn't fix it, try four double quotes inside the statement
Code:
Like """" * Antibody * """"
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
Thank you,

The next error message is "Wrong number of arguments or invalid property assignment."

Code:
'set up reference to the query to export

Set qdfTestData = CurrentDb.QueryDefs("TestData", strSQL)
Set qdfReportDate = CurrentDb.QueryDefs("ReporDate", strSQL2)

How do I correctly reference the query to export?

I really appreciate your help!
 

richsql

Registered User.
Local time
Today, 14:16
Joined
Feb 9, 2015
Messages
23
I believe it should read

Code:
 Set qdfTestData = CurrentDb.CreateQueryDef("TestData", strSQL)
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
I have an error message saying that Object "TestData" already exists and debugger stops at the same line.
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
Here is my edited code: I get en error message when I try to run this code from a button on a Form that Object "TestData2" already exist. Please help!:banghead:

Code:
Public Sub ExportToExcel()

'Step 1: Declare your variables

Dim XL As Excel.Application

Dim wbTarget As Workbook

Dim qdfTestData2 As QueryDef
Dim qdfReportDate As QueryDef

Dim rsTestData2 As Recordset
Dim rsReportDate As Recordset

Dim strSQL As String
Dim strSQL2 As String
Dim i As Integer
Dim j As Integer


'SQL and pass to a variable


 strSQL = "SELECT dbo_Patient.PatientId," & _
                   "dbo_Patient.HospitalID," & _
                   "dbo_Patient.lastnm," & _
                   "dbo_Patient.firstnm," & _
                   "dbo_Patient.categoryCd," & _
                   "dbo_Patient.IncludeMailInd," & _
                   "dbo_Sample.LogTime," & _
                   "dbo_Sample.SampleDt," & _
                   "dbo_Test.AssignedDt," & _
                   "dbo_Test.TestTypeCd," & _
                   "dbo_Test.TestId" & _
         "FROM ((dbo_Patient INNER JOIN dbo_Sample ON dbo_Patient.PatientId = dbo_Sample.PatientId) " & _
"INNER JOIN dbo_Test ON dbo_Sample.SampleID = dbo_Test.SampleId) " & _
"INNER JOIN dbo_UserTest ON dbo_Test.TestId = dbo_UserTest.TestId" & _
"WHERE (((dbo_Patient.lastnm)" & _
"Not In ('CDR','Research','UCLA','CAP','LABOTXBULK','SO FT-HLA','SOFTBULKIP6','SOFTBULKIP3','SOFT-ASAP-PEDS','TESTPatient')) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ('AT1R','AT1R_RPT','C1Q_I','C1Q_I_RPT','C1Q_II','C 1Q_II_RP','FL__PRAI','FL__PRAI_RPT','FL__PRAII','F L__PRAII_RPT','FL_EPC_XM_ALLO','FL_EPC_XM_ALLO_RPT ','FL_XM_ALLO','FL_XM_ALLO_RPT','FL_XM_AUTO','FL_X M_AUTO_RPT','GP__I','GP__IDv2','GP__II','GP__MICA','GP_MICARPT','IBEAD_SABI','IBEAD_SABI_RPT','LCTI','LPRI','LPRI_RPT','LPRII','LPRII_RPT','LSM__MICA','LSMI','LSMI_RPT','LSMII','LSMII_RPT','LSMMICA_RP T','MICA_SAB','SABI','SABI_Dilution','SABI_IgM','S ABI_RPT','SABII','SABII_Dilution','SABII_IgM','SAB II_RPT','Serum_Stored')));"

strSQL2 = "SELECT dbo_PatientReport.ReportNm," & _
                  "dbo_PatientReport.SignedOffDt," & _
                  "dbo_PatientReport.Patientid," & _
                  "dbo_PatientReportDetail.ParameterNm," & _
                  "dbo_PatientReportDetail.ValueTxt" & _
                  "FROM (dbo_PatientReport)" & _
                  "INNER JOIN dbo_PatientReportDetail ON dbo_PatientReport.PatientReportId = dbo_PatientReportDetail.PatientReportId" & _
                 "WHERE (((dbo_PatientReport.SignedOffDt) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_PatientReportDetail.ParameterNm) Like '*Antibody*' Or (dbo_PatientReportDetail.ParameterNm) Like '* Crossmatch *') AND ((dbo_PatientReportDetail.ValueTxt)<>""));"



'set up reference to the query to export

   Set qdfTestData2 = CurrentDb.CreateQueryDef("TestData2", strSQL)
   Set qdfReportDate = CurrentDb.CreateQueryDef("ReportDate", strSQL2)
   
'Execute the query
Set rsTestData2 = qdfTestData2.OpenRecordset()
Set rsReportDate = qdfReportDate.OpenRecordset()
'programatically refence Excel
Set XL = CreateObject("Excel.application")

'Set reference to the export workbook
Set wbTarget = XL.Workbooks.Open("D:\yelekley\Desktop\ACCESS\Test.xlsm")
'clear excel sheet
wbTarget.Worksheets("TestData").Cells.ClearContents
'Use paste from recordset to put in excel sheet
wbTarget.Worksheets("TestData").Cells(2, 1).CopyFromRecordset rsTestData2
'clear excel sheet
'wbTarget.Worksheets("ReportDate").Cells.ClearContents
'Use paste from recordset to put in excel sheet
'wbTarget.Worksheets("ReportDate").Cells(1, 1).CopyFromRecordset rsReportDate

'Add column heading names to both workbooks
        'For i = 1 To rsTestData2.Fields.Count
            'xlApp.ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
        'Next i
        'xlApp.Cells.EntireColumn.AutoFit
    'End With
    
    'For j = 1 To rsReportDate.Fields.Count
           ' xlApp.ActiveSheet.Cells(1, j).Value = MyRecordset.Fields(j - 1).Name
        'Next j
        'xlApp.Cells.EntireColumn.AutoFit
    
    
'save workbook
wbTarget.Save
End Sub
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
When I add

Code:
.QueryDefs.Delete ("TestData2")

before

Code:
Set qdfTestData2 = CurrentDb.CreateQueryDef("TestData2", strSQL)

The query is deleted and now I get a message that I'm missing an operator.

Could someone please tell me what I'm missing here:

Code:
strSQL = "SELECT dbo_Patient.PatientId," & _
                   "dbo_Patient.HospitalID," & _
                   "dbo_Patient.lastnm," & _
                   "dbo_Patient.firstnm," & _
                   "dbo_Patient.categoryCd," & _
                   "dbo_Patient.IncludeMailInd," & _
                   "dbo_Sample.LogTime," & _
                   "dbo_Sample.SampleDt," & _
                   "dbo_Test.AssignedDt," & _
                   "dbo_Test.TestTypeCd," & _
                   "dbo_Test.TestId" & _
         "FROM ((dbo_Patient INNER JOIN dbo_Sample ON dbo_Patient.PatientId = dbo_Sample.PatientId) " & _
"INNER JOIN dbo_Test ON dbo_Sample.SampleID = dbo_Test.SampleId) " & _
"INNER JOIN dbo_UserTest ON dbo_Test.TestId = dbo_UserTest.TestId" & _
"WHERE (((dbo_Patient.lastnm)" & _
"Not In ('CDR','Research','UCLA','CAP','LABOTXBULK','SO FT-HLA','SOFTBULKIP6','SOFTBULKIP3','SOFT-ASAP-PEDS','TESTPatient')) AND ((dbo_Sample.LogTime) Between [Forms]![Form1]![Start Date] And [Forms]![Form1]![End Date]) AND ((dbo_Test.TestTypeCd) In ('AT1R','AT1R_RPT','C1Q_I','C1Q_I_RPT','C1Q_II','C 1Q_II_RP','FL__PRAI','FL__PRAI_RPT','FL__PRAII','F L__PRAII_RPT','FL_EPC_XM_ALLO','FL_EPC_XM_ALLO_RPT ','FL_XM_ALLO','FL_XM_ALLO_RPT','FL_XM_AUTO','FL_X M_AUTO_RPT','GP__I','GP__IDv2','GP__II','GP__MICA','GP_MICARPT','IBEAD_SABI','IBEAD_SABI_RPT','LCTI','LPRI','LPRI_RPT','LPRII','LPRII_RPT','LSM__MICA','LSMI','LSMI_RPT','LSMII','LSMII_RPT','LSMMICA_RP T','MICA_SAB','SABI','SABI_Dilution','SABI_IgM','S ABI_RPT','SABII','SABII_Dilution','SABII_IgM','SAB II_RPT','Serum_Stored')));"
 
Last edited:

richsql

Registered User.
Local time
Today, 14:16
Joined
Feb 9, 2015
Messages
23
Missing a space change "FROM to " FROM

A good way to test these SQL strings is to step over the variable, capture the output in the immediate window (debug.print strSQL) and then create a new query with the output. Any syntax issues will be flagged which will assist in amending the strSQL string until it is correct...
 

MilaK

Registered User.
Local time
Today, 06:16
Joined
Feb 9, 2015
Messages
285
Thanks for helping me debug SQL.

I get a new error message when it tries to run the query.

Is this because of [Start Date] and [End Date] parameters?


I have tried two versions of the code but get error messages in both cases

1. Set rsTestData2 = qdfTestData2.OpenRecordset(dbOpenDynaset, dbSeeChanges)
2. Set rsTestData2 = qdfTestData2.OpenRecordset()
Run time error ‘3061’ too few parameters expected 2.

:rolleyes:
 

richsql

Registered User.
Local time
Today, 14:16
Joined
Feb 9, 2015
Messages
23
That does seem likely, before you associate it to your recordset you should add the required parameters - as these are dates I think you will need to pass them in the format "mm/dd/yyyy"

Code:
 qdfTestData2.Parameters("[Start Date]") = "02/16/2015"
 

Users who are viewing this thread

Top Bottom