+ Reply to Thread
Results 1 to 6 of 6

ODBC Connection

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    ODBC Connection

    I want to use code to connect Excel to an SQL database. I have successfully made this connection using MSQuery and have copied the SQL statement and the connection string to use in the code.
    Const CERMCONN for the connection and
    BuildSQL for the SQL String
    Please Login or Register  to view this content.
    As you can see I have simplified the SQL String as far as I can.
    I get an error at the line Set qtTAPEO = Sheets(SheetName).QueryTable.Add(CERMCONN, rngTape, strSQL)
    My question is how should I proceed to find if it is the SQL string or the Connection line that is giving the error
    John

  2. #2
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: ODBC Connection

    As there has been no immediate response to my ODBC link question, perhaps I can ask a more general one. I have used the connection string I found in the connection property window after I had successfully used MSquery . I used the SQL string I found in the SQL report in MSQuery. Should I have expected success when copying over to my VBA code, or are there some differences I should know about?
    John

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: ODBC Connection

    I have overcome the first problem by using a record set rather than a query table. I am still in difficulties though because I have some date variables which I need to put into the SQL string, and they are not working.
    I am using the code shown me by xlnitwit which works with MSQuery but it seems not with a recordset.
    This code works unless I add one or both lines marked ****
    Please Login or Register  to view this content.
    Can anyone show me where I have gone wrong
    John

  4. #4
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: ODBC Connection

    I have been looking at my problem from a different direction. I can manually prepare the query in MSQuery and use code to refresh it. This works but is clumsy as I have to have parameters for the dates.
    However in the connections/properties window is a button marked parameters which lets you choose a cell to give its value, and this would be an excellent solution for me. When I try it however I get an error message telling me I have the wrong date format.
    I have tried every format I can think of always with the same result. What format should I use?

    Please someone help before I go stark staring bonkers!
    John

  5. #5
    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: ODBC Connection

    Hi,

    I believe that for a querytable the connection string ought to begin with "ODBC;" which might explain the problems you had originally.

    I would suggest you enter fixed dates in your SQL string rather than using parameters, in order to determine what format works. It should then be a fairly simple matter to convert to a parameter- it may simply be a question of entering a formatted text string, rather than an actual date, into the parameter cell.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: ODBC Connection

    xlnitwit

    I have been entering fixed dates in the way you suggest and have found that MSQuery likes dates formatted yyyy/mm/dd. When I use a cell with such a formatted date I get an error.
    I have not thought about putting in a string though so thanks for that I shall go away and try it.
    John

+ 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] Use ODBC connection without setting it up in ODBC administration
    By ExcelGal in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-18-2012, 09:59 AM
  2. ODBC Connection:
    By Miguel Martins in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-04-2012, 02:39 PM
  3. Excel VBA makes ODBC connection to Access-How do you close the connection?
    By MrHockey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-04-2010, 06:29 PM
  4. ODBC Connection
    By ffffloyd in forum Excel General
    Replies: 1
    Last Post: 12-28-2008, 11:48 PM
  5. ODBC Connection
    By hiltonsaker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-09-2008, 10:35 PM
  6. SQL ODBC connection
    By pmguerra in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-24-2008, 11:15 AM
  7. ODBC connection
    By ExcelUser4 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-01-2007, 05:04 PM
  8. ODBC Connection
    By Adrian T in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-25-2005, 05:06 PM

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