Solved

Data From Many to One Table - vba

Posted on 2015-01-02
5
201 Views
Last Modified: 2015-01-05
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
0
Comment
Question by:CFMI
5 Comments
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40528528
>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)?
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 250 total points
ID: 40529214
I would suggest using single quotes within the SQL string, to avoid problems.
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 40530288
>>> 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.
0
 
LVL 1

Author Closing Comment

by:CFMI
ID: 40532198
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.
0
 
LVL 1

Author Comment

by:CFMI
ID: 40532215
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now