M
Mahen
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
(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