We help IT Professionals succeed at work.
Get Started

Understanding an Append Query's SQL

79 Views
Last Modified: 2018-02-02
Hi all,

This is an extremely vague question, but if I don't ask it I'll never find an answer...

I have many queries that feed into forms. When the forms are populated with data, I want to append data from these queries into another table to try and make my data thin (as this is how Access likes it!) to feed out into another output.

Is my query doing the correct thing?

Sub AppendQueries()

Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qryRS As DAO.Recordset
Dim aa As Integer
Dim i As Integer
Dim j As Integer
Dim CSiD As Integer
Dim ProcessID As Integer
Dim PCD As Long
Dim b As Integer
Dim RecordCount As Long
Dim qryApp As String


Set db = CurrentDb
Set rs = db.OpenRecordset("tblCompSeriesProcessDetails")

    'Debug.Print rs.RecordCount

RecordCount = 1

For i = 1 To rs.RecordCount

    If RecordCount < rs.Fields("CompSeriesProcessID").Value Then
        
    RecordCount = rs.Fields("CompSeriesProcessID").Value
    
    End If
    
    rs.MoveNext
        
Next i


rs.MoveFirst

DoCmd.SetWarnings False

For j = 4 To 15

    qryApp = "qryProcess" & j

    Set qryRS = db.OpenRecordset(qryApp & "_Sub")

    For i = 1 To qryRS.RecordCount


        strSQL = "INSERT INTO tblCompSeriesProcessDetails ( CompSeriesProcessID, CompSeriesID, ProcessID_FK, PlannedCompletionDate )" _
                & "SELECT " & RecordCount + 1 & ", " & qryApp & "_Sub.CompSeriesID, " & qryApp & "_Sub.CurrentProcess, " & qryApp & "_Sub.PCD " _
                & "FROM " & qryApp & "_Sub"

        Debug.Print strSQL

        DoCmd.RunSQL strSQL

        RecordCount = RecordCount + 1

        qryRS.MoveNext

    Next i

Next j


For j = 17 To 33

qryApp = "qryProcess" & j

strSQL = "INSERT INTO tblCompSeriesProcessDetails ( CompSeriesProcessID, CompSeriesID, ProcessID_FK, PlannedCompletionDate )" _
        & "SELECT " & RecordCount + 1 & ", " & qryApp & "_Sub.CompSeriesID, " & qryApp & "_Sub.CurrentProcess, " & qryApp & "_Sub.PCD " _
        & "FROM " & qryApp & "_Sub"

Debug.Print strSQL

DoCmd.RunSQL strSQL


RecordCount = RecordCount + 1

Next j

DoCmd.SetWarnings True

Set qryRS = Nothing
Set rs = Nothing
Set db = Nothing


End Sub

Open in new window


(prefix tbl is a table, prefix qry is a query, process 16 behaves in a totally different manner to the rest so have created loops from 4-15 and 17-33, 33 processes in total, similarities come from 4-15 and 17-33)

Also, do I need to loop for all the records in my query?

Thanks in advance for a quick response!

Sarith
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE