Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Looping thru records as a lookup

Posted on 2014-02-14
3
Medium Priority
?
210 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 52

Accepted Solution

by:
Gustav Brock earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

877 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