Results 1 to 1 of 1

How to test Querytable connection before actually executing SQL query

Threaded View

  1. #1
    Registered User
    Join Date
    10-03-2008
    Location
    Toronto
    Posts
    5

    How to test Querytable connection before actually executing SQL query

    Hi,
    I've tried several hours on internet for this but couldn't find a decent solution, could someone here point me in the right direction?

    I have a little piece of code to loop thru worksheets to receive data from different tables:
         For j = 1 To tn
          
            odbctxt = "ODBC;DSN=" & subsystem(j) & ";UID=" & myid & ";PWD=" & mypwd & ";MODE=SHARE;DBALIAS=" & subsystem(j) & ";"
            sheetname = wsname(j)
            Worksheets(sheetname).Activate
            cmdtxt = wheretxt(j)
            With ActiveSheet.QueryTables.Add(Connection:=odbctxt, Destination:=Range("A2"))
                  .CommandText = cmdtxt
                  .Name = "Query1"
                  .FieldNames = True
                  .RowNumbers = False
                  .FillAdjacentFormulas = False
                  .PreserveFormatting = True
                  .RefreshOnFileOpen = False
                  .BackgroundQuery = True
                  .RefreshStyle = xlInsertDeleteCells
                  .SavePassword = False
                  .SaveData = True
                  .AdjustColumnWidth = True
                  .RefreshPeriod = 0
                  .PreserveColumnInfo = True
                  .Refresh BackgroundQuery:=False
              End With
    
          Next j
    The above code wroks fine, it's just I want to improve the code :

    1. occastionally the typo in the password easilly causes the user id revoked at the remote system, so I'm just wondering if i can add one more step before the loop ( For j = 1 To tn ), to test the user id & password, if something wrong then exit the macro with the message box. Is there a easy way to test the connection without actaully execute a query ?

    2. Is it possible to capturet the SQLCODE or SQLSTATE after each SQL gets executed?

    Thank you very much !

    Jack
    Last edited by jackyan; 04-15-2013 at 04:19 PM.

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