Solved

Looping thru records as a lookup

Posted on 2014-02-14
3
206 Views
Last Modified: 2014-02-20
I need help to create the best option for check to see if the values in a table (4 fields) = True, if they do then run 2 queries.

the

SELECT tblCurrent.Year, tblCurrent.FreezeQ1, tblCurrent.FreezeQ2, tblCurrent.FreezeQ3, tblCurrent.FreezeQ4
FROM tblCurrent;

Since each field (FreezeQ & ((numeric Value)  - ie. FreezeQ1 or FreezeQ2, etc.

I would like to use a variable to pass the numeric value of the field name.

Also loop thru each of these fields to determine if they = True.

Below is my many attempts, feel free to choose the best option to simplify the task.

   Dim strsql As String
   Dim nCt As Integer
   Dim nQtr As String
   Dim rs As DAO.Recordset
   
   Set rs = curDB.OpenRecordset("Select * from tblcurrent")
    for each (i)
    If rs.Fields("FreezeQ" & (i)) Then
    
    End If
    Do Until X = nQtr
        For X = i To nQtr
        For Each X In nQtr
            curDB.Execute ("DELETE *" & _
                            " FROM tblSummary" & _
                            " WHERE Quarter = " & nQtr & ")")
            curDB.Execute ("INSERT INTO tblSummary" & _
                            "Select *" & _
                            " FROM tblSummaryQuartersFrozen" & _
                            " WHERE Quarter = " & nQtr & ")")

        Next
     MoveNext
    Loop

Open in new window

0
Comment
Question by:Karen Schaefer
3 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 39860799
You would do something like this:
  For i = 1 to 4
    If rs.Fields("FreezeQ" & i) Then
        ...
    Next i
    

Open in new window

Ron
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39861072
I guess you are after something like:
   Dim strsql As String
   Dim nCt As Integer
   Dim db As DAO.Database
   Dim rs As DAO.Recordset
   
   Set db = CurrentDb
   Set rs = db.OpenRecordset("Select * from tblcurrent")

   While rs.EOF = False
      For nCt = 1 To 4
          If rs.Fields("FreezeQ" & CStr(i)).Value = True Then
               db.Execute ("DELETE *" & _
                               " FROM tblSummary" & _
                               " WHERE Quarter = " & nQt & ")")
               db.Execute ("INSERT INTO tblSummary" & _
                               "Select *" & _
                               " FROM tblSummaryQuartersFrozen" & _
                               " WHERE Quarter = " & nQt & ")")
          End If
      Next
      rs.MoveNext
   Wend

Open in new window

/gustav
0
 

Author Closing Comment

by:Karen Schaefer
ID: 39875326
thanks that did the trick.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

735 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