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
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