简体   繁体   中英

How can i add parameter code to export a query from access 2013 to excel 2013

'Below is the current code that I have and it will export to the excel workbook and worksheet correctly. The only problem is that I need to limit the data that gets exported by a month end date range (example: 1/31/2017 to 4/30/2017) and also by a plant number (example: "4101") thanks for any help it is greatly appreciated. Public Function InventoryXport_4100()

Dim appXL As Object
Dim wb As Object
Dim wks As Object
Dim xlf As String
Dim rs As DAO.Recordset
Dim fld As Field
Dim intColCount As Integer

xlf = "Z:\COST ACCOUNTING INFO\Inventory Reports\MyFile.xlsx"

Set rs = CurrentDb.OpenRecordset("(QS)_Inventory") 
Set appXL = CreateObject("Excel.Application")
Set wb = appXL.Workbooks.Open(xlf)
Set wks = wb.Sheets("Inventory Xport") 'Sheet name

If rs.EOF = True Then
  MsgBox "No data", vbOKOnly
Exit Function
End If

With appXL
  .Application.worksheets("Inventory Xport").SELECT
  .Application.columns("A:AQ").SELECT
  .Application.columns.Clear
End With

intColCount = 1

For Each fld In rs.Fields
  wks.Cells(1, intColCount).Value = fld.Name
  intColCount = intColCount + 1
Next fld

appXL.displayalerts = False

wks.Range("A2").CopyFromRecordset rs

appXL.Visible = True

With appXL
  .Application.worksheets("Inventory Xport").SELECT
  .Application.columns("A:AQ").SELECT
  .Application.columns.AutoFit
  .Application.Range("A2").SELECT
  .Application.ActiveWindow.FreezePanes = True
End With

wb.Save
wb.Close
appXL.Quit

Set wb = Nothing
 rs.Close
Set rs = Nothing

End Function

You can use:

Dim Date1 As Date
Dim Date2 As Date
Dim PlantNr As String
Dim Sql As String

Date1 = #1/31/2017#
Date2 = #4/30/2017#
PlantNr = "4101"
Sql = "Select * From [(QS)_Inventory] Where YourDateField Between #" & Format(Date1, "yyyy\/mm\/dd") & "# And #" & Format(Date2, "yyyy\/mm\/dd") & "# And [Plant Number] = '" & PlantNr & "'"

Set rs = CurrentDb.OpenRecordset(Sql) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM