Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 7th, 2005, 02:37 PM
DaisySara DaisySara is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 4 DaisySara User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 25 sec
Reputation Power: 0
Unhappy Help with subqueries

Hello. Here is my issue.

Table A:
ID Name
1 Vance
2 Kasey
3 Alyssa

Table B:
ID IDA Image
1 1 Vance1.jpg
2 1 Vance2.jpg
3 2 Kasey.jpg
4 2 Kasey2.jpg

Tables connected by A.ID = B.IDA

I want to show a list of ALL names from table A, and their corresponding image, however, only the image with the max B.ID number.

For instance:
Name ID Image
Vance 2 Vance2.jpg
Kasey 4 Kasey2.jpg
Alyssa

I am assuming you use subqueries, but cannot figure it out...thanks for any help.

Reply With Quote
  #2  
Old January 7th, 2005, 03:09 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Just place the criteria under your tableB ID field using the DMax Function

DMax("[ID]","TableB")

Make sure TableA has your first criteria or sort, then TableB with your second criteria.

lwells

Sorry didn't read throughly...you wanted to display all names and then the associated picture with each name from TableB
Subqueries will only let you return one record at a time.

Last edited by lwells : January 7th, 2005 at 03:25 PM.

Reply With Quote
  #3  
Old January 7th, 2005, 09:46 PM
DaisySara DaisySara is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 4 DaisySara User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 25 sec
Reputation Power: 0
Right...so I don't know how to do it without subqueries.




Quote:
Originally Posted by lwells
Just place the criteria under your tableB ID field using the DMax Function

DMax("[ID]","TableB")

Make sure TableA has your first criteria or sort, then TableB with your second criteria.

lwells

Sorry didn't read throughly...you wanted to display all names and then the associated picture with each name from TableB
Subqueries will only let you return one record at a time.

Reply With Quote
  #4  
Old January 9th, 2005, 05:29 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Well, the only way I know how to do what you want is through vba code by creating a function. This function would create the sql for your query with the criteria that you want. The following example function can be run from a command button that would build this type of sql:

Function GetRecentPhoto()
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset, rs1 As DAO.Recordset
Dim strSQL As String, strSQL1 As String
Dim strCriteria As String

strSQL = "SELECT TableA.* FROM TableA;"

Set db = CurrentDb()
Set qdf = db.QueryDefs("QueryName")
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

With rs
Do Until rs.EOF
strSQL1 = "SELECT TableB.* From TableB " & _
"WHERE ID = " & DMax("[ID]", "TableB", "[IDA]=" & rs!ID)
Set rs1 = db.OpenRecordset(strSQL1, dbOpenSnapshot)
strCriteria = strCriteria & "TableB.ID =" & rs1!ID & " OR "
.MoveNext
Loop
End With

strCriteria = Left(strCriteria, Len(strCriteria) - 3)

qdf.SQL = "SELECT TableA.*, TableB.* " & _
"FROM TableA LEFT JOIN TableB ON TableA.ID = TableB.IDA " & _
"WHERE " & strCriteria

Set db = Nothing
Set qdf = Nothing
Set rs = Nothing
Set rs1 = Nothing
Exit_Handler:
Exit Function
Err_Handler:
MsgBox Err.Description & " " & Err.Number
Resume Exit_Handler
End Function

This function however requires a matching record in TableB for each Record in TableA or you will receive an error message. The photo can be missing, but there will have to be an IDA number in TableB for each ID in TableA.

Hopefully this will give you some ideas.
lwells

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Help with subqueries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT