Compile Error: Argument not Optional (1 Viewer)

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
Hello All,

This is my first post on the forum. I need assistance with this bit of code.

My end result will be a complex crosstab query that is exported to Excel. I believe I have the proper coding for this in a Public Function. I want to call the function with a commandbutton. Here is the code for the button:

Private Sub Command0_Click()
Call SendTQ2ExcelSheet
End Sub

**Now here is the code for the function.**

Public Function SendTQ2ExcelSheet(qryReport_All As String, RawData 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

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 = "\\ushosfps01\Corporate QD\QD Only\QD Associates\Chance\VPReportTest.xlsm"
Set rst = CurrentDb.OpenRecordset(strTQName)
Set ApXL = CreateObject("Excel.Application")

Set xlWBk = ApXL.Workbooks.Open(strPath)
ApXL.Visible = True

Set xlWSh = xlWBk.Worksheets(strSheetName)

xlWSh.Range("A1").Select

For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select

rst.Close
Set rst = Nothing
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function


My issue is that when I click the button I receive this error, "Compile Error: Argument not Optional".

What may I be doing wrong and what should I do to fix it?

Thanks
 

boblarson

Smeghead
Local time
Today, 04:34
Joined
Jan 12, 2001
Messages
32,059
1. you are not sending the parameters that are set up in the header:

Public Function SendTQ2ExcelSheet(qryReport_All As String, RawData As String)

needs to have the query name sent as a string, and RawData (whatever that is) sent as a string.

2. It looks like you took my function and modified it where you shouldn't have made modifications. The qryReport_All As String and RawData As String are two of those places.
 

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
Bob,

I am using your function. I am quite new to VBA and the modifications I made were what I thought I should do to pull my information using your Function.

So with your function what should I be doing so that I can call my query (qryReport_All) and export it to the spreadsheet (RawData)?

Thanks for your help.
 

boblarson

Smeghead
Local time
Today, 04:34
Joined
Jan 12, 2001
Messages
32,059
Bob,

I am using your function. I am quite new to VBA and the modifications I made here what I thought I should do to pull my information using your Function.

So with your function what should I be doing so that I can call my query (qryReport_All) and export it to the spreadsheet (RawData)?

Thanks for your help.

Okay, so you want to export that query to a specific workbook that exists with the sheet named RawData that exists?

if so, use this function instead (I've modified it for you):

Code:
Public Function SendTQ2ExcelSheet(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



    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)

    Set ApXL = CreateObject("Excel.Application")


    Set xlWBk = ApXL.Workbooks.Open(strPath)
    ApXL.Visible = True

    Set xlWSh = xlWBk.Worksheets(strSheetName)


    xlWSh.Range("A1").Select


    For Each fld In rst.Fields
        ApXL.ActiveCell = fld.Name
        ApXL.ActiveCell.Offset(0, 1).Select
    Next

    rst.MoveFirst
    xlWSh.Range("A2").CopyFromRecordset rst
    xlWSh.Range("1:1").Select
    ' This is included to show some of what you can do about formatting.  You can comment out or delete
    ' any of this that you don't want to use in your own export.
    With ApXL.Selection.Font
        .Name = "Arial"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
    End With
    ApXL.Selection.Font.Bold = True
    With ApXL.Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = False
    End With
    ' selects all of the cells
    ApXL.ActiveSheet.Cells.Select
    ' does the "autofit" for all columns
    ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
    ' selects the first cell to unselect all cells
    xlWSh.Range("A1").Select

    rst.Close
    Set rst = Nothing

    Exit Function
err_handler:
    DoCmd.SetWarnings True
    MsgBox Err.Description, vbExclamation, Err.Number
    Exit Function

End Function

Okay and then you call it like this:
Code:
Call SendTQ2ExcelSheet("qryReport_All", "RawData", "\\ushosfps01\Corporate QD\QD Only\QD Associates\Chance\VPReportTest.xlsm")
 

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
Bob,

I added the code for both in their proper places that you gave me. When I click the button to run the code I receive an error which says, "Too few parameters. Expected 2."

Could it have something to do with my actual query or what do you think would cause the error?

Here is the SQL for my query if needed. I appreciate your help on this. :)

Code:
PARAMETERS [Forms]![VPReport].[txtStartDate].[Value] DateTime, [Forms]![VPReport].[txtEndDate].[Value] DateTime;
TRANSFORM 100-((Sum([Answer])/Count(*)*-1*10000)/100) AS PR
SELECT qryCallCount.tblLocation.Description AS Location, qryCallCount.ManagerName, qryCallCount.SupervisorName, qryCallCount.EmployeeName, qryCallCount.tblCallType.Description AS CallType, qryCallCount.Calls
FROM (tblAssociate INNER JOIN (qryCallCount INNER JOIN tblCoaching ON qryCallCount.EmployeeID = tblCoaching.EmployeeID) ON tblAssociate.AssociateID = qryCallCount.EmployeeID) INNER JOIN (tblQuestion INNER JOIN tblQuestionAnswers ON tblQuestion.QuestionID = tblQuestionAnswers.QuestionID) ON tblCoaching.CoachingID = tblQuestionAnswers.CoachingID
WHERE (((tblCoaching.DateGraded) Between [Forms]![VPReport].[txtStartDate].[Value] And [Forms]![VPReport].[txtEndDate].[Value]))
GROUP BY qryCallCount.tblLocation.Description, qryCallCount.ManagerName, qryCallCount.SupervisorName, qryCallCount.EmployeeName, qryCallCount.tblCallType.Description, qryCallCount.Calls
ORDER BY qryCallCount.tblLocation.Description, qryCallCount.ManagerName, qryCallCount.SupervisorName
PIVOT tblQuestion.QuestionID;
 

boblarson

Smeghead
Local time
Today, 04:34
Joined
Jan 12, 2001
Messages
32,059
Ooh, pivots can be a pain.

First of all you should have the parameters in the underlying query that the piviot is based on. You then have to declare the parameters in both the underlying query and in the pivot.
 

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
In the underlying query I have placed the parameters and have removed them from the main crosstab query.

I have the parameters declared in the underlying query and in my crosstab query.

I still receive the same error as before. "Too few parameters. Expected 2."

Any thoughts?

You are right. These can be a pain.
 

boblarson

Smeghead
Local time
Today, 04:34
Joined
Jan 12, 2001
Messages
32,059
I'm not sure at the moment. I'd probably have to look at it directly to try to play with it. Can you upload a copy (using bogus data of course)?
 

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
I will get a copy for you and post it.
 

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
Bob,

Here is a copy of my database.

The queries you are working with are:
1. qryCallCount (underlying query)
2. qryReport_All (crosstab query)

The form used for the data will automatically load.

Attached an excel workbook named VPReportTest.xlsm. (The workbook is blank though.)
 

Attachments

  • VPReportTest.zip
    5.1 KB · Views: 125
  • QDC_Test.zip
    2 MB · Views: 139

boblarson

Smeghead
Local time
Today, 04:34
Joined
Jan 12, 2001
Messages
32,059
Not having much luck with this one. Will probably need to modify the query using querydefs in code to make it work.
 

Cbell5

Registered User.
Local time
Today, 04:34
Joined
Jun 24, 2010
Messages
11
Bob,

I appreciate your assitance on this one. Maybe I could bounce an idea off you to see if this would work better. I would rather not have to export any data to Excel, and just create a report in Access.

The managers of the call center that I work for need to know how employees are doing in each area of their calls. There are several different types of calls that associates take (e.g. Sales, Customer Service, etc...) Each call type has a different amount of questions so the query needs to be dynamic.

You'll notice that when you run qryReport_all the columns are numbers. These are id's for questions that we have that measure customer service to our customers, found in tblQuestion. The numbers generated are actually a percentage.

Can a report be created that will be dynamic and pull in the actual questions instead of the id's for the coumn headings?

If you open the form TeamListsSup you will be given a drop down box that allows you to select the supervisor. From there if you click on an associate in the listbox their scores populate to the right. By double clicking on a score to the right the EvaluationReport is run and you are able to see a summary of how the associate performed on each question for the call they took. I want each of those questions to populate in the column heading of a report for statistical data.

I know this is a big project, but any assitance from you would be greatly appreciated.

Thank you again.
 

Users who are viewing this thread

Top Bottom