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.
Try surrounding your date 1/1/2011 in single quotes.
Alan
Alan
Click the * below to say thanks.
Database Principles
Pivot Table Tips
Good Excel Video Tutorials
Sumifs or SumProduct
DataPig Access Tutorials
MS Query Tutorial
Worst Pie Chart Ever?
Thanks, that workedHowever I have a problem in variable-making the date; I want to write like:
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 advanceDim startdate As Date startdate = range("K2").value query = "select ID,Date,price from table_name where Date='startdate' order by Date"![]()
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
Alan
Click the * below to say thanks.
Database Principles
Pivot Table Tips
Good Excel Video Tutorials
Sumifs or SumProduct
DataPig Access Tutorials
MS Query Tutorial
Worst Pie Chart Ever?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks