Hello!
I use this code to make a excel worksheet out of a qry - MsgBox export_excel, vbInformation, "Finished"
-
Application.FollowHyperlink CurrentProject.Path & "\uttak\Månetlig uttak - " & Me.tbx_dato_ta_ut_varer_innen & ".xls"
-
- Public Function export_excel() As String
-
-
Dim appExcel As Excel.Application
-
Dim wbk As Excel.Workbook
-
Dim wks As Excel.Worksheet
-
-
Dim sTemplate As String
-
Dim sTempFile As String
-
Dim sOutput As String
-
-
Dim dbs As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim sSQL As String
-
Dim lRecords As Long
-
Dim iRow As Integer
-
Dim iCol As Integer
-
Dim iFld As Integer
-
-
Const cTabTwo As Byte = 2
-
Const cStartRow As Byte = 4
-
Const cStartColumn As Byte = 3
-
-
-
Application.SetOption "Error Trapping", 0
-
-
sTemplate = CurrentProject.Path & "\templ\utløpstemplate.xls"
-
sOutput = CurrentProject.Path & "\uttak\Månetlig uttak - " & Me.tbx_dato_ta_ut_varer_innen & ".xls"
-
If Dir(sOutput) <> "" Then Kill sOutput
-
-
FileCopy sTemplate, sOutput
-
-
-
Set appExcel = Excel.Application
-
Set wbk = appExcel.Workbooks.Open(sOutput)
-
Set wks = appExcel.Worksheets(cTabTwo)
-
-
DoCmd.OpenQuery "IBD qry månetlig uttak excel", acViewPreview, acReadOnly
-
sSQL = "SELECT * From [IBD qry månetlig uttak excel]"
-
Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
-
If Not rst.BOF Then rst.MoveFirst
-
-
iCol = cStartColumn
-
iRow = cStartRow
-
-
-
Do Until rst.EOF
-
iFld = 0
-
lRecords = lRecords + 1
-
-
For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
-
wks.Cells(iRow, iCol) = rst.Fields(iFld)
-
-
If InStr(1, rst.Fields(iFld).Name, "Date") > 0 Then
-
wks.Cells(iRow, iCol).NumberFormat = "mm/dd/yyyy"
-
End If
-
-
wks.Cells(iRow, iCol).WrapText = False
-
iFld = iFld + 1
-
Next
-
-
wks.Rows(iRow).EntireRow.AutoFit
-
iRow = iRow + 1
-
rst.MoveNext
-
Loop
-
-
export_excel = "Totalt " & lRecords & " rader ble overført."
-
DoCmd.Close acQuery, "IBD qry månetlig uttak excel"
-
-
exit_Here:
-
On Error Resume Next
-
Set wks = Nothing
-
Set wbk = Nothing
-
Set appExcel = Nothing
-
Set rst = Nothing
-
Set dbs = Nothing
-
DoCmd.Hourglass False
-
-
End Function
This code works magnificent if i Run it once. The second time i rune it excel hangs like attached
The solution for this is to go inn in Task Manager and kill the excel.exe application before the second time i run the code. Then i get a error code inn access: "The Remote Server Machine doen not exist or is unavailiable". I press end and then the third time i run the code it works just as good as first time.
Any teories on how to make this code work perfect?
Btw: The code is not entirely home designed. I have lent some parts from other sources
7 3284
Try to instantiate Excel application with CreateObject. -
Set appExcel = CreateObject("Excel.Application")
-
instead of -
Set appExcel = Excel.Application
-
Hi
I am not sure about
Application.FollowHyperlink (never used it in Access, but it doesn't seem to work for me!)
but in stead try inserting this
appExcel.Visible = True
before 'exit_Here:'
and see if this helps
MTB
Hi FishVal
Set appExcel = CreateObject("Excel.Application")
Yes, this is how have always instantiated an instance of Excel, but the posted method works if a reference is set to Excel. However,with either method, the code does not Quit the excel app, therefore it is still active/open, but not visible, which could be causing problems the second time around (at least I think it might!).
??
MTB
Hi FishVal
Set appExcel = CreateObject("Excel.Application")
Yes, this is how have always instantiated an instance of Excel, but the posted method works if a reference is set to Excel. However,with either method, the code does not Quit the excel app, therefore it is still active/open, but not visible, which could be causing problems the second time around (at least I think it might!).
??
MTB
Hi, MTB.
Yes your are right. Particularly this may cause problems this case.
Actually invoking Quit method doesn't cause immediate process termination. - when Excel.Application was instantiated but no workbook was opened
kills it to death
- when Excel.Application was instantiated and workbook was opened
-
appExcel.Quit
-
Set appExcel = Nothing
sequence needed to let the process RIP
off topic slightly... the other guys have answered merely an observation...looking at the screenshot version of excel there you would benefit from sending the RS out using the .CopyFromRecordset method its fast and you could format the spreadsheet once the RS is applied to the sheet?
Jim
Try to instantiate Excel application with CreateObject. -
Set appExcel = CreateObject("Excel.Application")
-
instead of -
Set appExcel = Excel.Application
-
This did the trick. Thank you Fishval! :)
This did the trick. Thank you Fishval! :)
Not a problem.
But be sure to kill excel process when no more needed.
See posts #4, #5.
And take a look at Jim's "invisible" post #6. Using WorkSheet.Range.CopyFromRecordset method will increase perfomance enormously.
Good luck.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sridevi |
last post by:
Hello
How to export data from ms-access database to excel worksheet using ASP.
mainly i need to export data to multiple worksheets. it is very urgent to
us.
i have a sample code which works...
|
by: jsudo |
last post by:
I have a Access database with a large amount of records (close to
500,000) that I would like to export to Excel. I found out that Excel
has the capability of of about 65,000 rows so I know I...
|
by: Gary Wright |
last post by:
I have an Access 2K database split into front and back. Quite often the
users want to do some data analysis that I have not created a report for so
they want to export some subset of the data into...
|
by: cjay85 |
last post by:
I wish to direct reports that I have created in Access to Excel
enabling me to produce a graphical output from Excel.
The report is a simple query regarding responses to a letter mailing.
There...
|
by: Jerome Ranch |
last post by:
Okay
So I've got pivot tables setup in Access 2003.
Only about 30K records in the current 2005 databases...the pivots
summarize the info in a number of nice ways.
I need to get the pivot tables...
|
by: excyauseme |
last post by:
Hi guys! Do you know what is the best way to export a text file, this
one is a log file that is already comma delimited thru a module run by
my access database, to an excel spreadsheet? I need to...
|
by: smaczylo |
last post by:
Hello, I've recently been asked to work with Microsoft Access, and
while I feel quite comfortable with Excel, I'm at a complete loss with
databases. If someone could help me with this issue I'm...
|
by: CoolFactor |
last post by:
MY CODE IS NEAR THE BOTTOM
I want to export this Access query into Excel using a command button on an Access form in the following way I describe below.
Below you will find the simple query I am...
|
by: Pauline |
last post by:
Dear all,
I have an enormous database (Access 2003) containing sales information, and an Excel tool to enable end users to do planning and forecasting. Untill now I would create several queries,...
|
by: Simon |
last post by:
Dear reader,
How to export a hyperlink field from Access to Excel, so it is also working
in Excel.
In Excel you have for the Hyperlink to parts:
a.. Text To Display
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |