3

Our requirement -

We have few suppliers and we want to generate a monthly report of purchases from each supplier during the month and export to excel. I think the solution is to create a parameter query where I can pass the value of the supplier ID, Month and Year. The list of supplier keeps changing and is stored in a separate table. So, basically, I should be able to sequentially read the supplier ID from that table and pass as a parameter to my query to generate the report for that supplier.

The closest solutions that I found for my requirement was on (which are similar in nature) -

Exporting Recordset to Spreadsheet

http://answers.microsoft.com/en-us/office/forum/office_2010-customize/filtering-a-query-used-by-docmdtransferspreadsheet/06d8a16c-cece-4f03-89dc-89d240436693

Why I think there could be a better solution -

In the suggested solution, we are creating multiple queries and deleting these. Conceptually, I feel there should be a way to create a parameter query and use the do while loop to sequentially pass the value of the parameter (DeptName in above example) to the query and export the results to excel.

I should be able to achieve this if I can use vba to pass value to a parameter query. And that is what I have not yet been able to figure out.

Update on 24-Feb -

Following is the code that I have written -

Private Sub Monthly_Supplier_Sales_Report_Click()
Dim strDirectoryPath As String
Dim DateFolderName As String
DateFolderName = Format$((DateSerial(year(Date), month(Date), 1) - 1), "YYYY MM")
strDirectoryPath = "C:\dropbox\Accounting\Sales Reports\" & DateFolderName
If Dir(strDirectoryPath, vbDirectory) = "" Then MkDir strDirectoryPath

Dim Filename As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim DesignerCode As String
Dim month1 As String
Dim year1 As Integer
Dim Query1 As DAO.QueryDef
Dim query2 As DAO.QueryDef
Dim rsDesigner As DAO.Recordset

Set rsDesigner = CurrentDb.OpenRecordset("Designer Details Master")

Do While Not rsDesigner.EOF
DesignerCode = rsDesigner![Designer Code]
month1 = "Jan" 'right now hardcoded, will call this programatically
year1 = 2014   'right now hardcoded, will call this programatically

strSQL1 = "SELECT * FROM [Sales Report Generation Data] WHERE [designer code] = '" & DesignerCode & "' AND [Shipping Month]= '" & month1 & "' AND [Shipping Year]=" & year1

strSQL2 = "SELECT * FROM [Sales Report Generation - Monthwise Inventory Snapshot] WHERE [designer code] = '" & DesignerCode & "' AND [Snapshot Month]= '" & month1 & "' AND [Snapshot Year]= " & year1

Set Query1 = CurrentDb.CreateQueryDef(Name:="TempSalesQuery", SQLText:=strSQL1)
Set query2 = CurrentDb.CreateQueryDef(Name:="TempInventoryQuery", SQLText:=strSQL2)

Filename = strDirectoryPath & "\" & DesignerCode & Format$(Now(), " yyyy mm") & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TempSalesQuery", Filename, False, "Sales Report"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TempInventoryQuery", Filename, False, "Inventory"

CurrentDb.QueryDefs.Delete "TempSalesQuery"
CurrentDb.QueryDefs.Delete "TempInventoryQuery"

rsDesigner.MoveNext
Loop

End Sub

Instead, The logic I want to apply is -

Do While Not 
assign Value to Parameter 1 = rsDesigner![Designer Code]
assign Value to Parameter 2 = Month1
assign Value to Parameter 3 = Year1
Run the two Parameter queries, for which about three parameters are the input value and export to excel in respective sheets.
Loop

Just that I have not yet been able to figure out - how to achieve this.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Rohit Bansal
  • 31
  • 1
  • 1
  • 3
  • Not sure how far along you are or if you are comfortable with the steps outlined in the links you posted, but here is my suggestion. (1) create a simple form with (a) a listbox for 12 months; (b) listbox for years; (c) listbox/combobox for suppliers ** omit if you ALWAYS want to run report for ALL suppliers; (d) command button to submit report; (e) add VBA code in button click event to select values from listbox/combobox to create filter for recordset then open report passing the filter. If you are interested in this approach, I can add more details on how to accomplish. – Wayne G. Dunn Feb 23 '14 at 16:37
  • I have been able to write the code as per the solution given in the two links and get the desired result. But just that I feel there should be a better solution. Even in what you are suggesting, I am wondering why should we have to create the form to fetch the value, because my plan is to use this to run on every 4th / 5th of a month to fetch the reports for previous month for all suppliers. So, ideally, we should be able to define the value of Month, Year and supplier using vba code. – Rohit Bansal Feb 24 '14 at 14:01
  • Of course you can have it run automatically -- I just thought you may want the flexibility of running the reports anytime you wanted them -- and for a selected supplier if you only needed for that one. The query, as shown in the links you attached, do not need to be deleted -- and you can use a query to 'TransferSpreadsheet' (at least in Office 2010 and above). You should have your query accept parameters (or Functions) for date and supplier (unless you want all suppliers), then just set the parameters in VBA. – Wayne G. Dunn Feb 24 '14 at 14:20
  • Editing the question above - to include the code that I have used as per the two solution given. – Rohit Bansal Feb 24 '14 at 14:33

2 Answers2

1

