+ Reply to Thread
Results 1 to 5 of 5

Connecting with SQL through VBA

  1. #1
    Registered User
    Join Date
    08-06-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    8

    Connecting with SQL through VBA

    Hi. I am having two main issues. Any help with these would be greatly appreciated.

    I have a stored SQL procedure saved in the variable SqlTextFile. The code below is meant to call the SQL server (defined in myConStr), run the stored query, and then copy the results into my excel worksheet.

    This code works fine when I run a query that is quick and outputs only 1 table. I am having two issues.

    1). When I run this on SQL code that takes perhaps 2 minutes to run, I get a timeout error after 30 seconds. I believe I am seeting the timeout to longer than this, but clearly I am not. Any ideas on why this is?

    2). The copying from the recordset at the end will only copy the first table that is output by the SQL query. How do I copy more than 1 table?

    objMyConn.ConnectionString = myConStr
    objMyConn.CommandTimeout = 90000
    objMyConn.ConnectionTimeout = 90000
    objMyConn.Open
    Dim hFile As Long
    hFile = FreeFile
    Open SqlTextFile For Input As #hFile
    sqlstatement = Input$(LOF(hFile), hFile)
    'Set and Excecute SQL Command'
    Set objMyCmd.ActiveConnection = objMyConn
    objMyCmd.CommandText = sqlstatement
    objMyCmd.CommandType = adCmdText
    ' 'Open Recordset'
    Set objMyRecordSet = objMyCmd.Execute
    ActiveSheet.Cells(3, 1).CopyFromRecordset (objMyRecordSet)
    Close #hFile


    Thank you very much for taking the time and helping.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Connecting with SQL through VBA

    The code actually setting the timeout to 90000 seconds which might explain why you get a timeout at 30 seconds as that's the default.

    If you want to get the multiple datasets returned by the stored procedure I think you need to loop through the recordsets returned.

    I've only done that a few times and I think I used NextRecordset for that.

    PS Can you add code tags?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    08-06-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Connecting with SQL through VBA

    Hi. Thank you for your response.

    1). I agree that the code setting the timeout to 90000 seconds is not working. Any idea how to fix it?

    2). I am not at all familiar with looping through recordsets. Would you be able to post some example code? What is the syntax with dealing with multiple recordsets?

    I am sorry but I don't know what code tags are. I would be happy to add them if you could explain.

    Thanks again.

  4. #4
    Registered User
    Join Date
    08-06-2013
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Connecting with SQL through VBA

    I see your link about code tags. Not sure how they wouold work with vba.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Connecting with SQL through VBA

    Code tags are nothing to do with VBA really, they are for posting any type of code on the forum.

    As for your questions.

    1 Try, if you want 2 minutes as the timeout, using 120.

    2 Off the top of my head it's something like this.
    Please Login or Register  to view this content.
    Actually I've found a decent example, here.

+ 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. Connecting cells of different name to one.
    By kubinha in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-27-2012, 09:26 PM
  2. Connecting to TFS
    By Osiris00001 in forum Excel General
    Replies: 1
    Last Post: 07-17-2012, 05:19 AM
  3. VBA for connecting to C++
    By Carnifex930 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2011, 03:14 PM
  4. connecting different sheets
    By luiskiambata in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-27-2008, 06:13 AM
  5. Connecting to FTP
    By ben in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2006, 12:15 PM

Tags for this Thread

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