+ Reply to Thread
Results 1 to 2 of 2

Excel VBA Querying SQL Server including Sheet Cell Reference in Query

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2015
    Location
    FL
    MS-Off Ver
    MS Office 2016
    Posts
    5

    Question Excel VBA Querying SQL Server including Sheet Cell Reference in Query

    Good Afternoon, Everyone! I'm trying to put together a workbook that queries a SQL server and have been successful in using the original vb code, but when I try to include a 'WHERE' in the SQL statement, it doesn't work. Here is what I have that works and returns all records. I have tried multiple ways of putting a 'WHERE' clause after 'FROM table' with all of them failing. What I'm trying to accomplish is, in Sheet1 of my workbook, I have Cell B4 that I would like the SQL statement to reference; (i.e. return records 'WHERE name = B4). I greatly appreciate anyone and everyone's help!

    This code, immediately below, works to return all records from the table.
    Private Sub RunQuery_Click()
    
        Dim conn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim sConnString As String
     
        ' Create the connection string.
        sConnString = "Provider=SQLOLEDB;Data Source=SERVERNAME\SQLEXPRESS;" & _
                      "Initial Catalog=CATALOG;" & _
                      "Integrated Security=SSPI;"
        
        ' Create the Connection and Recordset objects.
        Set conn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        
        ' Open the connection and execute.
        conn.Open sConnString
        Set rs = conn.Execute("SELECT name, description, comment FROM table;")
        
        ' Check we have data.
        If Not rs.EOF Then
            ' Transfer result.
            Sheets(2).Range("A1").CopyFromRecordset rs
        ' Close the recordset
            rs.Close
        Else
            MsgBox "Error: No records returned.", vbCritical
        End If
    
        ' Clean up
        If CBool(conn.State And adStateOpen) Then conn.Close
        Set conn = Nothing
        Set rs = Nothing
        
    End Sub
    I've tried, to no avail:

    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = B1 ;")
    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = B1 ")
    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = " & B1 & ";")
    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = " & B1 & ")
    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = " & Range("B1") & ";")
    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = " & B1 & ";")
    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = "  & Sheets("Sheet1").Range("B4").Value & ";")

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Excel VBA Querying SQL Server including Sheet Cell Reference in Query

    I think you are missing the quotes around the B4 value, e.g.

    Set rs = conn.Execute("SELECT name, description, comment FROM products WHERE name = '"  & Sheets("Sheet1").Range("B4").Value & "';")
    I assume B4 value is some string/text
    If you are pleased with a member's answer then use the Star icon to rate it.

+ 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] Using cell reference as a value in a Excel VBA SQL Query
    By trizzo in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-11-2013, 01:10 PM
  2. Replies: 2
    Last Post: 02-01-2013, 04:21 PM
  3. Excel Query With link To SQL Server
    By yorkshirewhite in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2011, 01:44 AM
  4. Querying a remote PH server?
    By daemonsan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-13-2007, 03:53 PM
  5. Excel Query connection to Server Help...
    By ImtiazV in forum Excel General
    Replies: 0
    Last Post: 08-03-2007, 11:04 AM
  6. Excel not finding the server in web query
    By JM in forum Excel General
    Replies: 0
    Last Post: 02-12-2006, 06:50 PM
  7. [SOLVED] Querying a SQL Server Database
    By Chaplain Doug in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-14-2005, 01:45 PM
  8. Querying an excel sheet
    By Jay in forum Excel General
    Replies: 0
    Last Post: 01-12-2005, 05:06 AM

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