Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
sd sd is offline
external usenet poster
 
Posts: 9
Default sql criteria change

I have a report coming from ms query. In order to get the correct data
into this report I need to change the criteria on a daily basis. Is
there any way to change this from the sheet the report is on. I'm new
to this and think it can be done using vb. But I need some help in
getting started. Any help would be greatly appreciated.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default sql criteria change

you don't really need to use MS Query. I'm assuming that your SQL query is
passed to a database and that a table is then populated?
Its easier IMHO to use ADO and get data that way.

so, in a new standard modue, add the following code, and set a
reference(Tolls/References) to the Microsoft ActiVe Data Objects Library 2.7
or later...

The code is pared down enormously, but the idea is to get you started

Option Explicit
Sub GetUserSQL()
Dim SQL As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConn As String
Dim index As Long
Dim OK As Boolean

sConn = "PROVIDER=MSDataShape;Data PROVIDER=MSDASQL;"
sConn = sConn & "driver={SQL Server};"
sConn = sConn & "server=BHW01;uid=;pwd=;database=ASQA_Projects ;"

Set conn = New ADODB.Connection

With conn
.ConnectionString = sConn
.Open
End With

SQL = "select top 100 * from Projects"

Set rs = New ADODB.Recordset

rs.Open SQL, conn, adOpenDynamic, adLockOptimistic

If rs.RecordCount 500 Then
MsgBox "Too many rows (500) "
ElseIf rs.RecordCount = 0 Then
MsgBox "Nothing returned"
ElseIf rs.Fields.Count 255 Then
MsgBox "Too many fields"
Else
OK = True
End If

If OK Then
Range("B2").CopyFromRecordset rs
With Range("B2").Resize(rs.RecordCount, rs.Fields.Count)
.Interior.Color = 16777164 'pale blue
End With

End If

rs.Close
conn.Close

Set rs = Nothing
Set conn = Nothing

End Sub

this works as is. But you need to change the server and database names and
also alter the SQL text appropriately...





"sd" wrote:

I have a report coming from ms query. In order to get the correct data
into this report I need to change the criteria on a daily basis. Is
there any way to change this from the sheet the report is on. I'm new
to this and think it can be done using vb. But I need some help in
getting started. Any help would be greatly appreciated.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change criteria in a CF CC Excel Discussion (Misc queries) 5 September 14th 09 04:18 PM
Change Row Colour if certain criteria is met Steven Excel Worksheet Functions 2 May 6th 09 01:43 PM
Based on one cell criteria to change the rest of that row to a col Rochelle B Excel Discussion (Misc queries) 7 November 13th 05 02:42 AM
Msg Box to change Macro criteria? John Excel Programming 1 July 18th 05 03:55 PM
Change Criteria in Code hotherps[_40_] Excel Programming 5 April 11th 04 12:19 PM


All times are GMT +1. The time now is 07:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"