简体   繁体   中英

How to link two sets of data when generating an extract in Excel?

I have a simply application where I let user add a Client information (Fname, Lname, Dob) to tblClient. Each client is assigned an id such as TalID101 , TalID102 , TalID103 and so on. On another form, I let the user add child information (Fname, Lname, DOB). This information gets saved to another table, with the same id that the parent was saved with, but this information goes into another table called tblFamilyMember . I created an Excel extract, and here's my select statement:

Set g_RS = New ADODB.Recordset
g_RS.CursorLocation = adUseClient
g_strSQL = "SELECT TalID, FirstName, LastName, from dbo.tblclients "
g_RS.Open g_strSQL, g_cnDatabase
Debug.Print g_strSQL
g_RS.MoveFirst
xlRow = 1

So I select this code, and make columns for the spreadsheet and it works beautifully. What I'm trying to do is to have the child be below the parent on the spreadsheet using the TALID. So parent with TalID 101, then all kids associated with TalID101 , then parent TalID102 , and kids, etc. I'm just not really sure how to link the two tables together, I've been trying something like this, but I keep seeing errors, something about "could not find stored procedure 'false'".

Not sure if I should do Inner Join or if there's another mistake I'm not seeing?

g_strSQL = "SELECT tblFamilyMember.TalID, tblFamilyMember.FirstName,  tblFamilyMember.LastName from tblFamilyMember "
g_strSQL = g_strSQL & "Where tblFamilyMember.TalID = tblClient.TaLID"

Yes, you should use an inner join. Also if you want child records you should add them to select clause eg tblClient.FirstName etc. Try something like this:

g_strSQL = "SELECT tblClient.TalID, tblFamilyMember.FirstName, tblFamilyMember.LastName 
FROM tblFamilyMember INNER JOIN tblClient 
ON tblFamilyMember.TalID = tblClient.TaLID "

If you want to include all clients regardless if they have child members or not the try this:

 g_strSQL = "SELECT tblClient.TalID, tblFamilyMember.FirstName,  tblFamilyMember.LastName FROM tblClient 
LEFT JOIN tblFamilyMember 
 ON tblFamilyMember.TalID = tblClient.TaLID "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM