473,666 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search code...how to filter using code/SQL

stonward
145 New Member
Hi again People.

This Forum has been a serious help for me...I'm sure you know how it is...you get pretty good at handling Access when wham! you come across something you just can't figure - and if you're in a hurry at all....!

I have a Main Form, based upon my main table (products). This main form has a subform (based upon a query that is based primarily upon the Products table, plus a few others to grab hold of related data (customer names rather than ID etc)).

One of my fields of the products table is called HANDLE and is, essentially, a made up field to enable simpler searching - it's an amalgam of several fields of the table.

When I click SEARCH, i want the subform to show the results based on the HANDLE the user taps into an (unbound) textbox. This works okay using a macro to run a filter, but I need something, um, firmer and more detailed and controllable.

I know there is a way using SQL entered via a variable in code...is there some 'generic' code someone can show me as to how this may be done.

Sorry for the complexity, but i want to give you as much info as possible.

Thanks.

Stonward the Troubled.
Sep 3 '07 #1
27 4387
ADezii
8,834 Recognized Expert Expert
Hi again People.

This Forum has been a serious help for me...I'm sure you know how it is...you get pretty good at handling Access when wham! you come across something you just can't figure - and if you're in a hurry at all....!

I have a Main Form, based upon my main table (products). This main form has a subform (based upon a query that is based primarily upon the Products table, plus a few others to grab hold of related data (customer names rather than ID etc)).

One of my fields of the products table is called HANDLE and is, essentially, a made up field to enable simpler searching - it's an amalgam of several fields of the table.

When I click SEARCH, i want the subform to show the results based on the HANDLE the user taps into an (unbound) textbox. This works okay using a macro to run a filter, but I need something, um, firmer and more detailed and controllable.

I know there is a way using SQL entered via a variable in code...is there some 'generic' code someone can show me as to how this may be done.

Sorry for the complexity, but i want to give you as much info as possible.

Thanks.

Stonward the Troubled.
I don't have that much to go, on so first, a couple of assumptions:
  1. [HANDLE] Field - Field in SubForm cosisting of an amalgam of various values.
  2. Main Form Name = frmMain.
  3. SubForm Control Name = subfSubForm.
  4. Search Button Name = cmdSearch.
  5. Text Box Name = txtSearchCriter ia.
  6. RecordSource for the SubForm = qryRecordSource .
  7. Primary Key Field on Main Form = [PrimaryKey].
  8. Foreign Key Field on SubForm = [ForeignKey].
  9. One Method to use is to modify the RecordSource of the SubForm to reflect the proper Foreign Key Fields, as well as a Partial Match for Values in the [HANDLE] Field, as indicated by entries in txtSearchCriter ia.
  10. Hope this makes sense.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdSearch_Click()
    2. If Not IsNull(Me![txtSearchCriteria]) Then
    3.   Dim MySQL As String
    4.   MySQL = "Select * From qryRecordSource Where [ForeignKey] = " & Me![PrimaryKey] & " And " & _
    5.           "[HANDLE] Like '*" & Me![txtSearchCriteria] & "*'"
    6.   Forms!MainForm!subfSubForm.Form.RecordSource = MySQL
    7. End If
    End Sub
Sep 3 '07 #2
stonward
145 New Member
That's what I'm talking about! Thanks ADezii!
Of course! Change the recordsource - I'd never have discovered how to get that syntax right!

Gonna try it now.

Thanx again.

Stonward the Impressed.
Sep 4 '07 #3
stonward
145 New Member
Hi. Trying out (above) code and getting teething troubles. The Foreign Key is the foreign of the recordsource of the subform? is that right? The code doesn't appear to 'see' the query that the subform is based on...
Sep 4 '07 #4
ADezii
8,834 Recognized Expert Expert
Hi. Trying out (above) code and getting teething troubles. The Foreign Key is the foreign of the recordsource of the subform? is that right? The code doesn't appear to 'see' the query that the subform is based on...
The Foreign Key is the Field that relates back to the Primary Key in the Main Form and is on the MANY side of the Relationship. If the code is not 'seeing' the Query, make sure:
  1. You have the Query Name spelled correctly.
  2. The Query in question, is in fact the RecordSource for the SubForm.
  3. The Foreign Key Field exists in the Query.
  4. Ledt me know how you make out.
Sep 4 '07 #5
stonward
145 New Member
No, sorry Adezii - i can't seem to get it running - and I've tried MANY permutations!

My Main form (FrmMain) is based upon my Products table. That table has a Primary key (of course). My subform is based upon a query - that query is also based upon the Products table. The subform is linked to the main via the Handle field. I think i may have a fundamental error relating to key use?

My products are tyres...the handle has a format like 195 55 15. I added the Handle field because i didn't know a way of searching using two separate fields, i.e., size (195x15) and series 15.

Please let me know what othe info i can give you to help. This is a major sticking point for both my system and my learning!
Sep 5 '07 #6
stonward
145 New Member
It has occurred to me that subforms are supposed to have a one:many relationship with their main form? Is that correct? if so, how can i base the forms upon what i need to base them on whilst obeying those rules?!

Stonward the Perplexed
Sep 5 '07 #7
ADezii
8,834 Recognized Expert Expert
It has occurred to me that subforms are supposed to have a one:many relationship with their main form? Is that correct? if so, how can i base the forms upon what i need to base them on whilst obeying those rules?!

Stonward the Perplexed
Please Post all relevant information such as: Table(s), Fields, Data types of the Fields, sample data, brief description of exactly what you are trying to accomplish, etc.
Sep 5 '07 #8
MMcCarthy
14,534 Recognized Expert Moderator MVP
Stoneward, I'm a little confused about what you are doing. Can you give the metadata of the table products and the full SQL of the query behind the subform.

Here is an example of how to post table MetaData :
Table Name=tblBooking s
Expand|Select|Wrap|Line Numbers
  1. Field; Type; IndexInfo
  2. StudentID; Autonumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. Mark; Numeric
  7. LastAttendance; Date/Time
Sep 5 '07 #9
stonward
145 New Member
Thanks Guys, I really appreciate this:

(The product is tyres)
Products Table (tblProducts)
ProductID; AutoNum; PK
Handle; Text
CategoryID; Number; FK (Categories Table PK)
Series; Text; FK (Series Table PK)
Size; Text; FK (Size Table PK)
Rating; Text; FK (etc)
Manufacturer; Text; FK
Notes; Text
UnitCost; Currency
Trade; Currency
Retail; Currency
Diameters; Number
Widths; Number
Stocklevel; Number

The CATEGORIES table tells me what type of tyre it is, Car, Van, 4X4 etc...
it's included in the (following) query(ies) so we get a tyre type and not just an ID number...

Subform Query ("Subform2", subform for frmMain)

SELECT Products.Produc tID, Products.Handle , Categories.Cate goryName, [Widths] & " " & [Series] & " " & [Rating] & " " & [Diameters] & " - " & [Manufacturer] & " " & [Notes] AS Tyre, Products.UnitCo st, Products.Trade, Products.Retail , Products.StockL evel
FROM Categories INNER JOIN Products ON Categories.Cate goryID = Products.Catego ryID
ORDER BY Products.Rating DESC , Products.Manufa cturer;


I need to have the User type in what they recognise, so I have an input mask set to allow input in this format; 195 55 15, or 185 65 14.

IF POSSIBLE, i need to add RATING to this to get 195 55 HR 15....

So, when a user taps in the handle, I need the subform to show all tyres that match that size/series (and, hopefully) rating.

Linking the subform to the main via the HANDLE field works, but the user must tap in every character- there is no leeway at present, and it doesn't always work correctly. For example, if one presses Return after entering handle everything is okay, but if he clicks the SEARCH button, the subform disappears!!

Help?

Thanks again.

StonwardR

PS: If you need anything else, please don't hesitate to ask...I've gotta get this right!
Sep 6 '07 #10

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

Similar topics

1
2095
by: N. Graves | last post by:
Hi, I want to have a Search Dialog box that has several text box and fields to build a search and display the results in a form. I can do everything that I need to if I us a report but I would like to have the search from data displayed in a form. The structure that I have for this was take from the Asset Manger from MS. Anyway I open a report that in turn opens up a search form that allow me to select data. Then you continue by...
9
20309
by: Christopher Koh | last post by:
I will make a form which will search the database (just like google interface) that will look/match for the exact name in the records of a given fieldname. Any suggestions on how to make the code?
8
3210
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled 'search' may be clicked on by the user and the user can then search all records by postcode. I want to do this to prevent duplicate data entry.
2
7246
by: Homey! | last post by:
Hello all I am new to Access. I have imported data from an old FoxPro 2.x database. This is probably the most basic function but I cant get a search box to work. I need to search for company name and cant figure it out in access. Tony (homey)
0
2071
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently structured relational database system. The CMS stores articles. The CMS also stores related items --...
6
2422
by: KiwiGenie | last post by:
Hi..I am trying to make a search form. I am fairly new to access and could well be looking at it completely wrong. I have an unbound form with textboxes in the header for entering different search criteria. I have a subform for displaying the results, which is bound to Query4. SQL for Query4 (taken from sql view in query): SELECT tblRecipes.RecipeName, tblRecipes.FoodCategory, Sum(Query3.IngredCost) AS SumOfIngredCost, Query3.RecipeID FROM...
3
2205
by: Elainie | last post by:
I would like to search a form with many fields on it, with out using the search facility through access. Througth a drop down list if possible.... How would I go about this? How could I also create this from a top bar menu option too...? Elaine
2
1601
by: chungiemo | last post by:
Hi Everybody, I am using Alan Brownes Example of a search form example and I keep getting an error of the following:- Enter Parameter Value Msg Box with the entered Value "Mar" and I re-enter "Mar" the value is displayed. Then the query works. I don't know why this msg box keeps on displaying. Hopefully somebody can help me with this. A bit more info down below.....
0
2719
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only, cannot serch by combine 3 table) Example I have the query table below, how do I make the code to seach based on the query from this: SELECT Product.ID, Product.Description, Quantity.Quantity, Quantity.SeialNo, Quantity.SupplierID,...
6
4664
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option for 2 list boxes:- county and nationality, while trying to add a third multi select list box for qualifications search is where i encounter my problem. I've copied the working code from my working list boxes, however it cant seem to pick up the...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.