Idealsteve
New member
- Local time
- Today, 02:25
- Joined
- Aug 18, 2015
- Messages
- 7
am pretty new to visual basic and I have created some code which exports a load of queries to excel based on query names I have stored in a table, however I am encountering problems with a few of the queries as they run based on a value I have put in a form as it creates an error as it says I have no parameters.
Can anyone help
This is the code I am using
Thanks
Steve
Can anyone help
This is the code I am using
Code:
Option Compare Database
Function cmdExportQueries()
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("qryQueryList")
Dim oXLApp As Excel.Application 'Declare the object variables
Dim oXLBook As Excel.Workbook
Dim oXLSheet As Excel.Worksheet
Set oXLApp = New Excel.Application 'Create a new instance of Excel
Set oXLBook = oXLApp.Workbooks.Add 'Create new workbook
oXLApp.DisplayAlerts = False
oXLBook.Worksheets(3).Delete
oXLBook.Worksheets(2).Delete
Dim qryNumber As Integer
qryNumber = 1
Do Until rst.EOF
Dim rstQry As DAO.Recordset
Dim qrySQL As String
Dim QryName As String
QryName = rst!QueryName
Dim FrmName As String
FrmName = rst!FormName
qrySQL = "SELECT * FROM [" & QryName & "]"
Set rstQry = CurrentDb.OpenRecordset(qrySQL)
Set oXLSheet = oXLBook.Worksheets(qryNumber)
oXLSheet.Range("A2").CopyFromRecordset rstQry
For iCols = 0 To rstQry.Fields.Count - 1
oXLSheet.Cells(1, iCols + 1).Value = rstQry.Fields(iCols).name
oXLSheet.Cells(1, iCols + 1).Font.Bold = True
Next
oXLSheet.Cells(1, rstQry.Fields.Count).AutoFilter
oXLSheet.name = Left(QryName, 30)
oXLBook.Sheets.Add After:=oXLBook.Sheets(qryNumber)
qryNumber = qryNumber + 1
rstQry.Close
rst.MoveNext
Loop
oXLApp.DisplayAlerts = True
oXLBook.Worksheets(qryNumber).Delete
Set oXLSheet = oXLBook.Worksheets(1)
oXLBook.Sheets(1).Activate
Dim FilePath As String
FilePath = "F:\LSF Querys\"
Dim TheFileName As String
TheFileName = "Bacs_1516_" & Year(Now()) & "_" & Month(Now()) & "_" & day(Now()) & ".xlsx"
oXLBook.SaveAs FilePath & TheFileName
oXLBook.Close
Set oXLBook = Nothing
Set oXLApp = Nothing
rst.Close
Set rst = Nothing
End Function
Steve