+ Reply to Thread
Results 1 to 5 of 5

Thread: SQL statement WHERE in vba code

  1. #1
    Registered User
    Join Date
    01-13-2011
    Location
    scandinavia
    MS-Off Ver
    Excel 2007
    Posts
    26

    SQL statement WHERE in vba code

    Hi,

    How can I manipulate with the data that I query in the following code? .. I want to make a WHERE statement in "select * from table_name" , but I don't know exactly how to write it..the code as below works finde, i just want to filter my data so as to fx. only look at the dates only in 2011...so i want to write something like "select * from table_name where date>1/1/2011", but that doesn't seem to work. I'm new to sql in vba so thank you a lot:

    Sub getData()
         Dim conn As Variant
         Dim rs As Variant
         Dim cs As String
         Dim query As String
         Dim row As Integer
     
         Set conn = CreateObject("adodb.connection")
         Set rs = CreateObject("adodb.recordset")
     
                cs = "DRIVER=SQL Server;"
         cs = cs & "DATABASE=database_name;"
         cs = cs & "SERVER=server_name"
     
         conn.Open cs, "userid", "password"
          
         query = "select * from table_name"
           
         rs.Open query, conn
    
        row = 0
         Do Until rs.EOF
           row = row + 1
           Cells(row, 1).Value = rs.Fields("price").Value
           rs.movenext
         Loop
     
         rs.Close
         Set rs = Nothing
     
         conn.Close
         Set conn = Nothing
     
    End Sub
    Last edited by Cedicon; 07-21-2011 at 07:25 PM.

  2. #2
    Valued Forum Contributor alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Lake County, Illinois
    MS-Off Ver
    MS Office 2010, 2007 and 2002
    Posts
    1,165

    Re: SQL statement WHERE in vba code

    Try surrounding your date 1/1/2011 in single quotes.

    Alan

  3. #3
    Registered User
    Join Date
    01-13-2011
    Location
    scandinavia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: SQL statement WHERE in vba code

    Quote Originally Posted by alansidman View Post
    Try surrounding your date 1/1/2011 in single quotes.

    Alan
    Thanks, that worked However I have a problem in variable-making the date; I want to write like:

    Dim startdate As Date
    startdate = range("K2").value
         query = "select ID,Date,price  from table_name where Date='startdate' order by Date"
    I get the error: conversion failed when converting datetime from character string. In cell K2 i have written for example 20-07-2011. Do you know any good site for manipulating with the tables in ado? i'd like to know then. thanks in advance

  4. #4
    Valued Forum Contributor alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Lake County, Illinois
    MS-Off Ver
    MS Office 2010, 2007 and 2002
    Posts
    1,165

    Re: SQL statement WHERE in vba code

    While your date in your cell k2 looks like a date, how is it formatted? Is it a date format or text format or general.

    Alan

  5. #5
    Registered User
    Join Date
    01-13-2011
    Location
    scandinavia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: SQL statement WHERE in vba code

    Quote Originally Posted by alansidman View Post
    While your date in your cell k2 looks like a date, how is it formatted? Is it a date format or text format or general.

    Alan
    The k2 cell is formatted as date, - or short dateformat it's called..it should be really simple i don't know what little thing i miss..or maybe i cannot include variables in my select-statement?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0