Help exporting parameter query to excel

jd.willis

Registered User.
Local time
Today, 17:39
Joined
Jun 7, 2010
Messages
28
:confused::confused::confused::confused:

I'm working on exporting a parameter query to excel. The code I am using is what I have used previously without using a parameter and the code works great! Since I have added the code for the parameter query the code is now giving a Runtime 5 error. Any help would be greatly appreciated! Thank you in advance for any and all help!

CODE:
PHP:
Private Sub NEKMH310_Click()
    On Error Resume Next
    Dim sCriteria As String
    Dim db As Database
    Dim rst As Recordset
    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim strTemplatePath As String
    Dim sOutput As String
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
     Set db = CurrentDb()
    
Set qdf = db.QueryDefs("10q_Daily_eKMH310_Charge_Data (NE)")
qdf![forms!10f_kmh310_publish!post_date] = [Forms]![10f_kmh310_publish]![Post_Date]
qdf.Parameters(0) = Post_Date
    sCriteria = " 1 = 1 "
    If COID <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = """ & COID & """"
    End If
    If RPT_DATE <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE like """ & RPT_DATE & """"
    End If
    If DEPT <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT = """ & DEPT & """"
    End If
      
    If DEPT_Name <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = """ & DEPT_Name & """"
    End If
    
    If PT_NAME <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT_Name = """ & PT_NAME & """"
    End If
    
    If PAT_ACCT <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PAT_acct = """ & PAT_ACCT & """"
    End If
    
    If CDM <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM = """ & CDM & """"
    End If
    
    If CDM_DESC <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM_DESC = """ & CDM_DESC & """"
    End If
    If Post_Date <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = """ & Post_Date & """"
    End If
    If TRANS_DATE <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = """ & TRANS_DATE & """"
    End If
    
    If QTY <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].QTY = """ & QTY & """"
    End If
    
    If PT <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT = """ & PT & """"
    End If
    
    If RVU <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RVU = """ & RVU & """"
    End If
    
    If AMOUNT <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].AMOUNT = """ & AMOUNT & """"
    End If
    If BATCH <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].BATCH = """ & BATCH & """"
    End If
   
    
    
    'This is new
    strTemplatePath = "W:\HER\HER - Administration\revops\RevOps_Databases\Monarch\ROSS_Uploads\01 - Daily\Excel Templates\NE_EKMH310 (2010-02-23).xlt" ' template file reference
    sOutput = "W:\HER\HER - Patient Access Management\PasCoord\Process Improvement Team\Jason\Revops\NE_eKMH310" & " (" & Format(Date, "yyyy-mm-dd") & ").xls" 'output file name and path
    
   
    
    'This is new
    Set objApp = New Excel.Application
    'This is new 'Your excel spreadsheet file goes here
    Set objBook = objApp.Workbooks.Add(strTemplatePath)
    'Name of sheet you want to export to
    Set objApp = objBook.Parent
    Set objSheet = objBook.Worksheets("KMH310")
    objBook.Windows(1).Visible = True
    'Opens the recordset and sets the variable
    
    
        With objSheet
        .Select
        'Clears the current contents in the workbook range
        .Range("A9:O65000").ClearContents
        'rst Copies the recordset into the worksheet
        .Range("A9").CopyFromRecordset rst
        
        
    End With
    objBook.SaveAs (sOutput)
    objBook.Close
    rst.Close
    objApp.Visible = False
    Set rst = Nothing
    Set db = Nothing
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objApp = Nothing
    MsgBox "NE eKMH310 has been published"
End Sub

breakpoint:
PHP:
.Range("A9").CopyFromRecordset rst
 
Am I missing where you set or open the rst recordset to equal anything?
 
What are the data types of the following fields?

COID
RPT_DATE
DEPT
DEPT_NAME
PT_Name
PAT_acct
CDM
CDM_DESC
POST_DATE
TRANS_DATE
QTY
PT
RVU
AMOUNT
BATCH

How are you supplying the parameter values? via a form?

For numeric fields, the correct sytax would be as follows

(assuming COID is numeric as an example)

Code:
sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = " & me.COID




For text fields, they need to be bounded by single quotes
Code:
sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = '" & me.DEPT_Name & "'"


For date fields, they need to be bounded by # signs

Code:
 sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = #" & me.TRANS_DATE & "#"

Also, I'm not sure why you are using the like operator for a date value here:
Code:
sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE like """ & RPT_DATE & """"

For your IF..THEN statements, I would recommend using the NZ() function to test your controls.

If NZ(me.DEPT_Name,"") <> "" Then


I would recommend building the entire query in code rather than using a query def.

mySQL= "SELECT...."

You can then use a debug.print statement and view the actual constructed query in the immediate window for troubleshooting purposes

debug.print mySQL

Once you know the query is correct, they I would worry about dumping the query results to Excel.
 
Am I missing where you set or open the rst recordset to equal anything?

Sorry I had overlooked that. I have added the following:
PHP:
Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")
and now I am getting a Runtime Error 3061 Too Few Parameters. Expected 2.
 
What are the data types of the following fields?

COID = Text
RPT_DATE = Date/Time
DEPT = Number
DEPT_NAME = Text
PT_Name = Text
PAT_acct = Text
CDM = Number
CDM_DESC = Text
POST_DATE = Date/Time
TRANS_DATE = Date/Time
QTY = Number
PT = Text
RVU =Number
AMOUNT = Number
BATCH = Text
How are you supplying the parameter values? via a form? Yes.


Here is the SQL of the query. Where would I place this in the code?
PHP:
PARAMETERS [Post_Date] DateTime;
SELECT [10t_KMH310_Data].COID, ([Post_Date]+1) AS Rpt_Date, [10t_KMH310_Data].Dept_ID AS DEPT, [10t_KMH310_Data].Dept_Description AS DEPT_NAME, [10t_KMH310_Data].Pt_Name, [10t_KMH310_Data].Pt_Num AS PT_ACCT, [10t_KMH310_Data].CDM, [10t_KMH310_Data].CDM_Description AS CDM_DESC, [10t_KMH310_Data].Post_Date, [10t_KMH310_Data].Trans_Date, [10t_KMH310_Data].Qty, [10t_KMH310_Data].PT, [10t_KMH310_Data].RVU, [10t_KMH310_Data].Amount, [10t_KMH310_Data].Batch
FROM 10t_KMH310_Data
WHERE ((([10t_KMH310_Data].COID)="NE") AND (([10t_KMH310_Data].Dept_ID)<>9999) AND (([10t_KMH310_Data].Pt_Num)<>"0000-0000") AND (([10t_KMH310_Data].Post_Date)=[Forms]![10f_KMH310_Publish]![Post_Date] & "#"));
Again Thank you for all of your help!
 
Here is a code snippet showing where to place the variable and with the corrected IF..THEN's for the criteria part of the query.

