Solved

Access automation

Posted on 1999-01-07
6
401 Views
Last Modified: 2011-10-03
I have an Access application that consists of multiple tables/queries and reports.  

I would like to add a command button to my Access form that allows the user to enter the name of a query or report to be analyzed with Excel.  Upon entering the query or report name, Excel would be launched and the new Excel file opened for analysis.

Basically I want to automate the following steps into a command button:

1. Use the database window to select the table, query, form,    or report to save & load into MS Excel
2. Select the Tools menu, point to Office Links, and click
   "analyze it with MS Excel"

Does anybody have some VB code I could put in a command button to accomplish this?

Thanks!
0
Comment
Question by:scottL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
dovholuk earned 100 total points
ID: 1615476
This should work for you. If not let me know and i'll tweak it.  If i could have sent you an attachment it would have been easier to get the answer to you.  Just make a button with the 'onclick' property set to "=ExportToExcel()"
Function ExportToExcel()
On Error GoTo ErrorHandler
Dim exportname As String, i As Integer, a(5) As Integer
Dim t As TableDef, q As QueryDef, r As Report
i = 0
exportname = InputBox("Please enter the object name:", "Analyze with Excel")
If exportname = "" Then
    MsgBox "Nothing entered. Quitting"
    Exit Function
End If
Set t = CurrentDb.TableDefs(exportname)
i = i + 1
Set q = CurrentDb.QueryDefs(exportname)

If a(0) = 0 Then
    DoCmd.OpenTable exportname
    DoCmd.RunCommand acCmdOutputToExcel
    DoCmd.Close
    Exit Function
End If
If a(2) = 0 Then
    DoCmd.OpenQuery exportname
    DoCmd.RunCommand acCmdOutputToExcel
    DoCmd.Close
    Exit Function
End If
If i = 3 Then
    DoCmd.OpenReport exportname, acViewPreview
    DoCmd.RunCommand acCmdOutputToExcel
    DoCmd.Close acReport, exportname
End If
If i = 5 Then
    MsgBox "The name you entered was not found. Try again."
End If
Exit Function
ErrorHandler:
a(i) = 1
i = i + 1
Resume Next
End Function
0
 

Author Comment

by:scottL
ID: 1615477
This works great!  The only problem I'm having is that an invalid object entered still results in Excel being launched.  It doesn't seem to fall into the logic:

If i = 5 Then
 MsgBox "The name you entered was not found. Try again."
End If

What do you think?  Thanks for your help!
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 1615478
sorry about that. here you go.  edit the last few lines so they read like this instead.
this should work well

have a good one!

If i = 3 Then
    DoCmd.OpenReport exportname, acViewPreview
    If i = 4 Then
        MsgBox "The name you entered was not found. Try again."
        Exit Function
    End If
    DoCmd.RunCommand acCmdOutputToExcel
    DoCmd.Close acReport, exportname
End If
Exit Function
ErrorHandler:
a(i) = 1
i = i + 1
Resume Next
End Function
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:dovholuk
ID: 1615479
sorry about that. here you go.  edit the last few lines so they read like this instead.
this should work well

have a good one!

If i = 3 Then
    DoCmd.OpenReport exportname, acViewPreview
    If i = 4 Then
        MsgBox "The name you entered was not found. Try again."
        Exit Function
    End If
    DoCmd.RunCommand acCmdOutputToExcel
    DoCmd.Close acReport, exportname
End If
Exit Function
ErrorHandler:
a(i) = 1
i = i + 1
Resume Next
End Function
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 1615480
sorry about that. here you go.  edit the last few lines so they read like this instead.
this should work well

have a good one!

If i = 3 Then
    DoCmd.OpenReport exportname, acViewPreview
    If i = 4 Then
        MsgBox "The name you entered was not found. Try again."
        Exit Function
    End If
    DoCmd.RunCommand acCmdOutputToExcel
    DoCmd.Close acReport, exportname
End If
Exit Function
ErrorHandler:
a(i) = 1
i = i + 1
Resume Next
End Function
0
 

Author Comment

by:scottL
ID: 1615481
That did the trick, thanks very much for your help!!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question