+ Reply to Thread
Results 1 to 2 of 2

VBA not working when running on windows schedule

Hybrid View

  1. #1
    Registered User
    Join Date
    09-09-2014
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    2

    VBA not working when running on windows schedule

    Hi All,

    The following code runs fine when I open the excel file on my local drive (it auto runs on open). However when run on windows server via a remote desktop, the exact same code doesn't return any records or field headers. I am trying to run it manually on windows server just now before I set up a schedule

    ***when I run this code on my local drive the value of rs.fields.count is 30 which is correct as there are 30 fields being returned, however when I run it from windows server the value of rs.fields.count is 0, even though the code is exact same, it is also the same for rs2 which returns the actual records***

    Any help would be greatly appreciated

    I am using Excel 2013 on my local machine and Excel 2008 on windows server and SQL Server 2014. Also it may not be relevant but I am using Microsoft ActiveX Data Objects 2.8 Library

    Dim cnn As New ADODB.Connection
    Dim rs As New ADODB.Recordset, rs2 As New ADODB.Recordset
    Dim sql As String
    Dim sql2 As String
    
    ' Open empty recordset and pull through field headers
    sql = "Select Top 1 * from vwStockPivotTables where 1=2"
    
    ' Pulls through all data for the manager
    sql2 = "Select * from vwStockPivotTables where Manager = 'John Smith'
    
    cnn.Open "Provider=SQLNCLI11;Server=MyServerHere;Database=MyDBHere;User Id=UserNameHere;Password=PasswordHere;"
    
        ' define recordset properties for field headers
        With rs
        Set .ActiveConnection = cnn
            .Source = sql
            .Open
        End With
        
        ' define recordset properties for the data pull
        With rs2
        Set .ActiveConnection = cnn
            .Source = sql2
            .Open
        End With
    
        ' loops through to return each field header
        For h = 0 To rs.Fields.Count - 1
        Cells(1, h + 1) = rs.Fields(h).Name
        Next h
    
        Sheet3.Range("A2").CopyFromRecordset rs
        
        Sheet3.Range("A2").CopyFromRecordset rs2
    
        Set rs = Nothing
        Set rs2 = Nothing
        Set cnn = Nothing
    Last edited by Leith Ross; 12-08-2014 at 02:27 PM. Reason: Added Code Tags

  2. #2
    Registered User
    Join Date
    09-09-2014
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    2

    Re: VBA not working when running on windows schedule

    Hi all,

    I am bumping this up in the hope that someone is able to offer a solution to this.

    Thanks

+ 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. [SOLVED] Create Schedule Task in windows 7 to Open Excel file
    By Faridwahidi in forum Excel General
    Replies: 2
    Last Post: 08-17-2014, 09:02 PM
  2. VBA code Error after changed to windows 7, working fine in windows XP before
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-11-2014, 06:23 AM
  3. Macros created in Windows XP not opening /working in Windows 7
    By Janane in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2012, 04:28 AM
  4. open Excel with Macros active from windows schedule
    By bagullo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2011, 03:44 AM
  5. ocde working in one windows login but stopped working in another.
    By rama1209 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2011, 08:38 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