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

How to reteive data of a primary key with duplicate value

6
Hi all,i will strongly appreciate if someone could help me,to figure out how i can handel my problem.
I have this report :" RequirementQuery_Report" and i have a command button that opens this report according certain conditions.
My report hodls all information regarding the requirements in my database such as the producs,status,and release version( this last three are listed in a listbox each)
so whenever i select a certain product,with a certain status and a certain release value i will get the report.
But my problem is, some requirements have 2 products and whenever i select one of those products i do not want to have that requirements because it has an other product.but whenever i select both product then i wanna see the requirement.
Here below is the code i wrote,it works only when i select one of the product that a requirement has.
could some one help me figure out how to handle that.
i think it should be a sql command with a where clause but i do not know what to writte in there.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Generate_Report_Click()
  2.  
  3. Dim var As Variant ' Product Selected
  4. Dim status As Variant ' Status selected
  5. Dim Release As Variant 'Release version selected
  6. Dim strf As String    ' holds Report filter
  7. Dim iCounter As Integer  'a counter
  8.  
  9. iCounter = 1 'Initialize it to 1 to be able to compare it with the last selected item
  10.  
  11. If Not IsNull(var) Then
  12.   If Not IsNull(status) Then
  13.      If Not IsNull(Release) Then
  14.  
  15.            strf = " ( "
  16.          For Each var In Me.ProductList.ItemsSelected
  17.             strf = strf + "RequirementQuery.ProductTag = '" + (Me.ProductList.Column(1, var)) + "' "
  18.  
  19.                 If iCounter < Me.ProductList.ItemsSelected.Count Then ' As long as icounter<the last Prod selected; add OR
  20.                     strf = strf + "OR "
  21.                 End If
  22.                 iCounter = iCounter + 1
  23.          Next var
  24.  
  25.            strf = strf + " ) AND ( "
  26.  
  27.            iCounter = 1 'Initialize icounter again otherwise it will still have the previous value
  28.  
  29.          For Each status In Me.StatusList.ItemsSelected
  30.             strf = strf + "RequirementQuery.StatusTag= '" + (Me.StatusList.Column(1, status)) + "'"
  31.  
  32.                 If iCounter < Me.StatusList.ItemsSelected.Count Then ' As long as iCouter< the last status selected add OR
  33.                   strf = strf + " OR "
  34.                 End If
  35.                 iCounter = iCounter + 1
  36.          Next status
  37.             strf = strf + " )"
  38.  
  39.           strf = strf + "  AND ( "
  40.           iCounter = 1
  41.            For Each Release In Me.ReleaseList.ItemsSelected
  42.               strf = strf + "RequirementQuery.ReleaseVersion= '" + (Me.ReleaseList.Column(1, Release)) + " ' "
  43.  
  44.                    If iCounter < Me.ReleaseList.ItemsSelected.Count Then
  45.                          strf = strf + " OR "
  46.                    End If
  47.                     iCounter = iCounter + 1
  48.            Next Release
  49.  
  50.            strf = strf + " )"
  51.  
  52.         DoCmd.OpenReport "RequirementQuery_Report", acViewPreview, , "( " + strf + " )"
  53.  
  54.      Else
  55.        MsgBox "Provide a Release version"
  56.        Exit Sub
  57.     End If
  58. Else
  59.  
  60.    Exit Sub
  61.  
  62. End If
  63. End If
  64.  
  65. End sub
Please help.
Best Regards,
Joelle
Mar 27 '08 #1
7 1290
PianoMan64
374 Expert 256MB
Hi all,i will strongly appreciate if someone could help me,to figure out how i can handel my problem.
I have this report :" RequirementQuery_Report" and i have a command button that opens this report according certain conditions.
My report hodls all information regarding the requirements in my database such as the producs,status,and release version( this last three are listed in a listbox each)
so whenever i select a certain product,with a certain status and a certain release value i will get the report.
But my problem is, some requirements have 2 products and whenever i select one of those products i do not want to have that requirements because it has an other product.but whenever i select both product then i wanna see the requirement.
Here below is the code i wrote,it works only when i select one of the product that a requirement has.
could some one help me figure out how to handle that.
i think it should be a sql command with a where clause but i do not know what to writte in there.

