473,396 Members | 2,061 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,396 software developers and data experts.

querystring with related database results

229 100+
Hi, I wonder if you anyone could give me a pointer as to how to return related results to a main query.
I have a main query from an access database, like this, it works fine.

Expand|Select|Wrap|Line Numbers
  1. description=Request("titleimagesix")
  2. If description <> "" Then
  3.  SQLQuery="Select tblGreetingcategories.categoryname,tblGreetingpostcards.categoryid,tblGreetingPostCards.titleimagesix,tblGreetingPostCards.NewImagesix from tblGreetingcategories,tblGreetingPostCards Where  tblGreetingcategories.categoryid = tblGreetingpostcards.categoryid and titleimagesix='" & replace(description ,"-"," ") & "' "
  4.  
It returns one result as expected.

Below this result i want to also list all other records that can be found with the same categoryid as the above query. I have the code below but it returns all results and not those only related to the above query. I imagine its something like adding where titleimagesix=categoryid but of course i dont know how to write it or indeed if thats right at all. Or if i need to include the main query in this one.

Expand|Select|Wrap|Line Numbers
  1. SQLQuery="Select tblGreetingcategories.categoryid,tblGreetingpostcards.categoryid,tblGreetingPostCards.titleimagesix,tblGreetingPostCards.NewImagesix from tblGreetingcategories,tblGreetingPostCards Where  tblGreetingcategories.categoryid = tblGreetingpostcards.categoryid "
Thanks in advance for any pointers
Richard
Mar 31 '12 #1

✓ answered by limweizhong

If you are so sure that the first query will return exactly one record, then you:
1. Create the Recordset
Expand|Select|Wrap|Line Numbers
  1. Dim r as Recordset
  2. Set r = CurrentDb.OpenRecordset(SQLQuery)
2. Retrieve the categoryid field of the first record
Expand|Select|Wrap|Line Numbers
  1. If Not r.EOF Then catid = r.Fields("categoryid")
3. Build the second query filtering for the categoryid that you retrieved.
Expand|Select|Wrap|Line Numbers
  1. SQLQuery2 = "... categoryid='" & catid & "' ..."
(fill in the "..." and remove the single quotes if categoryid is not a text type)

Search "Microsoft DAO Recordset object" for more information on Google.

However, if your first query has multiple records, then you will need to include the main query in the second one. There are a few ways:

Use the filter:
Expand|Select|Wrap|Line Numbers
  1. tblGreetingcategories.categoryid in (select tblGreetingcategories.categoryid from ...)
where ... is the remainder of the main query. Search "sql in operator" on Google.

Another way is to do a join with a query retrieving unique categoryid(s) of the main query, using a real nested query, but I am not sure if the new version of Microsoft Access supports this. From previous experience with old versions, the nested query needs to be saved as a QueryDef.

Note that you should also change both your queries each to use a join with the join criterion tblGreetingcategories.categoryid =tblGreetingpostcards.categoryid, using the "__table1name__ [left] join __table2name__ on __join_criterion__ " construct. Search "sql joins" on Google.

However, if you are using this query directly in a form/report, you might want to just use a sub-form/sub-report, and set the appropriate "child/master" linking fields. Search "Microsoft Access subforms" on Google.

1 1488
If you are so sure that the first query will return exactly one record, then you:
1. Create the Recordset
Expand|Select|Wrap|Line Numbers
  1. Dim r as Recordset
  2. Set r = CurrentDb.OpenRecordset(SQLQuery)
2. Retrieve the categoryid field of the first record
Expand|Select|Wrap|Line Numbers
  1. If Not r.EOF Then catid = r.Fields("categoryid")
3. Build the second query filtering for the categoryid that you retrieved.
Expand|Select|Wrap|Line Numbers
  1. SQLQuery2 = "... categoryid='" & catid & "' ..."
(fill in the "..." and remove the single quotes if categoryid is not a text type)

Search "Microsoft DAO Recordset object" for more information on Google.

However, if your first query has multiple records, then you will need to include the main query in the second one. There are a few ways:

Use the filter:
Expand|Select|Wrap|Line Numbers
  1. tblGreetingcategories.categoryid in (select tblGreetingcategories.categoryid from ...)
where ... is the remainder of the main query. Search "sql in operator" on Google.

Another way is to do a join with a query retrieving unique categoryid(s) of the main query, using a real nested query, but I am not sure if the new version of Microsoft Access supports this. From previous experience with old versions, the nested query needs to be saved as a QueryDef.

Note that you should also change both your queries each to use a join with the join criterion tblGreetingcategories.categoryid =tblGreetingpostcards.categoryid, using the "__table1name__ [left] join __table2name__ on __join_criterion__ " construct. Search "sql joins" on Google.

However, if you are using this query directly in a form/report, you might want to just use a sub-form/sub-report, and set the appropriate "child/master" linking fields. Search "Microsoft Access subforms" on Google.
Mar 31 '12 #2

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

Similar topics

7
by: Robert | last post by:
I have a php/mysql query working like so: $Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%' " All I want to do now is sort them alphabetically. By using the above...
2
by: Graham Taylor | last post by:
I am reading data from an MDB Access database into an ASP page. Four fields in the database are Yes/No fields, and I want to show those on the ASP page as check boxes. How do I do this? The...
12
by: magmike | last post by:
When users of my site submit articles to our database (http://www.netterweb.com/articles/) they almost always have http:// addresses within the text. They are pasting that text into a box which...
4
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to...
1
by: abcd | last post by:
I am making request to my asp page as http://localhost/test/test.asp?id=mike this works great query string value is retrived as "mike" whereas when I give the URL like this ...
13
by: TheBATManPhln | last post by:
I'm a newbie (less than a week of php) and I'm curious about how results can lay out. Is it possible to have the results layed out in 4 columns? Usual RECORD 1 GOES HERE RECORD 2 GOES HERE...
1
by: Brave | last post by:
I have MS Access databases that are viewed via ASP pages made in Frontpage. I have two issues that have the same problem. 1: When someone views database results on one of my webpages, and...
3
by: dlamarche | last post by:
Hello I am new to PHP and I am reading the book Build you own database driven website using PHP and MySQL. I am trying something on my own as an exercise from the book. I sucessfully created a...
1
by: shamlafazal | last post by:
When i use Send Form Results to a Database and an E-mail Address and Use the File Upload Component Simultaneously , am getting an error mentioned below. Please any can help me on this. "Database...
6
by: Christopher Fro | last post by:
Hey guys, i am quite new to this database thing, instructions on the net i find confusing, i was wondering if someone could point me in the right direction heres what im trying to do: Create a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...

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.