LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Stored procedure in SQL + vba to return a subset in Excel based onLIKE %varchar% clause

I am using MS Excel 2003 vba to return a subset of Suppliers
(Northwind database) with "exo" in their names. The query returns
names
with "e", "x", "o", "ex", "xo", "exo". . . not exactly what I want!
Details as follows:

1) Stored procedure in Northwind database
CREATE PROCEDURE [dbo].[mlsp_Tbl_Suppliers_Filter]
(
@val varchar
)
as
SELECT TOP 100 PERCENT SupplierID, CompanyName
FROM dbo.Suppliers
WHERE (CompanyName LIKE '%'+@val+'%')
ORDER BY SupplierID
GO


2)VBA code
2a)
Sub FilterData(wkS As Worksheet, rngStart As Range, rADO As Range,
val
As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim qryD As QueryTable
Dim sSQL As String
Dim sADO As String
Range(rngStart, rngStart.End(xlDown).End(xlToRight)).Clear
sADO = ""
Do
If IsEmpty(rADO) Then Exit Do
sADO = sADO & " " & rADO.Value
Set rADO = rADO.Offset(1, 0)
Loop
sSQL = "EXEC mlsp_Tbl_" & wkS.Name & "_Filter '" & val & "';"
Set cn = New ADODB.Connection
With cn
.CursorLocation = adUseClient
.Open sADO
Set rs = .Execute(sSQL)
End With
Set qryD = wkS.QueryTables.Add(rs, rngStart)
With qryD
.RefreshStyle = xlOverwriteCells
.Refresh
End With
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub


2b) Worksheet_Change trigger in sheet "Suppliers"
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$1" Then
Application.EnableEvents = False
FilterData _
ActiveWorkbook.Sheets("Suppliers"), _
ActiveWorkbook.Sheets("Suppliers").Range("a1"), _
ActiveWorkbook.Sheets("sql").Range("rngADOnw"), _
Target.Value
Application.EnableEvents = True
End If
End Sub


2c) ActiveWorkbook.Sheets("sql").Range("rngADOnw") contains the
following:
Provider=SQLOLEDB.1;
Integrated Security=SSPI;
Persist Security Info=False;
Initial Catalog=
Northwind


2d) val is a value type in cell E1 on sheet "Suppliers".


Thanks for your help

 
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
Best way to call stored procedure, have results return in cell ran SQLScott Excel Programming 3 October 28th 08 08:14 PM
Data return from SQL Stored procedure... Jonathon Shull Excel Discussion (Misc queries) 0 July 29th 08 04:25 PM
HELP No return data from my stored procedure RocketMan[_2_] Excel Programming 1 June 6th 07 06:52 PM
Return a stored procedure value to Excel nurkus[_2_] Excel Programming 1 September 6th 06 07:52 AM
Run a stored procedure in Excel 2K ArthurB Excel Programming 0 February 26th 04 04:46 PM


All times are GMT +1. The time now is 11:56 PM.

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"