Link to home
Start Free TrialLog in
Avatar of CFMI
CFMIFlag for United States of America

asked on

Data From Many to One Table - vba

I'm trying to pull data from many months into one table.  I've started with the vba code below but get bogged down when it comes to using the various source tables.  Please see query in attached database.  Ultimately I want data from both 201404 and 201405 tables into one 'tbl_Med-Drug_PREM' table with a column showing the net for each month.  The 'qSel_QS_PREM_SUM' is a sample from only one generic table but it shows the output with only one month's NET column.  

Below is the code.  I know the query works but it returns with a Run-time error '2342'.  " A RunSQL action requires an argument consisting of an SQL statement."

I think it's missing a "rstGroup" or some such that should be part of the run/movenext commands but don't know where it should be placed.  I've seen in my research that other errors can result if I place it wrong.

I'm hoping to get all months, each with its own net column but can't get through it with my limited vba skills.  Can someone please review the code I started to help me flesh out this little project?

Public Sub Quota_Share_Out()

Dim Db As DAO.Database
Dim rstTables As DAO.Recordset
Dim rstGroup As DAO.Recordset
Dim strSql As String
    Set Db = CurrentDb
    Set rstTables = _
      Db.OpenRecordset("tblPeriods", dbOpenTable)
     
rstTables.MoveFirst
Do While Not rstTables.EOF
    strSql = "SELECT Format([TDS_GB_ENT " & rstTables.Fields(0) & "]![BLAC_POSTING_DT],""yyyymm"") AS YYYYMM, [TDS_GB_ENT " & rstTables.Fields(0) & "].GRGR_ID, [TDS_GB_ENT " & rstTables.Fields(0) & "].GL_LGL_ENT_CD, " & _
    "Facets_QS_Gps_xls.[GRP NAME], IIf(IsNull([Facets_QS_Gps_xls]![GRP NAME]),""N"",""Y"") AS QS, [TDS_GB_ENT " & rstTables.Fields(0) & "].GL_CJA_CD, Sum([TDS_GB_ENT " & rstTables.Fields(0) & "].NET_AMT) AS NET " & _
    "FROM [TDS_GB_ENT " & rstTables.Fields(0) & "] LEFT JOIN Facets_QS_Gps_xls ON [TDS_GB_ENT " & rstTables.Fields(0) & "].GRGR_ID = Facets_QS_Gps_xls.GROUP " & _
    "WHERE (((Mid([TDS_GB_ENT " & rstTables.Fields(0) & "]![CSPI_ID],""7"",""1"")) Not In (""A"",""B"")) AND ((Left([TDS_GB_ENT " & rstTables.Fields(0) & "]![PDPD_ID],""1"")) Not In (""D"",""V"")) AND " & _
        "((Mid([TDS_GB_ENT " & rstTables.Fields(0) & "]![PDPD_ID],""4"",""1"")) Not In (""R"")) AND (([TDS_GB_ENT " & rstTables.Fields(0) & "].RATE_SIZE_IND) In (""5"",""6"",""7"",""8"")) AND (([TDS_GB_ENT " & rstTables.Fields(0) & "].ACGL_TYPE)=""P"")) " & _
    "GROUP BY Format([TDS_GB_ENT " & rstTables.Fields(0) & "]![BLAC_POSTING_DT],""yyyymm""), [TDS_GB_ENT " & rstTables.Fields(0) & "].GRGR_ID, [TDS_GB_ENT " & rstTables.Fields(0) & "].GL_LGL_ENT_CD, Facets_QS_Gps_xls.[GRP NAME], " & _
    "IIf(IsNull([Facets_QS_Gps_xls]![GRP NAME]),""N"",""Y""), [TDS_GB_ENT " & rstTables.Fields(0) & "].GL_CJA_CD;"
     
     Debug.Print strSql
     
'DoCmd.SetWarnings False
DoCmd.RunSQL strSql
DoCmd.SetWarnings True
rstTables.MoveNext
Loop

End Sub
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

>I know the query works but it returns with a Run-time error '2342'.  " A RunSQL action requires an argument consisting of an SQL statement."

The fact that you're getting the error suggests that your query is not properly formulated.

If you take the output from your "Debug.Print strSql" line, does that execute if you paste it into the query builder?

How many tables do you have to loop through?

Why is your dynamic query a SELECT rather than an INSERT (if you are trying to build a summary table with the results of the queries)?
ASKER CERTIFIED SOLUTION
Avatar of Helen Feddema
Helen Feddema
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFMI

ASKER

mbizup - Issue resolved with "You cannot "Run" a SELECT query."  Well I was trying to create an INSERT query. . . .OOPS.  When I changed it to

"INSERT INTO [tbl_Med-Drug_PREM] ( Period, Source, QS, GRGR_ID, [GRP NAME], GL_LGL_ENT_CD, GL_ACCT_NBR, GL_RSK_CD, GL_PRODT_CD, GL_MKT_CD, GL_CJA_CD, NET ) " & and added a "GROUP BY"

it all worked beautifully!

Helen_Feddema - I changed the double quotes to single and will note that for the future.

Thanks to all.
Avatar of CFMI

ASKER

Another mistake.  My apologies to SimonAdept.  I failed to note he also saw "a SELECT rather than an INSERT" and should have been given points.

I'm so sorry and will be sure to allocate better next time.  If I can redo it this time, please notify.