Here is one solution. Note that I created Functions that the two queries will use. Just create your two queries and save them (see sample SQL below), add your code to pick dates and everything should be fine.

Option Compare Database
Option Explicit

Dim fvShipMonth     As String
Dim fvShipYear      As Integer
Dim fvDesignerCode  As String

Public Function fShipMonth() As String
    fShipMonth = fvShipMonth
End Function

Public Function fShipYear() As Integer
    fShipYear = fvShipYear
End Function

Public Function fDesignerCode() As String
    fDesignerCode = fvDesignerCode
End Function

Private Sub Monthly_Supplier_Sales_Report_Click()
Dim Filename    As String
Dim strSQL1     As String
Dim strSQL2     As String
Dim DesignerCode As String
Dim month1      As String
Dim year1       As Integer
Dim rsDesigner  As DAO.Recordset

'SAMPLE SQL
'SELECT * FROM [Sales Report Generation Data] " & _
'WHERE [designer code] = '" & fDesignerCode() & "' AND [Shipping Month]= '" & fShipMonth() & "' AND [Shipping Year]=" & fShipYear()

    fvShipMonth = "Jan"
    fvShipYear = 2014
    Set rsDesigner = CurrentDb.OpenRecordset("Designer Details Master")
    Do While Not rsDesigner.EOF
        fvDesignerCode = rsDesigner![Designer Code]
        Filename = strDirectoryPath & "\" & DesignerCode & Format$(Now(), " yyyy mm") & ".xls"
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "<your Query 1>", Filename, False, "Sales Report"
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "<your Query 2>", Filename, False, "Inventory"
        rsDesigner.MoveNext
    Loop
    rsDesigner.Close
    Set rsDesigner = Nothing
End Sub
Wayne G. Dunn
  • 4,282
  • 1
  • 12
  • 24
  • Hi Thanks for your solution. But it does not seem to be working for me. Actually, it took time for me to figure out the concept of public function etc. But the functions seem to be returning no value. I inserted a msgbox and verified that functions are returning blank/zero. What could be the reason? Am I doing something wrong? – Rohit Bansal Feb 25 '14 at 14:02
  • I don't know how you implemented my suggestion - did you copy ALL of the code? Where is your msgbox - inside the 'Sub Monthly_Supplier_Sales_Report_Click'? NOTE that I edited my code 22 hours ago because I forgot the () in the SQL after the function names. Also be careful since the variable names are similar to the function names!! Here is how to test USING THE ABOVE: (1) place the following msgbox just after you set fvShipMonth & fvShipYear: MsgBox "Mo: " & fvShipMonth & vbTab & "Yr: " & fvShipYear & vbTab & "fMo: " & fShipMonth() & vbTab & "fYr: " & fShipYear() – Wayne G. Dunn Feb 25 '14 at 14:34
  • 1. Copied the public function in module, 2. modified the parameter queries to set value of three parameters equal to fshipmonth() and so on, 3. copied pasted your code, 4. updated the names of queries in the transferspreadsheat method. This created the excel files, but all were empty. So, created a message box within the loop - MsgBox (fDesignerCode() & ", " & fShipMonth() & ", " & fShipYear()) this returned , ,0 – Rohit Bansal Feb 25 '14 at 15:00
  • I just ran your msgbox and got |, Jan, 2014| this means you have something set wrong. Please MANUALLY overide the values before the msgbox with the two lines: fvShipMonth = "Jan" and fvShipYear = 2014 Then see what your msgbox produces. – Wayne G. Dunn Feb 25 '14 at 15:13
  • Still No luck! I think there is something wrong with the way the function is set up, which I am not being able to figure out. But anyway, I like the solution you provided and I learnt a few new things. Thanks much! – Rohit Bansal Feb 26 '14 at 06:14
  • Give me a minute and I will provide another answer with code that you can test with to prove my original code should work. – Wayne G. Dunn Feb 26 '14 at 14:18
-1
  1. Open your database
  2. Create a NEW module
  3. Paste the following code
  4. Place your cursor inside Function TEST_MY_CODE()
  5. Press F5
  6. Paste the results from the immediate window in your response.

    Option Compare Database
    Option Explicit
    
    Dim fvShipMonth     As String
    Dim fvShipYear      As Integer
    Dim fvDesignerCode  As String
    
    Public Function fShipMonth() As String
        fShipMonth = fvShipMonth
    End Function
    
    Public Function fShipYear() As Integer
        fShipYear = fvShipYear
    End Function
    
    Public Function fDesignerCode() As String
        fDesignerCode = fvDesignerCode
    End Function
    
    Function TEST_MY_CODE()
        My_Click_EVENT      ' Test the code I provided
    End Function
    
    Private Sub My_Click_EVENT()
    Dim month1      As String
    Dim year1       As Integer
    
    fvShipMonth = "Jan"
    fvShipYear = 2014
    Debug.Print "**** START TEST ****"
    Debug.Print "fvShipMonth = " & fvShipMonth
    Debug.Print "fvShipYear = " & fvShipYear
    Debug.Print "fShipMonth() = " & fShipMonth()
    Debug.Print "fShipYear() = " & fShipYear()
    Debug.Print "**** END TEST ****"
    
    End Sub
    
Wayne G. Dunn
  • 4,282
  • 1
  • 12
  • 24