?
Solved

Access automation

Posted on 1999-01-07
6
Medium Priority
?
402 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

765 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