Reports output in Excel

No need to apologise, life has a habit of throwing things at us when we least need them.... hope you are feeling well now?

I do still need to do this if you could when you get time :)

Thanks for getting back to me.
 
Hmm, feeling back to normal, well, normal for me… :D
[QueryField] Excel Sheet and Cell

[W1SatEnhTotal] Week 1 C8
[W1M-FEnhTotal] Week 1 D8
[W2SatEnhTotal] Week 2 C8
[W3SatEnhTotal] Week 2 D8

These are a few from the first query, with subsequent records to be put into the row underneath (so A9, C9 & D9 etc)

From the second query

[W1SatOTHrsGSA] Week 1 C33
[W2SatOTHrsGSA] Week 2 C33

Trying to put data from two different fields from what appears to be two different rows into the same cell is something I cannot do. Access does not have a way to identify Row, Column like Excel does. They need to be in the same row is that the case here? If not you need to be in order for me to combine the fields into one cell.
 
Sorry, I think I explained that badly

Data from field [W1SatEnhTotal] to go into cell C8 on sheet called Week 1
Data from field [W1M-FEnhTotal] to go into cell D8 on sheet called Week 1
Data from field [W2SatEnhTotal] to go into cell C8 on sheet called Week 2
Data from field [W2M-FEnhTotal] to go into cell D8 on sheet called Week 2

The above being for the first record returned by the first query; the next record to then go as follows:

Data from field [W1SatEnhTotal] to go into cell C9 on sheet called Week 1
Data from field [W1M-FEnhTotal] to go into cell D9 on sheet called Week 1
Data from field [W2SatEnhTotal] to go into cell C9 on sheet called Week 2
Data from field [W2M-FEnhTotal] to go into cell D9 on sheet called Week 2

I won't post the stuff from the other query until I know if Access can do the above?

Failing this, it is not beyond the realms that I have two sheets contained within the workbook with the first query exporting to one sheet, and the second query to the other and simply have the relevant cells linked up on the other five sheets that we currently use. As long as Access outputs to the same cells in these two 'import' sheets each time, it won't really matter how it puts it out, The only restriction may be the number of cells available left to right? Or have I got the wrong end of the stick?
 
Okay, so you have several Worksheets... okay, I got it now. I'm doing it now so give me a few minutes :D
 
Okay, test this...

Code:
 Function SendToExcel(strTQName As String, strSheetName As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
    
        Dim rst As DAO.Recordset
        Dim ApXL As Object
        Dim xlWBk As Object
        Dim xlWSh As Object
        Dim fld As DAO.Field
        Dim lngMaxRow As Long
        Dim lngMaxCol As Long
        Dim lngLastRow As Long
        Dim N As String
        Dim strPath As String
    
        Const xlCenter As Long = -4108
        Const xlBottom As Long = -4107
        Const xlUp As Long = -4162
        Const xlDown As Long = -4121
        
        On Error GoTo Err_Handler
    
            strPath = "E:\Payroll Test\Template\4WeeklyMasterTemplate.xls"
        
            Set rst = CurrentDb.OpenRecordset(strTQName)
            Set ApXL = CreateObject("Excel.Application")
        
        
            Set xlWBk = ApXL.Workbooks.Open(strPath)
            'Moved to end so Excel doesn't open before the report is finished rendering
            'ApXL.Visible = True
                
            Set xlWSh = xlWBk.Worksheets(strSheetName)
             
            rst.MoveFirst
            xlWSh.Range("C8").CopyFromRecordset rst
            xlWSh.Range("D8").CopyFromRecordset rst
            ' selects the first cell to unselect all cells
            xlWSh.Range("A1").Select
    
        rst.Close
        Set rst = Nothing
        'Remove prompt to save file
        ApXL.DisplayAlerts = False
             xlWBk.SaveAs "E:\Payroll Test\FourWeeksEnding" & Date & ".xlsx", 51
        ApXL.DisplayAlerts = True
        'Open after report is completes
        ApXL.Visible = True
        'ApXL.Quit
    
        Exit Function
Err_Handler:
        DoCmd.SetWarnings True
        MsgBox Err.Description, vbExclamation, Err.Number
        ApXL.Quit
        Set ApXL = Nothing
        Exit Function
 End Function
Use the above like this (behind a Command Button)...

Code:
 Call SendToExcel("4WeeklyPayrollControllersProcess", "Week 1")
 Call SendToExcel("4WeeklyGSAProcess", "Week 2")
 
I will give it a shot, hopefully over the weekend and let you know; I am going to have to solve an issue with the calculation of times first however..... going to have a bite to eat, and will then post in the forms section for help as I am sinking fast
 
Thanks - I reckon I will be back on track shortly, but I have to go back and rethink a couple of forms and reports completely first. I appreciate you sticking with me on this :)
 
