473,432 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

Access export to excel BUG

157 100+
Hello!

I use this code to make a excel worksheet out of a qry
Expand|Select|Wrap|Line Numbers
  1.     MsgBox export_excel, vbInformation, "Finished"
  2.     Application.FollowHyperlink CurrentProject.Path & "\uttak\Månetlig uttak - " & Me.tbx_dato_ta_ut_varer_innen & ".xls"
  3.  
Expand|Select|Wrap|Line Numbers
  1. Public Function export_excel() As String
  2.  
  3. Dim appExcel As Excel.Application
  4.    Dim wbk As Excel.Workbook
  5.    Dim wks As Excel.Worksheet
  6.  
  7.    Dim sTemplate As String
  8.    Dim sTempFile As String
  9.    Dim sOutput As String
  10.  
  11.    Dim dbs As DAO.Database
  12.    Dim rst As DAO.Recordset
  13.    Dim sSQL As String
  14.    Dim lRecords As Long
  15.    Dim iRow As Integer
  16.    Dim iCol As Integer
  17.    Dim iFld As Integer
  18.  
  19.    Const cTabTwo As Byte = 2
  20.    Const cStartRow As Byte = 4
  21.    Const cStartColumn As Byte = 3
  22.  
  23.  
  24.    Application.SetOption "Error Trapping", 0
  25.  
  26.    sTemplate = CurrentProject.Path & "\templ\utløpstemplate.xls"
  27.    sOutput = CurrentProject.Path & "\uttak\Månetlig uttak - " & Me.tbx_dato_ta_ut_varer_innen & ".xls"
  28.    If Dir(sOutput) <> "" Then Kill sOutput
  29.  
  30.    FileCopy sTemplate, sOutput
  31.  
  32.  
  33.    Set appExcel = Excel.Application
  34.    Set wbk = appExcel.Workbooks.Open(sOutput)
  35.    Set wks = appExcel.Worksheets(cTabTwo)
  36.  
  37.    DoCmd.OpenQuery "IBD qry månetlig uttak excel", acViewPreview, acReadOnly
  38.    sSQL = "SELECT * From [IBD qry månetlig uttak excel]"
  39.    Set dbs = CurrentDb
  40.    Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
  41.    If Not rst.BOF Then rst.MoveFirst
  42.  
  43.    iCol = cStartColumn
  44.    iRow = cStartRow
  45.  
  46.  
  47.    Do Until rst.EOF
  48.       iFld = 0
  49.       lRecords = lRecords + 1
  50.  
  51.       For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
  52.          wks.Cells(iRow, iCol) = rst.Fields(iFld)
  53.  
  54.          If InStr(1, rst.Fields(iFld).Name, "Date") > 0 Then
  55.             wks.Cells(iRow, iCol).NumberFormat = "mm/dd/yyyy"
  56.          End If
  57.  
  58.          wks.Cells(iRow, iCol).WrapText = False
  59.          iFld = iFld + 1
  60.       Next
  61.  
  62.       wks.Rows(iRow).EntireRow.AutoFit
  63.       iRow = iRow + 1
  64.       rst.MoveNext
  65.    Loop
  66.  
  67.    export_excel = "Totalt " & lRecords & " rader ble overført."
  68.    DoCmd.Close acQuery, "IBD qry månetlig uttak excel"
  69.  
  70. exit_Here:
  71.    On Error Resume Next
  72.    Set wks = Nothing
  73.    Set wbk = Nothing
  74.    Set appExcel = Nothing
  75.    Set rst = Nothing
  76.    Set dbs = Nothing
  77.    DoCmd.Hourglass False
  78.  
  79. 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
Attached Images
File Type: jpg t2.jpg (18.0 KB, 469 views)
Sep 4 '07 #1
7 3284
FishVal
2,653 Expert 2GB
Try to instantiate Excel application with CreateObject.
Expand|Select|Wrap|Line Numbers
  1. Set appExcel = CreateObject("Excel.Application")
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. Set appExcel = Excel.Application
  2.  
Sep 4 '07 #2
MikeTheBike
639 Expert 512MB
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
Sep 4 '07 #3
MikeTheBike
639 Expert 512MB
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
Sep 4 '07 #4
FishVal
2,653 Expert 2GB
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
    Expand|Select|Wrap|Line Numbers
    1. Set appExcel = Nothing
    kills it to death
  • when Excel.Application was instantiated and workbook was opened
    Expand|Select|Wrap|Line Numbers
    1. appExcel.Quit
    2. Set appExcel = Nothing 
    sequence needed to let the process RIP
Sep 4 '07 #5
Jim Doherty
897 Expert 512MB
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
Sep 4 '07 #6
MrDeej
157 100+
Try to instantiate Excel application with CreateObject.
Expand|Select|Wrap|Line Numbers
  1. Set appExcel = CreateObject("Excel.Application")
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. Set appExcel = Excel.Application
  2.  


This did the trick. Thank you Fishval! :)
Sep 5 '07 #7
FishVal
2,653 Expert 2GB
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.
Sep 5 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
5
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...
4
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...
2
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...
8
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...
3
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...
1
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...
1
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...
1
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,...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...
0
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.