Exporting an access form based on a query to Excel (1 Viewer)

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
I have a form which contains filtered information from a query. My form shows 157 records. I have placed a button on the form which calls a function to export the form data to a macro enabled workbook. The export takes place and poulates the specified worksheet with 157 records. However, the data is not as expected.

The export appears to take the first 148 rows of the form and export them OK. It then duplicates the first few rows to make up the 157 rows of the form.

My export function is:-
Code:
Public Function SendToSheet(frm As Form, strSheetName As String, strFilePath As String)
' frm is the name of the form used to query table
' strSheetName is the name of the sheet to copy data to in the XL workbook
' strFilePath is the spreadsheet to use
 
    Dim rst As DAO.Recordset
    Dim ApXL As Object
    Dim xlWBk As Object
    Dim xlWSh As Object
    Dim fld As DAO.Field
    Dim MacNm As String
    Dim ClearWSht As String
    Dim strPath As String
    Const xlCenter As Long = -4108
    Const xlBottom As Long = -4107
 
    MacNm = "Macro2"
    ClearWSht = "Macro1"
    On Error GoTo err_handler
 
 
    strPath = strFilePath
 
 
    Set rst = frm.RecordsetClone
 
 
    Set ApXL = CreateObject("Excel.Application")
 
 
    Set xlWBk = ApXL.Workbooks.Open(strPath)
 
 
    ApXL.Visible = True
 
    Set xlWSh = xlWBk.Worksheets(strSheetName)
 
     xlWSh.Activate
     xlWSh.Range("A1").Select
 
    For Each fld In rst.Fields
        ApXL.ActiveCell = fld.Name
        ApXL.ActiveCell.Offset(0, 1).Select
    Next
 
    rst.MoveFirst
    xlWSh.Range("A1").CopyFromRecordset rst
 
    xlWSh.Range("1:1").Select
 
 
 
    ' Formatting
    With ApXL.Selection.Font
        .Name = "Arial"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
    End With
 
    ApXL.Selection.Font.Bold = True
 
    With ApXL.Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = False
    End With
 
    ' Run Macro of Worksheet to produce graphs data
    xlWBk.Application.Run "'" & strPath & "'!'" & MacNm & "'"
 
    ' selects all of the cells
    ApXL.ActiveSheet.Cells.Select
 
    ' does the "autofit" for all columns
    ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
 
    ' selects the first cell to unselect all cells
    xlWSh.Activate
    xlWSh.Range("A1").Select
 
    rst.Close
    Set rst = Nothing
    'Run Macro to Clear the import sheet (prevent any error when function is re-used)
     xlWBk.Application.Run "'" & strPath & "'!'" & ClearWSht & "'"
 
Exit_SendToSheet:
    Exit Function
 
err_handler:
    DoCmd.SetWarnings True
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_SendToSheet
End Function

I was quite happy with this at first but when I tried to reconcile spreadsheet with form data I noticed the problem with the data.

I am not an expert in VBA and am obviously doing something wrong but I cannot fathom out the logic of why it is only copying 148 rows of my form and then duplicates the first few to make up the difference?

I have posted a similar thread but have not received any answer.

Could anyone please advise me
 

MStef

Registered User.
Local time
Today, 14:27
Joined
Oct 28, 2004
Messages
2,251
Try to do this via "OutputTo" command, but not form but query.
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
Thanks for this but I am unsure of how to go about this. I need to show the form results to the user (results are based upon a seleted combo box and fiters accordingly via a query using Forms!Form!ComboBox).

If I am to use the output to syntax how do I need to open the query first? and how can I export to an xlsm format speadsheet?

Presumably, I dont need to use my function for the output to call?
 

boblarson

Smeghead
Local time
Today, 06:27
Joined
Jan 12, 2001
Messages
32,059
Without seeing the data itself I'd be hard-pressed to answer. Is there any chance you can upload a copy of the database so that we can take a look? (if you do, make sure to not have real data, run compact and repair first, and then zip the file)
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
Ok Bob

I have attached a copy of the database (cut down version) and a copy of the spreadsheet.

I appreaciate you looking at this
 

Attachments

  • Sqry.zip
    134.2 KB · Views: 125
  • ESRS_Copy.zip
    86.6 KB · Views: 104
Last edited:

boblarson

Smeghead
Local time
Today, 06:27
Joined
Jan 12, 2001
Messages
32,059
I'll have to look at it when I get home in about 9 hours. I just have Office 2003 here at work.

If anyone else can get to it before me, feel free.
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
Thanks a lot Bob. The database contains fictitious data as this is purely an academic project I have been set at Uni.

I will await your reply eagerly.
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
Ok Bob

I have attached a copy of the database (cut down version) and a copy of the spreadsheet.

I appreaciate you looking at this

Bob,
As an aid to looking. Your function is located in the Functions Module and it is called by the Button on the frmseizure_event Form. The spreadsheet is included but you will need to change the path in the event sub.

From the Access Menu choose the show siezures and pick Camille Hindle from the combo box (this has the 157 records). If you use the export button excel will open at a menu page but if you find the 'Table Data' tab you will see the exported data which has been copied from the Import Tab. You will see that 147 rows have been copied from the form and then it starts to duplicate from the top until it has reached 157 records.
 

Beetle

Duly Registered Boozer
Local time
Today, 07:27
Joined
Apr 30, 2011
Messages
1,808
I played around with this for a bit. When the data is transferred to a new (or existing) blank spreadsheet the problem does not occur. It only seems to occur when writing the data to your Sqry spreadsheet. I didn't have time to really look through your xlsm file to see if I could tell what the issue is but that would seem to be where to look.
 

boblarson

Smeghead
Local time
Today, 06:27
Joined
Jan 12, 2001
Messages
32,059
Okay, a couple of things.

1. delete your Import spreadsheet and add it back in again as it seems to not work correctly unless you do that (only have to do it manually once). It must have some leftover problems from your testing.

2. Replace Macro 2's code with this:
Code:
    Sheets("Table Data").Activate
    Sheets("Table Data").Cells.ClearContents

    Sheets("Import").Activate
    Sheets("Import").Cells.Select
    Sheets("Import").Cells.Copy

    Sheets("Table Data").Activate
    Sheets("Table Data").Cells.Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                           False, Transpose:=False
    '
If you only want columns B and on, you will need to do that slightly differently. But I wasn't sure exactly what you needed. Your original macro code was a little confusing as it had what appeared to be arbitrary ranges set.

3. Next, in your Access module you can change to this (I highlighted in red what was modified):
Code:
Public Function SendToSheet(frm As Form, strSheetName As String, strFilePath As String)
' frm is the name of the form used to query table
' strSheetName is the name of the sheet to copy data to in the XL workbook
' strFilePath is the spreadsheet to use


    Dim rst As DAO.Recordset
    Dim ApXL As Object
    Dim xlWBk As Object
    Dim xlWSh As Object
    Dim fld As DAO.Field
    Dim MacNm As String
    Dim ClearWSht As String

    Dim strPath As String

    Const xlCenter As Long = -4108
    Const xlBottom As Long = -4107
    
    MacNm = "Macro2"
    ClearWSht = "Macro1"

    On Error GoTo err_handler




    strPath = strFilePath




    Set rst = frm.RecordsetClone




    Set ApXL = CreateObject("Excel.Application")




    Set xlWBk = ApXL.Workbooks.Open(strPath)
    



    ApXL.Visible = True



    Set xlWSh = xlWBk.Worksheets(strSheetName)
   

     xlWSh.Activate
     xlWSh.Range("A1").Select



    For Each fld In rst.Fields
        ApXL.ActiveCell = fld.Name
        ApXL.ActiveCell.Offset(0, 1).Select
    Next


    rst.MoveFirst

    xlWSh.Range("A2").CopyFromRecordset rst
    

    xlWSh.Range("1:1").Select
    
   
    
    ' Formatting
    With ApXL.Selection.Font
        .Name = "Arial"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
    End With


    ApXL.Selection.Font.Bold = True


    With ApXL.Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = False
    End With
    
'    ' Run Macro of Worksheet to produce graphs data
[B][COLOR="red"]    ApXL.Run MacNm[/COLOR][/B]
    
    ' selects all of the cells
    ApXL.ActiveSheet.Cells.Select


    ' does the "autofit" for all columns
    ApXL.ActiveSheet.Cells.EntireColumn.AutoFit


    ' selects the first cell to unselect all cells
    xlWSh.Activate
    xlWSh.Range("A1").Select
    

    rst.Close
    Set rst = Nothing
    
    'Run Macro to Clear the import sheet (prevent any error when function is re-used)
[B][COLOR="Red"]    ApXL.Run ClearWSht[/COLOR][/B]


Exit_SendToSheet:
    Exit Function
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
Hi Bob,

Many thanks for looking at this. Very strange isnt it.

The reason I have an Import Tab on the workbook is to have a page to export the form to. I then transfer the data to a Table Data tab where I have the formulas for counting etc. I then clear the import tab so that the export can cater for any number of records. If the next set only has say 12 records, the old ones from the last export would remain.

I cannot change macro 2 to clear Table Data because I have formulas within that page (to the right of the data. However, I necessary I will revamp the calculations and place then on another tab.

I will now go away and carry out your recomendations. I will let you know the results.

Do we have anything to learn from this or is it just a strange anomaly?
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
Hi Bob,
Have carried out the recommended fixes and the function now works fine. I must admit, I would never have thought of deleting the Import Tab on the spreadsheet. I new it was some problem with the export but, given that the Import tab was cleared before export I would not have thaought it was an excel problem. However, I have done what you suggested and it works a dream........many many thanks.

Just one question, There is one cell A114 that for some reason wants to set the formatting to Bold, align left, and different font type & size. Just this one cell? It it of no consequence but would like to get an understanding of why! have you any thoughts on this?
 

boblarson

Smeghead
Local time
Today, 06:27
Joined
Jan 12, 2001
Messages
32,059
Just one question, There is one cell A114 that for some reason wants to set the formatting to Bold, align left, and different font type & size. Just this one cell? It it of no consequence but would like to get an understanding of why! have you any thoughts on this?
Not sure why it is doing that. It was doing that to me too until I made it paste all (including formats). But since you can't do that, you can probably just reformat that page from Macro2.
 

cyd44

Registered User.
Local time
Today, 14:27
Joined
Oct 30, 2011
Messages
85
I can live with it as it is Bob, I was just interested as to why it was occuring. I only need the data to do my calculations for the graphs. In hindsight I would have been better putting the functions on a different tab, but I was using quite complex formulas and didnt want the added problen of getting data from another tab.

Once again, many thanks for the help
 

Users who are viewing this thread

Top Bottom