Hmm, not moved looks like they are having site issues. I sent Juan a message, hopefully will be resolved soon.
 
Sorry for the delay in replying to this - a sudden heavy workload (emergency circumstances), followed by some considerable health problems really took the rug from under my feet. I am now almost back to normal, and will be having a go at this in the next week or so.

Thanks for bearing with me :)
 
Okay, test this...

Code:
 Function SendToExcel(strTQName As String, strSheetName As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
    
        Dim rst As DAO.Recordset
        Dim ApXL As Object
        Dim xlWBk As Object
        Dim xlWSh As Object
        Dim fld As DAO.Field
        Dim lngMaxRow As Long
        Dim lngMaxCol As Long
        Dim lngLastRow As Long
        Dim N As String
        Dim strPath As String
    
        Const xlCenter As Long = -4108
        Const xlBottom As Long = -4107
        Const xlUp As Long = -4162
        Const xlDown As Long = -4121
        
        On Error GoTo Err_Handler
    
            strPath = "E:\Payroll Test\Template\4WeeklyMasterTemplate.xls"
        
            Set rst = CurrentDb.OpenRecordset(strTQName)
            Set ApXL = CreateObject("Excel.Application")
        
        
            Set xlWBk = ApXL.Workbooks.Open(strPath)
            'Moved to end so Excel doesn't open before the report is finished rendering
            'ApXL.Visible = True
                
            Set xlWSh = xlWBk.Worksheets(strSheetName)
             
            rst.MoveFirst
            xlWSh.Range("C8").CopyFromRecordset rst
            xlWSh.Range("D8").CopyFromRecordset rst
            ' selects the first cell to unselect all cells
            xlWSh.Range("A1").Select
    
        rst.Close
        Set rst = Nothing
        'Remove prompt to save file
        ApXL.DisplayAlerts = False
             xlWBk.SaveAs "E:\Payroll Test\FourWeeksEnding" & Date & ".xlsx", 51
        ApXL.DisplayAlerts = True
        'Open after report is completes
        ApXL.Visible = True
        'ApXL.Quit
    
        Exit Function
Err_Handler:
        DoCmd.SetWarnings True
        MsgBox Err.Description, vbExclamation, Err.Number
        ApXL.Quit
        Set ApXL = Nothing
        Exit Function
 End Function
Use the above like this (behind a Command Button)...

Code:
 Call SendToExcel("4WeeklyPayrollControllersProcess", "Week 1")
 Call SendToExcel("4WeeklyGSAProcess", "Week 2")

OK, I am about to embark on this.... fingers crossed :D

Before I get cracking, can I clarify a couple of bits please - - The first line
Code:
 Function SendToExcel(strTQName As String, strSheetName As String)
Do I change strTQName and strSheetName here, or do I declare it further along the line?


Also, what line(s) are telling it which fields in the query are to be copied to which cells in the worksheet?
 
No, you do not change the first line. You put the entire thing in a Module. Then where I posted the two Call lines is where you replace the name of your Query or Table and Sheet name with the name of your Query/Table name and Sheet name.

The lines under rst.MoveFirst is where you start putting what you want copied and where.
 
OK, thanks :)

Module done, and the button to call the queries will be done in a couple of mins or so, however I am struggling a bit to get my head round what the code is doing to select the data and copy it to the right cell?

I assume that this is the relevant bit that needs expanding upon?
Code:
xlWSh.Range("C8").CopyFromRecordset rst
            xlWSh.Range("D8").CopyFromRecordset rst
            ' selects the first cell to unselect all cells
            xlWSh.Range("A1").Select

I have just pulled the query, and have 17 records that I want to move to the spreadsheet; I have attached a screenshot of a sample of what field is to go where - once I understand the code to direct the right field from the right record to the right sheet and right cell on the spreadsheet, I should be cooking with gas....

Thanks again for your ongoing patience :)
 
PMFJI, but I've read this thread with interest just because it is sending data to Excel and the method used to do that.

I see the logic of Gina's code, but as she has already mentioned Access is unable to put different fields in different cells as you are requiring. The data in the query is really just one Excel sheet, unless you are going to do some nifty VBA code (which you say you do not know?)
As far as I can see the code does the equivalent of a copy and paste from query to an starting cell? Correct me if I am wrong.

The way I would approach this in your situation is to have 'input' sheets where Access deposits the data. You then use Excel formula to populate sheets week1, week2 etc.

That way you will know where the data is located and be able to refer to it easily in Excel. So each query would deposit it's data into it's own input sheet (or in one sheet if you can be certain that the second query will not overwrite the data of the first query).

You could do it in Access by walking the Excel cells as you walk through each query record, but that would be a lot harder to debug and get right I believe.

I would be interested in the final solution though.?

HTH
 
I could easily have an input sheet for each query and then link the relevant cells on the other sheets to that input sheet if that is required - hopefully Gina will be able to confirm how to output each query to its own sheet within the workbook. If I can get it in that state, then it is pretty much job done.

That is what I like about this forum - everyone is willing to pitch in and give help and advice in a way that you learn whilst resolving your issue; I have learnt an incredible amount from the ladies and gents on here :)

PS - no need for forgiveness, jump in wherever you want, I am not one of those that thinks a thread is 'mine'; the more the merrier in my view :)
 
Well this line

Code:
Set xlWSh = xlWBk.Worksheets(strSheetName)

is selecting the correct sheet, which you are passing in via the call of the routine

Code:
Call SendToExcel("4WeeklyPayrollControllersProcess", [b]"Week 1"[/b])

So whether you call it "Week1", "Payroll","GSA" or whatever, is entirely up to you.

I'd prefer two sheets just in case the second query start point overwrites the first query data.

In your example you start at A33 for the GSA query. What happens if the Payroll query produces 36 rows?. it might not now, but can it in the future?. When creating processes like these I always try and think ahead. :D

If you are certain that it never can, then A33 would be fine, but if it ever does, 6 or 9 months down the line, it will likely take you a while to discover why it is no longer working (that is if it is noticed) and then what is required to fix it.

In that regard it is always worth adding plenty of comments to your code, so that it helps you or someone else further down the road.

HTH
 
OK - I am getting somewhere (I think)

I have put the code as supplied into an existing module (modUtilities), and then have the following code under a command button:

Code:
Dim PerDate
PerDate = Me.cboDateSelect.Column(1)
If IsNull(cboDateSelect) Then
MsgBox "Oops! You need to select a period commencing date to continue", vbOKOnly, "Oops!...."
End If
Me.txtPerDate.Value = PerDate
Call SendToExcel("ControllersDirectExportQuery", "Inputsheet1")


The textbox txtPerDate is an unbound box that gets populated by the date chosen from a combo box, and this is the criteria that applies to the query.

Now, when I run this, I get a message 'Too few parameters Expected1'

So, I am clearly missing something?

Inputsheet1 is a blank sheet in the workbook that I would like everything in the query to be pasted into - the next call can then do the same but paste into Inputsheet2 (again, blank), and I can then link the four relevant sheets to those two input sheets.

EDIT - am I right in assuming that I need to be changing whatever is written under rst.Moverfirst as noted above - if so, how do I basically select all and export it to the chosen input sheet?
 
Last edited:

Users who are viewing this thread

Back
Top Bottom