+ Reply to Thread
Results 1 to 3 of 3

VBA SQL fetching the old cache Data

  1. #1
    Registered User
    Join Date
    06-30-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    70

    Question VBA SQL fetching the old cache Data

    Hi All,

    I am using the SQL query in Excel and fetch the data from Sheet1 to Sheet2 and after Sheet2 to Sheet3 via one code and it is working fine in the Excel 2013 but when I am running this in Excel 2007, Then is transfer the data on Sheet1 to Sheet2 and it is not transfer the data from Sheet2 to Sheet3. If in Sheet2 has any data before updating the data from Sheet1 then Sheet3 is updated with the old Data of Sheet2. I have resolve the problem by Saving the Workbook after first query and then run the second query. But due to large data it is taking time, So is there any quick solution of that.

    Here is an example:
    I have 3 sheets in the Workbook: Sheet1, Sheet2, Sheet3. Now,I have created a dummy database in Sheet1 and write this Script at the backend:



    Sub SQL123()

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset

    strFile = ThisWorkbook.FullName
    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"

    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    cn.Open strCon

    strSQL = "SELECT * FROM [Sheet1$]"

    rs.Open strSQL, cn, 3, 3

    Sheet2.Range("A2").CopyFromRecordset rs
    For iCols = 0 To rs.Fields.Count - 1
    Worksheets("Sheet2").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
    Next
    cn.Close

    'ThisWorkbook.Save
    cn.Open strCon
    strSQL = "SELECT * FROM [Sheet2$]"

    rs.Open strSQL, cn, 3, 3
    Sheet3.Range("A2").CopyFromRecordset rs
    For iCols = 0 To rs.Fields.Count - 1
    Worksheets("Sheet3").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
    Next

    End Sub

    Here I want to eliminate "'ThisWorkbook.Save" line.


    Regards,
    Pawan

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA SQL fetching the old cache Data

    Hi,

    You need to wrap your code in code tags please, following the forum rules.

    It would also help if you explain why you are copying the same data from sheet to sheet using two separate queries.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    11-13-2009
    Location
    Cumbria
    MS-Off Ver
    Excel 2016
    Posts
    87

    Re: VBA SQL fetching the old cache Data

    Was there ever a resolution to this problem? I'm having the same problem on Excel 2013. Any help with this would be much appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Fetching Data from different workbooks
    By Mohan93 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2015, 10:07 AM
  2. [SOLVED] Macro to update pivot tables and use the SAME cache instead of repeatedly recreating cache
    By Fowzee1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2015, 11:26 PM
  3. Fetching Data from website
    By catchanil in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2014, 09:45 AM
  4. Data fetching
    By shivarajm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-23-2012, 12:32 PM
  5. Fetching data from another worksheet
    By baapji in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 01-07-2010, 05:29 PM
  6. Fetching data from another Worksheet
    By Jhedge in forum Excel General
    Replies: 2
    Last Post: 01-17-2007, 12:47 AM
  7. Fetching data from another WS
    By Jhedge in forum Excel General
    Replies: 4
    Last Post: 01-17-2007, 12:45 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1