Results 1 to 10 of 10

[SOLVED] SQL query interrupted by a for loop and closing/saving files

Threaded View

  1. #1
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    [SOLVED] SQL query interrupted by a for loop and closing/saving files

    Dear members,

    My queries simply do not wish to be executed when combined with a for loop or if a file is closed an saved after the query function (see the code below):

           ' Close files
            'Filename_FROM.Close False
            'Filename_TO.Close True
            'Choose_Structure_5.Hide
        TO_Sheet_CTO.Activate
        'For i = 2 To Application.CountA(Range("B:B"))
            Call CustomExpense
            
            'TO_Sheet_CTO.Range("S2") = TO_Sheet_CTO.Range("AF2")
            'TO_Sheet_CTO.Range("AF:AF").Delete
        'Next i
        
        End If
        
    End Sub
    
    Private Sub CustomExpense()
        
        Dim sqlstring As String
        
        
        Dim connstring As String
        Dim qt As QueryTable
    
        sqlstring = "select IXLCFP from IIX where IXITMN = '" & Cells(2, 2) & "'"
        connstring = _
                "ODBC;DSN=XY;UID=Y;PWD=atlas;Database=X"
                With ActiveSheet.QueryTables.Add(Connection:=connstring, _
                        Destination:=Range("AF1"), Sql:=sqlstring)
                    .Refresh
                End With
        
    End Sub
    I have tried puttig the query code in different areas of the original code and using the Wait method. Nothing works. I need to take item numbers, send them as a query and receive a corresponding price, which is saved into an Excel sheet. This needs to be done for 2 up to 100 (!) item numbers. Each item number is a seperate query; I have not combined them into a single query as you never know how many item numbers you have. It can be 2 one day, 50 tomorrow.

    In MATLAB I have successfully used 1000 queries in a for loop with zero issues. I don't understand why the queries (1) take so much time with VBA and (2) why they get interuppted by surrounding events. Any thoughts?
    Last edited by nErD; 01-10-2012 at 05:16 AM.

Thread Information

Users Browsing this Thread

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

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