Darrenc
07-27-2009, 08:59 AM
Hi, I'm having a problem with querydef!
I have looked at tried all sorts of different solutions and now i need your help.
I'm trying to export a query into excel, and this query has a parameter that looks at 2 date fields on a form.
Between Forms!frmSwictboard!frmDatabaseReports!txtStartDat e and Forms!frmSwictboard!frmDatabaseReports!txtEndDate
I cannot for the life of me pass through this parameter using QueryDef.
This is the code i'm using to create the excel report.
Dim qdf As QueryDef
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs("qryrptAllOverdueIncidents")
qdf.Parameters("IncDateRaised") = "Between Forms!frmSwictboard!frmDatabaseReports!txtStartDat e And Forms!frmSwictboard!frmDatabaseReports!txtEndDate"
Set rs = qdf.OpenRecordset
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With oSheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
db.Close
Is it even possible to pass through a parameter like this using querydef?
Can anyone point out any obvious flaw!
Thanks.
I have looked at tried all sorts of different solutions and now i need your help.
I'm trying to export a query into excel, and this query has a parameter that looks at 2 date fields on a form.
Between Forms!frmSwictboard!frmDatabaseReports!txtStartDat e and Forms!frmSwictboard!frmDatabaseReports!txtEndDate
I cannot for the life of me pass through this parameter using QueryDef.
This is the code i'm using to create the excel report.
Dim qdf As QueryDef
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs("qryrptAllOverdueIncidents")
qdf.Parameters("IncDateRaised") = "Between Forms!frmSwictboard!frmDatabaseReports!txtStartDat e And Forms!frmSwictboard!frmDatabaseReports!txtEndDate"
Set rs = qdf.OpenRecordset
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With oSheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
db.Close
Is it even possible to pass through a parameter like this using querydef?
Can anyone point out any obvious flaw!
Thanks.