Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access automation

Posted on 1999-01-07
6
Medium Priority
?
404 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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.
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

610 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