473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Filter by value expressed in a cell not in the VBA

I have the following code which pulls the results of a table in Microsoft access into excel. Currently the code filters the results from within the code, however I would like to filter the results by the vaule expressed in a cell.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub ADO_Demo()
  4. '   This demo requires a reference to
  5. '   the Microsoft ActiveX Data Objects 2.x Library
  6.  
  7.     Dim DBFullName As String
  8.     Dim Cnct As String, Src As String
  9.     Dim Connection As ADODB.Connection
  10.     Dim Recordset As ADODB.Recordset
  11.     Dim Col As Integer
  12.  
  13.     Cells.Clear
  14.     MsgBox "This demo retrieves the data for the records in which ITEM = LEASE and DIVISION = N. AMERICA."
  15.  
  16. '   Database information
  17.     DBFullName = ThisWorkbook.Path & "\budget.mdb"
  18.  
  19. '   Open the connection
  20.     Set Connection = New ADODB.Connection
  21.     Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "
  22.     Cnct = Cnct & "Data Source=" & DBFullName & ";"
  23.     Connection.Open ConnectionString:=Cnct
  24.  
  25. '   Create RecordSet
  26.     Set Recordset = New ADODB.Recordset
  27.     With Recordset
  28. '       Filter
  29.         Src = "SELECT * FROM Budget WHERE Item = 'Lease' "
  30.         Src = Src & "and Division = 'N. America'"
  31.         .Open Source:=Src, ActiveConnection:=Connection
  32.  
  33. '       Write the field names
  34.         For Col = 0 To Recordset.Fields.Count - 1
  35.            Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
  36.         Next
  37.  
  38. '       Write the recordset
  39.         Range("A1").Offset(1, 0).CopyFromRecordset Recordset
  40.     End With
  41.     Set Recordset = Nothing
  42.     Connection.Close
  43.     Set Connection = Nothing
  44. End Sub
  45.  
Dec 13 '07 #1
1 1662
FishVal
2,653 Expert 2GB
Assuming the cells are on worksheet "Input", cells B1, B2.

Expand|Select|Wrap|Line Numbers
  1. ................
  2. Src = "SELECT * FROM Budget WHERE Item = '" & ThisWorkBook.WorkSheets("Input").Range("B1") & "' "
  3. Src = Src & "and Division = '" & ThisWorkBook.WorkSheets("Input").Range("B2") & "'"
  4. ................
  5.  
Dec 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marek Mänd | last post by:
This posting will express my concern about the future of css3 forthcoming recommendation. I think for long time now, that the current implementation of CSS attribute opacity is less than usable...
0
by: CSDunn | last post by:
Hello, I have a problem with field filtering between an Access 2000 Project form (the application is called CELDT), and the report that shows the results of the filter. Both the form and the...
11
by: hazz | last post by:
before I start filling up the first page of perhaps many pages of code with if/then or switch:case buckets, I wanted to step back and see if there is a better way... I will have a table with up to...
0
by: g-fro | last post by:
My stored procedure is below. I would like to be able to take the delaydata.timestamp field and grab only the records during a certain hour. How would I go about doing this? Currently I filter...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.