Darrenc
Registered User.
- Local time
- Today, 06:31
- Joined
- Apr 30, 2004
- Messages
- 62
I can normally find all the answers to my questions by searching for them, unfortunately this little problem has me stumped.
I'm not even sure I’m going about this in the right way, which is why I’m posting here.
I'm creating a database, and the basic concept is I want to be able to create a temporary excel file and email this file to a designated user.
All the information I need is held in a table:
SupplierCode
SupplierEmail
SupplierOwner
etc.
The excel file is generated by a query, I only want to use 1 query for all the different suppliers in the table.
To do this I need to do is pass a stored variable strSuppCode to the query. Like I say, I have most of it working, apart from passing the variable to the query.
I've used querydef before in another database, without any problems, but on that occasion I was getting the value from a form.
Question is, what is the syntax to pass a variable into a query.
There are also other parameters in the query, which will be the same for each supplier, do I have to reference these too?
This is this bit of code I’ve been failing to get working.
The bit highlighted in red is where its all going wrong.
Any thoughts on this would be greatly appreciated.
I'm not even sure I’m going about this in the right way, which is why I’m posting here.
I'm creating a database, and the basic concept is I want to be able to create a temporary excel file and email this file to a designated user.
All the information I need is held in a table:
SupplierCode
SupplierEmail
SupplierOwner
etc.
The excel file is generated by a query, I only want to use 1 query for all the different suppliers in the table.
To do this I need to do is pass a stored variable strSuppCode to the query. Like I say, I have most of it working, apart from passing the variable to the query.
I've used querydef before in another database, without any problems, but on that occasion I was getting the value from a form.
Question is, what is the syntax to pass a variable into a query.
There are also other parameters in the query, which will be the same for each supplier, do I have to reference these too?
This is this bit of code I’ve been failing to get working.
Code:
Public Function ExportReportsExcelFormat(szTempbook, szQueryName, strEmailAddress, strSupplierName, strSuppCode)
Dim szFullTempPath As String
Dim rstq As Recordset
Dim dbs As Database
Dim QD1 As QueryDef
szFullTempPath = "T:\TempReports" & szTempbook
'On Error GoTo Err_ModifyExportedExcelFileFormats
Application.SetOption "Show Status Bar", True
vStatusBar = SysCmd(acSysCmdSetStatus, "Formatting export file... please wait.")
[COLOR="Red"] Set dbs = CurrentDb 'Set Current Database
Set QD1 = dbs.QueryDefs(szQueryName) 'Set QD1 to define the query parameter for the named query
With QD1
.Parameters([SupplierCode]) = strSuppCode
End With
Set rstq = QD1.OpenRecordset 'Set rst as the lines returned from the updated query
DoCmd.OutputTo acOutputQuery, szQueryName, acFormatXLS, szFullTempPath, False[/COLOR]
Dim xlApp As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Open(szFullTempPath).Sheets(1)
With xlApp
.Application.Sheets(szQueryName).Select
.Application.Cells.Select
.Application.Selection.ClearFormats
.Application.Rows("1:1").Select
.Application.Selection.Font.Bold = True
.Application.Cells.Select
.Application.Selection.RowHeight = 12.75
.Application.Selection.Columns.AutoFit
.Application.Range("A2").Select
.Application.ActiveWindow.FreezePanes = True
.Application.Range("A1").Select
'.Application.Selection.AutoFilter
.Columns(2).DateFormat = "dd-mmm-yy"
.Columns(7).DateFormat = "dd-mmm-yy"
.Application.ActiveWorkbook.Save
.Application.ActiveWorkbook.Close
.Quit
End With
Set xlApp = Nothing
Set xlSheet = Nothing
vStatusBar = SysCmd(acSysCmdClearStatus)
Call SendEMail(szTempbook, strEmailAddress, strSupplierName)
Kill szFullTempPath
Exit_ModifyExportedExcelFileFormats:
Exit Function
Err_ModifyExportedExcelFileFormats:
vStatusBar = SysCmd(acSysCmdClearStatus)
MsgBox Err.Number & " - " & Err.Description
Resume Exit_ModifyExportedExcelFileFormats
End Function
The bit highlighted in red is where its all going wrong.
Any thoughts on this would be greatly appreciated.