• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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)
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

End Sub
2 Solutions
>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)?
Helen FeddemaCommented:
I would suggest using single quotes within the SQL string, to avoid problems.
>>> Please see query in attached database

There is no database attached to your question.

You cannot "Run" a SELECT query.   DoCmd.RunSQL  requires an Action query, such as SELECT ... INTO, INSERT, UPDATE, DELETE, etc.
CFMIFinancial Systems AnalystAuthor Commented:
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


it all worked beautifully!

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

Thanks to all.
CFMIFinancial Systems AnalystAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now