Code:
    On Error Resume Next
    Dim sCriteria As String
    Dim db As Database
    Dim rst As Recordset
    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim strTemplatePath As String
    Dim sOutput As String
   
     Set db = CurrentDb()
    Dim mySQL As String
    
    
    'construct the SELECT and FROM clauses of the query
    mySQL = "SELECT [10t_KMH310_Data].COID, ([Post_Date]+1) AS Rpt_Date, [10t_KMH310_Data].Dept_ID AS DEPT, [10t_KMH310_Data].Dept_Description AS DEPT_NAME, "
    mySQL = mySQL & " [10t_KMH310_Data].Pt_Name, [10t_KMH310_Data].Pt_Num AS PT_ACCT, [10t_KMH310_Data].CDM, [10t_KMH310_Data].CDM_Description AS CDM_DESC, "
    mySQL = mySQL & " [10t_KMH310_Data].Post_Date, [10t_KMH310_Data].Trans_Date, [10t_KMH310_Data].Qty, [10t_KMH310_Data].PT, [10t_KMH310_Data].RVU, "
    mySQL = mySQL & " [10t_KMH310_Data].Amount, [10t_KMH310_Data].Batch"
    mySQL = mySQL & " FROM 10t_KMH310_Data"
    
    'construct the WHERE Clause
    

    If Nz(Me.COID, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = '" & Me.COID & "'"
    End If
    If Not IsNull(Me.RPT_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE =#" & Me.RPT_DATE & "#"
    End If
    If Nz(Me.DEPT, "") Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT = " & Me.DEPT
    End If
      
    If Nz(Me.DEPT_Name, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = '" & Me.DEPT_Name & "'"
    End If
    
    If Nz(Me.PT_NAME, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT_Name = '" & Me.PT_NAME & "'"
    End If
    
    If Nz(Me.PAT_ACCT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PAT_acct = '" & Me.PAT_ACCT & "'"
    End If
    
    If Nz(Me.CDM, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM = " & Me.CDM
    End If
    
    If Nz(Me.CDM_DESC, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM_DESC = '" & Me.CDM_DESC & "'"
    End If
    If Not IsNull(Me.Post_Date) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & Me.Post_Date & "#"
    End If
    If Not IsNull(Me.TRANS_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = #" & Me.TRANS_DATE & "#"
    End If
    
    If Nz(Me.QTY, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].QTY = " & Me.QTY
    End If
    
    If Nz(Me.PT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT = '" & Me.PT & "'"
    End If
    
    If Nz(Me.RVU, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RVU = " & Me.RVU
    End If
    
    If Nz(Me.AMOUNT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].AMOUNT = " & Me.AMOUNT
    End If
    If Nz(Me.BATCH, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].BATCH = '" & Me.BATCH & "'"
    End If
   
'add the WHERE Clause to the mySQL variable

mySQL = mySQL & " WHERE" & sCriteria

Debug.Print mySQL


'proceed with opening the recordset
 
I have found if I create the query in Access and duplicate it using the form I can compare the 2 from the imediate window where the debug.print prints to to see what I need to tweak to get the code correct.
 
Thank you! I've updated the code like you provided, now I'm getting a runtime error 3061 Too Few Parameters. Expected 2. :(
The parameter is the Post_Date which comes from a form.
 
Now that you have updated the code, can you repost it?
 
PHP:
Private Sub NEKMH310_Click()
    On Error Resume Next
    Dim sCriteria As String
    Dim db As Database
    Dim rst As Recordset
    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim strTemplatePath As String
    Dim sOutput As String
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
     Set db = CurrentDb()
 Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")
If Nz(Me.COID, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = '" & Me.COID & "'"
    End If
    If Not IsNull(Me.RPT_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE =#" & Me.RPT_DATE & "#"
    End If
    If Nz(Me.DEPT, "") Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT = " & Me.DEPT
    End If
      
    If Nz(Me.DEPT_Name, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = '" & Me.DEPT_Name & "'"
    End If
    
    If Nz(Me.PT_NAME, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT_Name = '" & Me.PT_NAME & "'"
    End If
    
    If Nz(Me.PAT_ACCT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PAT_acct = '" & Me.PAT_ACCT & "'"
    End If
    
    If Nz(Me.CDM, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM = " & Me.CDM
    End If
    
    If Nz(Me.CDM_DESC, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM_DESC = '" & Me.CDM_DESC & "'"
    End If
    If Not IsNull(Me.Post_Date) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & Me.Post_Date & "#"
    End If
    If Not IsNull(Me.TRANS_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = #" & Me.TRANS_DATE & "#"
    End If
    
    If Nz(Me.QTY, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].QTY = " & Me.QTY
    End If
    
    If Nz(Me.PT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT = '" & Me.PT & "'"
    End If
    
    If Nz(Me.RVU, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RVU = " & Me.RVU
    End If
    
    If Nz(Me.AMOUNT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].AMOUNT = " & Me.AMOUNT
    End If
    If Nz(Me.BATCH, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].BATCH = '" & Me.BATCH & "'"
    End If
   
'add the WHERE Clause to the mySQL variable
mySQL = mySQL & " WHERE" & sCriteria
Debug.Print mySQL

'proceed with opening the recordset
   
    
    
    'This is new
    strTemplatePath = "W:\HER\HER - Administration\revops\RevOps_Databases\Monarch\ROSS_Uploads\01 - Daily\Excel Templates\NE_EKMH310 (2010-02-23).xlt" ' template file reference
    sOutput = "W:\HER\HER - Patient Access Management\PasCoord\Process Improvement Team\Jason\Revops\NE_eKMH310" & " (" & Format(Date, "yyyy-mm-dd") & ").xls" 'output file name and path
    
   
    
    'This is new
    Set objApp = New Excel.Application
    'This is new 'Your excel spreadsheet file goes here
    Set objBook = objApp.Workbooks.Add(strTemplatePath)
    'Name of sheet you want to export to
    Set objApp = objBook.Parent
    Set objSheet = objBook.Worksheets("KMH310")
    objBook.Windows(1).Visible = True
    'Opens the recordset and sets the variable
    
    Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")
        With objSheet
        .Select
        'Clears the current contents in the workbook range
        .Range("A9:O65000").ClearContents
        'rst Copies the recordset into the worksheet
        .Range("A9").CopyFromRecordset rst
        
        
    End With
    objBook.SaveAs (sOutput)
    objBook.Close
    rst.Close
    objApp.Visible = False
    Set rst = Nothing
    Set db = Nothing
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objApp = Nothing
    MsgBox "NE eKMH310 has been published"
End Sub

Having the ME. statements produces a compile error method or data member not found message.
When I remove the ME. is when I get the runtime error 3061

Thank you again for your help!
 
Code:
 Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")

The statement above needs to be moved to after where you construct the query and needs to reference the query you constructed in code not your originally saved query in Access.



Code:
 Set rst = db.OpenRecordset(mySQL)

I don't remember if the mySQL needs to be enclosed within double quotes ("mySQL"). Try the above first; if it fails, then enclose in quotes and try again.
 
Now I am getting Runtime Error 3075
Syntax Error (missing operator) in query expression 'AND [10q_daily_ekmh310_charge_data (ne)].Rpt_date=## and [10q_daily_eKMH310_charge_data (ne)].Post_date=#5/25/2010# and [10q_daily_eKMH310_charge_data (ne)].Trans_date = ##.


Revised Code:
PHP:
Private Sub NEKMH310_Click()
    On Error Resume Next
    Dim sCriteria As String
    Dim db As Database
    Dim rst As Recordset
    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim strTemplatePath As String
    Dim sOutput As String
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
     Set db = CurrentDb()
'construct the SELECT and FROM clauses of the query
    mySQL = "SELECT [10t_KMH310_Data].COID, ([Post_Date]+1) AS Rpt_Date, [10t_KMH310_Data].Dept_ID AS DEPT, [10t_KMH310_Data].Dept_Description AS DEPT_NAME, "
    mySQL = mySQL & " [10t_KMH310_Data].Pt_Name, [10t_KMH310_Data].Pt_Num AS PT_ACCT, [10t_KMH310_Data].CDM, [10t_KMH310_Data].CDM_Description AS CDM_DESC, "
    mySQL = mySQL & " [10t_KMH310_Data].Post_Date, [10t_KMH310_Data].Trans_Date, [10t_KMH310_Data].Qty, [10t_KMH310_Data].PT, [10t_KMH310_Data].RVU, "
    mySQL = mySQL & " [10t_KMH310_Data].Amount, [10t_KMH310_Data].Batch"
    mySQL = mySQL & " FROM 10t_KMH310_Data"
    
    'construct the WHERE Clause
 
If Nz(COID, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = '" & COID & "'"
    End If
    If Not IsNull(RPT_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE =#" & RPT_DATE & "#"
    End If
    If Nz(DEPT, "") Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT = " & DEPT
    End If
      
    If Nz(DEPT_Name, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = '" & DEPT_Name & "'"
    End If
    
    If Nz(PT_NAME, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT_Name = '" & PT_NAME & "'"
    End If
    
    If Nz(PAT_ACCT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PAT_acct = '" & PAT_ACCT & "'"
    End If
    
    If Nz(CDM, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM = " & CDM
    End If
    
    If Nz(CDM_DESC, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM_DESC = '" & CDM_DESC & "'"
    End If
    If Not IsNull(Post_Date) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & Post_Date & "#"
    End If
    If Not IsNull(TRANS_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = #" & TRANS_DATE & "#"
    End If
    
    If Nz(QTY, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].QTY = " & QTY
    End If
    
    If Nz(PT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT = '" & PT & "'"
    End If
    
    If Nz(RVU, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RVU = " & RVU
    End If
    
    If Nz(AMOUNT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].AMOUNT = " & AMOUNT
    End If
    If Nz(BATCH, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].BATCH = '" & BATCH & "'"
    End If
   
'add the WHERE Clause to the mySQL variable
mySQL = mySQL & " WHERE" & sCriteria
Debug.Print mySQL

'proceed with opening the recordset
   
    
  Set rst = db.OpenRecordset(mySQL)
    'This is new
    strTemplatePath = "W:\HER\HER - Administration\revops\RevOps_Databases\Monarch\ROSS_Uploads\01 - Daily\Excel Templates\NE_EKMH310 (2010-02-23).xlt" ' template file reference
    sOutput = "W:\HER\HER - Patient Access Management\PasCoord\Process Improvement Team\Jason\Revops\NE_eKMH310" & " (" & Format(Date, "yyyy-mm-dd") & ").xls" 'output file name and path
    
   
    
    'This is new
    Set objApp = New Excel.Application
    'This is new 'Your excel spreadsheet file goes here
    Set objBook = objApp.Workbooks.Add(strTemplatePath)
    'Name of sheet you want to export to
    Set objApp = objBook.Parent
    Set objSheet = objBook.Worksheets("KMH310")
    objBook.Windows(1).Visible = True
    'Opens the recordset and sets the variable
    
    Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")
        With objSheet
        .Select
        'Clears the current contents in the workbook range
        .Range("A9:O65000").ClearContents
        'rst Copies the recordset into the worksheet
        .Range("A9").CopyFromRecordset rst
        
        
    End With
    objBook.SaveAs (sOutput)
    objBook.Close
    rst.Close
    objApp.Visible = False
    Set rst = Nothing
    Set db = Nothing
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objApp = Nothing
    MsgBox "NE eKMH310 has been published"
End Sub

Break point:
PHP:
  Set rst = db.OpenRecordset(mySQL)
:confused:

Thanks again for all of your assistance with this!
 
Are the form controls for rpt_date, trans_date and post_date set to short date format?

Did you provide values for the rpt_date and trans_date in your form?

If not, you may have to switch the IF..THEN's for these and the post_date from the Not IsNull() to the NZ() function.
 
The only parameter being entered is the POST_DATE.

The post_date field on the form is set to short date.
I changed the RPT_DATE and TRANS_Date to the NZ function.

Now I'm getting Run-time error 3075
Syntax Error (missing operator) in query expression 'AND [10q_Daily_eKMH310_Charge_data (ne)].Post_Date = #5/25/2010#.

PHP:
Private Sub NEKMH310_Click()
    On Error Resume Next
    Dim sCriteria As String
    Dim db As Database
    Dim rst As Recordset
    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim strTemplatePath As String
    Dim sOutput As String
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
     Set db = CurrentDb()
'construct the SELECT and FROM clauses of the query
    mySQL = "SELECT [10t_KMH310_Data].COID, ([Post_Date]+1) AS Rpt_Date, [10t_KMH310_Data].Dept_ID AS DEPT, [10t_KMH310_Data].Dept_Description AS DEPT_NAME, "
    mySQL = mySQL & " [10t_KMH310_Data].Pt_Name, [10t_KMH310_Data].Pt_Num AS PT_ACCT, [10t_KMH310_Data].CDM, [10t_KMH310_Data].CDM_Description AS CDM_DESC, "
    mySQL = mySQL & " [10t_KMH310_Data].Post_Date, [10t_KMH310_Data].Trans_Date, [10t_KMH310_Data].Qty, [10t_KMH310_Data].PT, [10t_KMH310_Data].RVU, "
    mySQL = mySQL & " [10t_KMH310_Data].Amount, [10t_KMH310_Data].Batch"
    mySQL = mySQL & " FROM 10t_KMH310_Data"
    
    'construct the WHERE Clause
 
If Nz(COID, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = '" & COID & "'"
    End If
    If Nz(RPT_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE =#" & RPT_DATE & "#"
    End If
    If Nz(DEPT, "") Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT = " & DEPT
    End If
      
    If Nz(DEPT_Name, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = '" & DEPT_Name & "'"
    End If
    
    If Nz(PT_NAME, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT_Name = '" & PT_NAME & "'"
    End If
    
    If Nz(PAT_ACCT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PAT_acct = '" & PAT_ACCT & "'"
    End If
    
    If Nz(CDM, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM = " & CDM
    End If
    
    If Nz(CDM_DESC, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM_DESC = '" & CDM_DESC & "'"
    End If
    If Nz(Post_Date) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & Post_Date & "#"
    End If
    If Nz(TRANS_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = #" & TRANS_DATE & "#"
    End If
    
    If Nz(QTY, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].QTY = " & QTY
    End If
    
    If Nz(PT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT = '" & PT & "'"
    End If
    
    If Nz(RVU, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RVU = " & RVU
    End If
    
    If Nz(AMOUNT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].AMOUNT = " & AMOUNT
    End If
    If Nz(BATCH, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].BATCH = '" & BATCH & "'"
    End If
   
'add the WHERE Clause to the mySQL variable
mySQL = mySQL & " WHERE" & sCriteria
Debug.Print mySQL

'proceed with opening the recordset
   
    
  Set rst = db.OpenRecordset(mySQL)
    'This is new
    strTemplatePath = "W:\HER\HER - Administration\revops\RevOps_Databases\Monarch\ROSS_Uploads\01 - Daily\Excel Templates\NE_EKMH310 (2010-02-23).xlt" ' template file reference
    sOutput = "W:\HER\HER - Patient Access Management\PasCoord\Process Improvement Team\Jason\Revops\NE_eKMH310" & " (" & Format(Date, "yyyy-mm-dd") & ").xls" 'output file name and path
    
   
    
    'This is new
    Set objApp = New Excel.Application
    'This is new 'Your excel spreadsheet file goes here
    Set objBook = objApp.Workbooks.Add(strTemplatePath)
    'Name of sheet you want to export to
    Set objApp = objBook.Parent
    Set objSheet = objBook.Worksheets("KMH310")
    objBook.Windows(1).Visible = True
    'Opens the recordset and sets the variable
    
    Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")
        With objSheet
        .Select
        'Clears the current contents in the workbook range
        .Range("A9:O65000").ClearContents
        'rst Copies the recordset into the worksheet
        .Range("A9").CopyFromRecordset rst
        
        
    End With
    objBook.SaveAs (sOutput)
    objBook.Close
    rst.Close
    objApp.Visible = False
    Set rst = Nothing
    Set db = Nothing
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objApp = Nothing
    MsgBox "NE eKMH310 has been published"
End Sub

Thank you for helping me on this.!
 
You have this: If Nz(Post_Date) Then
The appropriate syntax is this: If Nz(Post_Date, "")="" Then

At what line does the error occur? If you make it past the debug.print mySQL statement, you can go to the immediate window and see the entire content of the mySQL variable. If you copy and paste the text to a new query in Access & run it, you may be able to find out what is wrong.
 
Ok, I have copied the mysql to a new query and modified the query so that it is working. I have also copied the updated sql statement into the mysql statement:

it is breaking on
PHP:
Set rst = db.OpenRecordset(mySQL)


CODE:
PHP:
Private Sub NEKMH310_Click()
    On Error Resume Next
    Dim sCriteria As String
    Dim db As Database
    Dim rst As Recordset
    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim strTemplatePath As String
    Dim sOutput As String
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
     Set db = CurrentDb()
'construct the SELECT and FROM clauses of the query
    mySQL = "SELECT [SELECT [10t_KMH310_Data].COID, ([Post_Date]+1) AS Rpt_Date, [10t_KMH310_Data].Dept_ID AS DEPT, [10t_KMH310_Data].Dept_Description AS DEPT_NAME, [10t_KMH310_Data].Pt_Name, [10t_KMH310_Data].Pt_Num AS PT_ACCT, [10t_KMH310_Data].CDM, [10t_KMH310_Data].CDM_Description AS CDM_DESC, [10t_KMH310_Data].Post_Date, [10t_KMH310_Data].Trans_Date, [10t_KMH310_Data].Qty, [10t_KMH310_Data].PT, [10t_KMH310_Data].RVU, [10t_KMH310_Data].Amount, [10t_KMH310_Data].Batch WHERE ((([10t_KMH310_Data].Post_Date)=!forms.10f_kmh310_post_date));"
    
    
    
    'construct the WHERE Clause
 
If Nz(COID, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].COID = '" & COID & "'"
    End If
    If Nz(RPT_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RPT_DATE =#" & RPT_DATE & "#"
    End If
    If Nz(DEPT, "") Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT = " & DEPT
    End If
      
    If Nz(DEPT_Name, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].DEPT_NAME = '" & DEPT_Name & "'"
    End If
    
    If Nz(PT_NAME, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT_Name = '" & PT_NAME & "'"
    End If
    
    If Nz(PAT_ACCT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PAT_acct = '" & PAT_ACCT & "'"
    End If
    
    If Nz(CDM, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM = " & CDM
    End If
    
    If Nz(CDM_DESC, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].CDM_DESC = '" & CDM_DESC & "'"
    End If
    If Nz(Post_Date) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & Post_Date & "#"
    End If
    If Nz(TRANS_DATE) Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].TRANS_DATE = #" & TRANS_DATE & "#"
    End If
    
    If Nz(QTY, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].QTY = " & QTY
    End If
    
    If Nz(PT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].PT = '" & PT & "'"
    End If
    
    If Nz(RVU, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].RVU = " & RVU
    End If
    
    If Nz(AMOUNT, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].AMOUNT = " & AMOUNT
    End If
    If Nz(BATCH, "") <> "" Then
        sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].BATCH = '" & BATCH & "'"
    End If
   
'add the WHERE Clause to the mySQL variable
mySQL = mySQL & " WHERE" & sCriteria
Debug.Print mySQL

'proceed with opening the recordset
   
    
  Set rst = db.OpenRecordset(mySQL)
    'This is new
    strTemplatePath = "W:\HER\HER - Administration\revops\RevOps_Databases\Monarch\ROSS_Uploads\01 - Daily\Excel Templates\NE_EKMH310 (2010-02-23).xlt" ' template file reference
    sOutput = "W:\HER\HER - Patient Access Management\PasCoord\Process Improvement Team\Jason\Revops\NE_eKMH310" & " (" & Format(Date, "yyyy-mm-dd") & ").xls" 'output file name and path
    
   
    
    'This is new
    Set objApp = New Excel.Application
    'This is new 'Your excel spreadsheet file goes here
    Set objBook = objApp.Workbooks.Add(strTemplatePath)
    'Name of sheet you want to export to
    Set objApp = objBook.Parent
    Set objSheet = objBook.Worksheets("KMH310")
    objBook.Windows(1).Visible = True
    'Opens the recordset and sets the variable
    
    Set rst = db.OpenRecordset("10q_Daily_eKMH310_Charge_Data (NE)")
        With objSheet
        .Select
        'Clears the current contents in the workbook range
        .Range("A9:O65000").ClearContents
        'rst Copies the recordset into the worksheet
        .Range("A9").CopyFromRecordset rst
        
        
    End With
    objBook.SaveAs (sOutput)
    objBook.Close
    rst.Close
    objApp.Visible = False
    Set rst = Nothing
    Set db = Nothing
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objApp = Nothing
    MsgBox "NE eKMH310 has been published"
End Sub

THANK YOU SOO MUCH For all of your help with this I really appreciate it!
 
The problem lies in your form reference in the sCriteria. From the SQL text I extracted this portion:

WHERE ((([10t_KMH310_Data].Post_Date)=!forms.10f_kmh310_post_date));"


You have to use the me. prefix to reference the form from which the button is clicked. In your code you have this:

sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & Post_Date & "#"

It should be this:
sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & me.Post_Date & "#"

BUT... it does not look like post_date is the name of the control on your form. What is the actual form control name where you enter the desired post_date? Is it: 10f_kmh310_post_date?

If so then the code most show:
sCriteria = sCriteria & " AND [10q_Daily_eKMH310_Charge_Data (NE)].POST_DATE = #" & me.10f_kmh310_post_date & "#"

What is the form name? What is the control name?

Is it possible for you to post the DB (with any sensitive data removed)?
 
I'm making a copy of it now that I can strip down. as it stands right now the db file size is 1.4 GB. I have already removed all daily transaction detail except for one day! I will look at the summary tables. It also may take a while I have to remove patient data also.

Thanks again. Will update shortly
 
I've removed all sensitive data copied just the table and query that i'm working with here to a new db. I have also removed all other COID info other than NE.

Thank you soo much for your help!\
 

Attachments

Since the only parameter is the posting date, the rest of the criteria that prompted for parameters was not necessary. I did not test the Excel part of the code; I only made sure that you had a valid query being supplied to the open recordset command. The modified DB is attached.
 

Attachments

Users who are viewing this thread

Back
Top Bottom