Results 1 to 6 of 6

Thread: Export data from Access to Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    The Netherlands
    Posts
    34

    Export data from Access to Excel

    I am exporting data from Access to Excel with the following code:

    Code:
      Dim xlApp As Excel.Application
      Dim xlBook As Excel.Workbook   'Excel werkboek object
    
      If Not IsExcelRunning (FUNCTION Checking if excel is open) Then
        Set xlApp = CreateObject("Excel.Application")
      Else
        Set xlApp = GetObject(, "Excel.Application")
      End If
    
      xlApp.DisplayAlerts = False
      Set xlBook = xlApp.Workbooks.Add()
      xlBook.Sheets.Add
    
      <DATA EXPORT LINES>
      
      xlApp.DisplayAlerts = True
      xlApp.Visible = True
      Set xlBook = Nothing
      Set xlApp = Nothing
    Now when i execute this code it works fine till i execute it a second time after closing Excel. The first time Excel is show with data, the second time Excel opens also, but no data is shown. When i close Access and reopen it it works again one single time.

    I already discovered that this happens ONLY when i use certain functions like Range. When i dont use them i dont have this problem.

    By the way i am using Excel 2000 with VBA 6.0

    Anyone know how to solve this? Did i forgot something?

  2. #2

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    The Netherlands
    Posts
    34
    Apparently when adding xlapp.activesheet before the to be executed functions is necessary. If u dont the problem as i described can happen. Only question which remains is why?

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    If you are using activesheet or selection then I guess you ar enot coding to the correct objects... this can be tested by opening excel and trying to update a different excel sheet whilst your program runs...

    If you create a variable to hold worksheets then as you add you set that (see following) then you have a pointer to that sheet and you shouldn't need activesheet method.
    VB Code:
    1. dim sht as worksheet
    2.  
    3.     set sht = wrkBk.sheets.add
    Assumes that wrkBk is the work book set in a similar way after you have the excel object.

    Possibly the excel app you opened is not defaultly pointing to a sheet (being invisible) and therefore it needed to set before you ran anything?


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    The Netherlands
    Posts
    34
    It did work so thanks for that

    But it is still strange that some procedures/functions (cell(y,x) = <value>) do work and some others (range(cell 1:cell2)) cause the problem i described. So why did some procedures/functions work and some not?

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Originally posted by irenicus
    It did work so thanks for that

    But it is still strange that some procedures/functions (cell(y,x) = <value>) do work and some others (range(cell 1:cell2)) cause the problem i described. So why did some procedures/functions work and some not?
    Apart from Excel being a bit crap like all ms office apps (incl. Access) we still put up with their eccentricities

    range(cell 1:cell2)
    range should accept this, but only if cell1:cell2 is in a text string.
    sometimes plays up, though I've noticed. Cell(x,y) is the easier and more programmatically useful function, as it doesn't depend on whether excel is in a1 or r1c1 mode.

    Sometimes range("cell1:cell2") needs a .select after it... weird but there you go.

    As long as its working (knock on wood) leave it be ... hehehe


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2004
    Location
    The Netherlands
    Posts
    34
    Originally posted by Ecniv


    Sometimes range("cell1:cell2") needs a .select after it... weird but there you go.

    As long as its working (knock on wood) leave it be ... hehehe

    Vince
    Ok thanks, and will do

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width