home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Export from MS Access to MS Excel and show exported data in MS Excelta

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1619
Discussions: 200,957, Posts: 2,379,495, Members: 246,327
Old September 16th, 2003, 05:16 PM   Digg it!   #1 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: New Hampshire, USA
Posts: 641
Export from MS Access to MS Excel and show exported data in MS Excelta

I have already got a function running from a command button on a subform that exports data to MS Excel. What I would like to do is have MS Excel open at the end of the command click so that it shows the data on screen and appears to be part of the same operation to the end user.

Here is my working code that exports query results to an MS Excel file:


Private Sub cmdExportInfluenzaPneumoniaReport_Click()
On Error GoTo Err_cmdExportInfluenzaPneumoniaReport_Click

Dim dtDateTime As Date
Dim strDateTime As String

' Returns current system time in the system-defined long time format.
dtDateTime = Date
strDateTime = Format(dtDateTime, "MMM d yyyy")
MsgBox strDateTime

'Export report to MS Excel
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryInfluenzaPneumoniaReport_7", "Influenza_Pneumonia_Report_" & strDateTime & ".xls"
DoCmd.Beep


Exit_cmdExportInfluenzaPneumoniaReport_Click:
Exit Sub

Err_cmdExportInfluenzaPneumoniaReport_Click:
MsgBox Err.Description
Resume Exit_cmdExportInfluenzaPneumoniaReport_Click

End SubExit Sub

Err_cmdInfluenzaReport_Click:
MsgBox Err.Description
Resume Exit_cmdInfluenzaReport_Click
End Sub




Alternately I was considering writing another line or so of code to try to open the MS Excel document from within MS Access, but didn't figure that one out either. This statement


DoCmd.RunCommand acCmdOutputToExcel


will export the contents of the main form (excluding the contents of containing the subform to MS Excel and leave the MS Excel open so that data may be viewed, but don't know how to do this and have it open a pre-existing MS Excel file.

Any thoughts would be appreciated.

Thanks in advance.
ctaylor is offline   Reply With Quote
Old September 19th, 2003, 02:01 AM     #2 (permalink)
Member
 
Join Date: Sep 2002
Posts: 364
Put this is your global declarations of the vba:

Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpszOp As String, _
ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal LpszDir As String, ByVal FsShowCmd As Long) _
As Long
Put this wherever you actually want to open the file:

Code:
ShellExecute src_hDC, "Open", "C:\SomeFolder\SomeFile.xls", "", "C:\", 1
More info/Source
Creosote is offline   Reply With Quote
Old September 26th, 2003, 11:45 AM     #3 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: New Hampshire, USA
Posts: 641
Once again, I could not get the code to compile when I got around to testing it. ('src_hDC' would not compile as it remained undefined.)

I did a little looking around and found something that does appear to work for me though by using the OUTPUTTABLE method in place of the TRANSFERSPREADSHEET method. The last argument of the OUTPUTTABLE method actually allows you to open the document. (The -1 argument opens the document once it has been created. A value of 0 would leave the document created, but unopened.) To get this to work I created a temporary extract table to store the values prior to outputting them.

'Returns current system time in the system-defined long time format.
dtDateTime = Date
strDateTime = Format(dtDateTime, "MMM_d_yyyy")
strExportFileName = "Influenza_Pneumonia_Report_" & strDateTime & ".xls"


'Export report to MS Excel
With DoCmd
.SetWarnings False
.OpenQuery "qryDeleteInfluenzaPneumoniaExtract"
.OpenQuery "qryAppendInfluenzaPneumoniaExtract"
.SetWarnings True
.OutputTo acOutputTable, "tblInfluenzaPneumoniaExtract", acFormatXLS, strExportFileName, -1
.Beep
End With

Last edited by ctaylor : September 26th, 2003 at 11:48 AM.
ctaylor is offline   Reply With Quote
Old September 26th, 2003, 04:56 PM     #4 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: New Hampshire, USA
Posts: 641
Creosote,

I'm still interested in resolving this ShellExecute concern. Do you have any ideas? Do you think there are any specific reference libraries I should have enabled for the shell23.dll library to be accessed?

My VBA module has the following libraries enabled:

Visual Basic for Applications
Microsoft Access 9.0 Object Library (MSACC9.OLB
OLE Automation (stdole2.tbl)
Microsoft ActiveX Data Objects 2.1 Library (msado21.tbl)
Microsoft Excel 9.0 Object Library (EXCEL9.OLB)


<EDIT>
Oops - I think I found it

Microsoft Shell Controls and Automation (SHELL32.DLL).

I will let you know if this works sometime next week.

CT
</EDIT>

Last edited by ctaylor : September 26th, 2003 at 05:03 PM.
ctaylor is offline   Reply With Quote
Old September 27th, 2003, 03:44 AM     #5 (permalink)
Member
 
Join Date: Sep 2002
Posts: 364
Yeah, I'm thinking its just VBA stuff, whereupon, you have to specifically reference things you normally wouldn't have to in VB.

Good luck
Creosote is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2947)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
Is the PSU I received dead? (12)
Print spooler problem (15)
windows vista security holes (11)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Recent Discussions
Open With ..... Win7 (1)
Kingston Bluetooth Dongle Driver (0)
webcam (0)
upgrade for hp a6101 (0)
windows vista security holes (11)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)


All times are GMT -4. The time now is 07:13 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28