?
Solved

Beginner help running VBA code for each record in SQL query

Posted on 2009-04-21
5
Medium Priority
?
309 Views
Last Modified: 2012-05-06
I have a table with a fluxuating number of records. I would like to periodically look at that table and determine if fieldA is greater than fieldB.

How do I code vba to run a logical comparison on each record in the table?
0
Comment
Question by:jrc4728
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24197661
This can be done in sql as well like this

select iif(fieldA>=fieldB,"FieldA is Greater or Equal To FieldB","FieldB is Greater than FieldB") as FieldDiff
from mytable

syntax of iif

iif(condition,value if true,value if false)
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24197670
I can do you a vba solution as well but for what you want, it can easily be done in sql.
0
 
LVL 5

Author Comment

by:jrc4728
ID: 24197691
Thanks. I'd like to see the vba solution. I will be calling some external code if condition is true. As well, I will be performing different actions based on the amount of difference.
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 2000 total points
ID: 24198070
ok, an example that goes thru the whole table

Public Sub Example()

    Dim rs As DAO.Recordset
   
   
    'Define your SQL here, the records you want to check
    Set rs = CurrentDb.OpenRecordset("select * from mytable")
   
    'Keep looping until no more records
    Do While rs.EOF = False
   
        'If FieldA > FieldB then tell user (results immediate window)
       
        'Reference field using !
        If rs!FieldA > rs!FieldB Then
            Debug.Print "FieldA is greater"
        ElseIf rs!FieldA < rs!FieldB Then
            Debug.Print "FieldB is greater"
        Else
            Debug.Print "FieldA is the same as FieldB"
        End If
       
        'Check next record
        rs.MoveNext
    Loop
   
    'Close recordset
    rs.Close
    Set rs = Nothing
End Sub
0
 
LVL 5

Author Closing Comment

by:jrc4728
ID: 31572927
thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

839 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