Private Sub Generate_Report_Click()

Dim var As Variant ' Product Selected
Dim status As Variant ' Status selected
Dim Release As Variant 'Release version selected
Dim strf As String ' holds Report filter
Dim iCounter As Integer 'a counter

iCounter = 1 'Initialize it to 1 to be able to compare it with the last selected item

If Not IsNull(var) Then
If Not IsNull(status) Then
If Not IsNull(Release) Then

strf = " ( "
For Each var In Me.ProductList.ItemsSelected
strf = strf + "RequirementQuery.ProductTag = '" + (Me.ProductList.Column(1, var)) + "' "

If iCounter < Me.ProductList.ItemsSelected.Count Then ' As long as icounter<the last Prod selected; add OR
strf = strf + "OR "
End If
iCounter = iCounter + 1
Next var

strf = strf + " ) AND ( "

iCounter = 1 'Initialize icounter again otherwise it will still have the previous value

For Each status In Me.StatusList.ItemsSelected
strf = strf + "RequirementQuery.StatusTag= '" + (Me.StatusList.Column(1, status)) + "'"

If iCounter < Me.StatusList.ItemsSelected.Count Then ' As long as iCouter< the last status selected add OR
strf = strf + " OR "
End If
iCounter = iCounter + 1
Next status
strf = strf + " )"

strf = strf + " AND ( "
iCounter = 1
For Each Release In Me.ReleaseList.ItemsSelected
strf = strf + "RequirementQuery.ReleaseVersion= '" + (Me.ReleaseList.Column(1, Release)) + " ' "

If iCounter < Me.ReleaseList.ItemsSelected.Count Then
strf = strf + " OR "
End If
iCounter = iCounter + 1
Next Release

strf = strf + " )"

DoCmd.OpenReport "RequirementQuery_Report", acViewPreview, , "( " + strf + " )"

Else
MsgBox "Provide a Release version"
Exit Sub
End If
Else

Exit Sub

End If
End If

End sub

Please help.
Best Regards,
Joelle
Simply Joelle,

You need to put conditional statement for the second half of your SQL Statement, so that if you don't select anything on the second Listbox, then don't include the "AND (" and simply replace it with the ")" to close it up. then you'll get a valid SQL Statement.

Hope that helps,

Joe P.
Mar 29 '08 #2
Joelle
6
Simply Joelle,

You need to put conditional statement for the second half of your SQL Statement, so that if you don't select anything on the second Listbox, then don't include the "AND (" and simply replace it with the ")" to close it up. then you'll get a valid SQL Statement.

Hope that helps,

Joe P.
Hi PainoMan64,
Thank you for your reply but my problem was not if i do not select something from the second or third listbox.
May be i did not explain my problem well,In my database i have a tables named Requirements, Products.there is a many-to-many relation between those two which means that i have an intermediate table named ProductRequirements that show which requirement belongs to which product.i do also have a table named Object which holds pictures(this ones have OLE as datatype) the Object table has also a many-to-many relationship with the Requirements table,so i have also an intermediate table named ObectRequirements. I do have other table too but they have a one to many relationship with requirements table.So i made a query based on those tables and i generated a report for that.
What i want now is when ever i select "Product A "i do want to have the report with all requirements related to Product A if and only if those requirements are not related to any other product.
Thank again
Joelle
Mar 29 '08 #3
PianoMan64
374 Expert 256MB
Hi PainoMan64,
Thank you for your reply but my problem was not if i do not select something from the second or third listbox.
May be i did not explain my problem well,In my database i have a tables named Requirements, Products.there is a many-to-many relation between those two which means that i have an intermediate table named ProductRequirements that show which requirement belongs to which product.i do also have a table named Object which holds pictures(this ones have OLE as datatype) the Object table has also a many-to-many relationship with the Requirements table,so i have also an intermediate table named ObectRequirements. I do have other table too but they have a one to many relationship with requirements table.So i made a query based on those tables and i generated a report for that.
What i want now is when ever i select "Product A "i do want to have the report with all requirements related to Product A if and only if those requirements are not related to any other product.
Thank again
Joelle
Then to answer your question,

You're going to need to establish somehow, a list that would have a one to many relationship so as to be able to select the records that you want based on the conditions that you're wanting.

The easiest way to do that is search in the requirments tables that you've meantioned and see if any of those items are required and exclude them from the returned SELECT table results.

this way you can have all the items but only the ones that don't have requirements from the other.

Hope that helps,

If you want more detailed answer, if you include the table and query structure, I would be able to give you a more detailed answer.

Joe P.
Mar 30 '08 #4
Joelle
6
Hi ,
thanks for getting back to me.Here is the structure of my query from which i have generated a report

Expand|Select|Wrap|Line Numbers
  1. SELECT Requirements.RequirementId, Components.ComponentTag, Products.ProductTag, Releases.ReleaseVersion, Statusses.StatusTag, RequirementCategories.RequirementCategorySynopsis, RequirementCategories.RequirementCategoryL1, RequirementCategories.RequirementCategoryL2, RequirementCategories.RequirementCategoryL3, Requirements.RequirementDescription, Objects.Object
  2. FROM (((Releases INNER JOIN ((Products INNER JOIN (((Components INNER JOIN (Requirements INNER JOIN ComponentRequirements ON Requirements.RequirementNumber = ComponentRequirements.RequirementNumber) ON Components.ComponentNumber = ComponentRequirements.ComponentNumber) INNER JOIN Statusses ON Requirements.StatusNumber = Statusses.StatusNumber) INNER JOIN ProductRequirements ON Requirements.RequirementNumber = ProductRequirements.RequirementNumber) ON Products.ProductNumber = ProductRequirements.ProductNumber) INNER JOIN RequirementReleases ON Requirements.RequirementNumber = RequirementReleases.RequirementNumber) ON Releases.ReleaseNumber = RequirementReleases.ReleaseNumber) INNER JOIN RequirementCategories ON Requirements.RequirementCategoryNumber = RequirementCategories.RequirementCategoryNumber) INNER JOIN ObjectRequirements ON Requirements.RequirementNumber = ObjectRequirements.RequirementNumber) INNER JOIN Objects ON ObjectRequirements.ObjectNumber = Objects.ObjectNumber
  3. WHERE (((RequirementReleases.RequirementToBeSupported)=True));
  4.  
so with my 3 listboxes,named ProductList,StatusList and ReleaseList.
for this last 2 listboxes there is no problem because a requirement has only one status or release but the problem is on the product list because one requirement can have 2 products.
As an example Requirement1 is related to Product A and B.
if i select product A i will get Requirement1 and the same goes to Product B .with the codes i am using now(see first post)
which is not what i want. What i want is: to see in my report Requirement1 only if i select Product A and Product B otherwise not!

Thanks for helping!
Regards,
Joelle
Mar 31 '08 #5
Scott Price
1,384 Expert 1GB
Just a request, Joelle! When posting code, especially the long, involved queries and code sections that you have here, please use the [code] tags provided! They are simple to use: select your code text in the reply window, then click the # icon on the top of the reply window. This makes your code MUCH easier to read and debug.

As an added bonus, you can manually edit the first code tag to look like this: [code=vb] for VBA code, and [code=sql] for SQL statements.

Thanks!

Scott
Mar 31 '08 #6
PianoMan64
374 Expert 256MB
Hi ,
thanks for getting back to me.Here is the structure of my query from which i have generated a report

Expand|Select|Wrap|Line Numbers
  1. SELECT Requirements.RequirementId, Components.ComponentTag, Products.ProductTag, Releases.ReleaseVersion, Statusses.StatusTag, RequirementCategories.RequirementCategorySynopsis, RequirementCategories.RequirementCategoryL1, RequirementCategories.RequirementCategoryL2, RequirementCategories.RequirementCategoryL3, Requirements.RequirementDescription, Objects.Object
  2. FROM (((Releases INNER JOIN ((Products INNER JOIN (((Components INNER JOIN (Requirements INNER JOIN ComponentRequirements ON Requirements.RequirementNumber = ComponentRequirements.RequirementNumber) ON Components.ComponentNumber = ComponentRequirements.ComponentNumber) INNER JOIN Statusses ON Requirements.StatusNumber = Statusses.StatusNumber) INNER JOIN ProductRequirements ON Requirements.RequirementNumber = ProductRequirements.RequirementNumber) ON Products.ProductNumber = ProductRequirements.ProductNumber) INNER JOIN RequirementReleases ON Requirements.RequirementNumber = RequirementReleases.RequirementNumber) ON Releases.ReleaseNumber = RequirementReleases.ReleaseNumber) INNER JOIN RequirementCategories ON Requirements.RequirementCategoryNumber = RequirementCategories.RequirementCategoryNumber) INNER JOIN ObjectRequirements ON Requirements.RequirementNumber = ObjectRequirements.RequirementNumber) INNER JOIN Objects ON ObjectRequirements.ObjectNumber = Objects.ObjectNumber
  3. WHERE (((RequirementReleases.RequirementToBeSupported)=True));
  4.  
so with my 3 listboxes,named ProductList,StatusList and ReleaseList.
for this last 2 listboxes there is no problem because a requirement has only one status or release but the problem is on the product list because one requirement can have 2 products.
As an example Requirement1 is related to Product A and B.
if i select product A i will get Requirement1 and the same goes to Product B .with the codes i am using now(see first post)
which is not what i want. What i want is: to see in my report Requirement1 only if i select Product A and Product B otherwise not!

Thanks for helping!
Regards,
Joelle
Joelle, Like I've explained before, you're going to need to set one of the relationship between the tables to be a LEFT JOIN in order words it has to be the primary table that is going to have the main overall structure to how the other tables are related. It sounds like you're going to need to re-structure your tables so that somewhere along the line, that you have some difinitive relationships that aren't so abmiguous.

I'm also assuming that this query runs very slow, if there is a lot of data?

If you can send me data examples so I can re-create the senerio, then that would help in giving you possibly an answer to your question.

Hope that helps,

Joe P.
Mar 31 '08 #7
Joelle
6
Joelle, Like I've explained before, you're going to need to set one of the relationship between the tables to be a LEFT JOIN in order words it has to be the primary table that is going to have the main overall structure to how the other tables are related. It sounds like you're going to need to re-structure your tables so that somewhere along the line, that you have some difinitive relationships that aren't so abmiguous.

I'm also assuming that this query runs very slow, if there is a lot of data?

If you can send me data examples so I can re-create the senerio, then that would help in giving you possibly an answer to your question.

Hope that helps,

Joe P.
Hi Piano,
Sorry for getting back to uyou so late,i was out of town for a while.
Thx again for your response ,i checked my tables and restructured my table.Thx for your help
Joelle
Apr 16 '08 #8

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

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
4
by: jeff brubaker | last post by:
Hello, Currently we have a database, and it is our desire for it to be able to store millions of records. The data in the table can be divided up by client, and it stores nothing but about 7...
1
by: Anna Warloe | last post by:
This might be a dumb question, but I am pretty new to Access.... I am trying to write code in Access to take river forecasts which are delivered as attachments in emails and append the data into...
12
by: Santosh | last post by:
Since I just started my new work, I have inherited a MS Access database which has nearly 13000 records in a single table. Now, my mandate is to clean the database and maybe split the table into...
3
by: Nicolae Fieraru | last post by:
Hi All, I have a problem and I can't figure out how to solve it. My database has three tables: tblCustomers, with CustomerId and CustomerName tblProducts, with ProductId and ProductCode...
3
by: Lloyd Sheen | last post by:
I am having trouble with an app that does the following: 1. Query SQL Server and return one row 2. Bind the columns to text boxes 3. User updates info 4. User clicks update button ...
2
by: embarkr | last post by:
I am getting the error: "Violation of PRIMARY KEY constraint 'PK_tblCustomsTariffTreeMap'. Cannot insert duplicate key in object 'dbo.tblCustomsTariffCodeTreeMap'." However, the record I am...
1
by: oaklander | last post by:
I would like to make sure there are no duplicate data entries in my Oracle 9i table (called MainTable) which has an Id field that is the primary key, ValData with a varchar data type, Fid and Fid2...
2
by: lindabaldwin | last post by:
Hi there all you smarter than me people! I am stuck. I have been working on this database for days and cannot figure out how to solve my problem. I have a form with two embedded subforms. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
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.