Solved

Access automation

Posted on 1999-01-07
6
395 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Outlook Free & Paid Tools
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now