I want to join 3 tables as given in the attached image, fetching common field from A, fields 2 and 3 from B and memo field from C using common field. I require the latest record in the memo field to be retrieved. Table A is the main table and tables B & C may or may not have all the common fields. When I try to retrieve memo field in table c using sub query, it is showing invalid memo in sub query.
Sub Tel_List_Load()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim mcon As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim qry As String, s As String
s = ThisWorkbook.Sheets("Data").Range("F2").Value
mcon.Open "Provider=Microsoft.ACE.oledb.12.0;Data source='"& s & "';"
qry = "Select Distinct CU_DAT.Proposal,COL_RPT.LAN,CU_DAT.C_Name,COL_RPT.Adj_Bucket,(Select Tel_List.C_Stat,Tel_List.C_Resp From Tel_List Where COL_RPT.LAN=Tel_List.LAN And Tel_List.Ctrl=(Select Max(Tel_List.Ctrl) From Tel_List Where COL_RPT.LAN=Tel_List.LAN)) From (COL_RPT Left Join CU_DAT On COL_RPT.LAN=CU_DAT.LAN) Left Join Tel_List On COL_RPT.LAN=Tel_List.LAN Where COL_RPT.Adj_Bucket>2.99 AND COL_RPT.Adj_Bucket<5 And COL_RPT.Ins Is Null And COL_RPT.Ins_Ac Is Null And CU_DAT.Pdt_Type='Two Wheeler'"
mrs.Open qry, mcon
DoEvents
ThisWorkbook.Sheets("Tel_List").Range("B4").CopyFromRecordset mrs
Set mrs = Nothing
mcon.Close
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
The subquery (select ....Tel_List.C_Resp) returns error, "You have written a subquery that can return more than one field....". But Tel_List.C_Resp and Tel_List.C_Stat are in same table but when I remove Tel_List.C_Resp it is fine. Tel_List.C_Resp is a text field. I want to return the latest record for COL_RPT.LAN from Tel_List during the join.
The tables given below are indicative, the above is my original code.