troubleshooting Question

Understanding an Append Query's SQL

Avatar of Sarith Gada
Sarith GadaFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft AccessSQL
5 Comments1 Solution82 ViewsLast Modified:
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

(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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros