Solved

"VBA" "SQL" How to get a percentage of the difference between two numbers.

Posted on 2014-04-21
6
1,126 Views
Last Modified: 2014-04-28
I have a SQL query in VBA that contains several result fields that show up in a subform of a main form.  Two of these result fields are numeric.
The query looks for a difference in comparing two numeric values from two tables with the exact same structure.

Question:  Is it possible to display the difference in the two numbers as a percentage of difference?

The code is below: Thank you.

These are the two fields to get the percentage of difference:
( T1." & thismonth & " as T1Feb ,T2." & thismonth & " as T2Feb " )

' dim array variables
Dim tableone As String
Dim tabletwo As String
Dim thismonth As Variant
 
'Set Variable values
tableone = Me.ListT1.Value
tabletwo = Me.ListT2.Value
thismonth = Me.ListMonths

'Run query based on changing variables from parent form to be displayed in subform.

Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.RecordSource = "SELECT T1.MOR_GROUP ,T1.CHARTDB_BIN as T1CHARTDB_BIN, T2.CHARTDB_BIN as T2CHARTDB_BIN, T1." & thismonth & " as T1Feb ,T2." & thismonth & " as T2Feb " & _
"FROM " & tableone & " as T1 " & _
"Left JOIN " & tabletwo & "  as T2 " & _
"ON T1.MOR_GROUP = T2.MOR_GROUP " & _
"WHERE NZ(T1.CHARTDB_BIN, """") = NZ(T2.CHARTDB_BIN, """") " & _
" AND NZ(T1." & thismonth & ", """") <> NZ(T2." & thismonth & ", """");"

'Refresh the subform post query
Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.Requery

MsgBox "Done "

Thank you !
0
Comment
Question by:UserName935
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
try this.
Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.RecordSource = "SELECT T1.MOR_GROUP ,T1.CHARTDB_BIN as T1CHARTDB_BIN, T2.CHARTDB_BIN as T2CHARTDB_BIN, T1." & thismonth & " as T1Feb ,T2." & thismonth & " as T2Feb,(T1." & thismonth & "-T2." & thismonth & ")*100/T1." & thismonth  & _
"FROM " & tableone & " as T1 " & _
"Left JOIN " & tabletwo & "  as T2 " & _
"ON T1.MOR_GROUP = T2.MOR_GROUP " & _
"WHERE NZ(T1.CHARTDB_BIN, """") = NZ(T2.CHARTDB_BIN, """") " & _
" AND NZ(T1." & thismonth & ", """") <> NZ(T2." & thismonth & ", """");"

Open in new window

0
 
LVL 84
Comment Utility
You can also do this directly on the form:

1. Add a Textbox named txPct
2. Set the Textbox Format property to Percent
2. Set the Textbox ControlSource to this:

= (T1 - T2) / T1

You may need to adjust the formula to show things correctly, like:

= ((T1 - T2)*100) / T1

Or

= ((T1 - T2) / T1) * 100

Or something of that nature
0
 

Author Comment

by:UserName935
Comment Utility
Umm, some interesting approaches.

Changing the sql code did not function as a syntax error kept rearing it's ugly head, even with playing with the syntax this approach did not function.  I must be missing something or have syntax dumb hat on.

The other approach, I think would work, however there are many records and fields that get displayed as a result of the query, so, I am a bit lost on how a text box would be able to display the percentage in numeric difference for each record.

I will continue "playing" with the sql code.

Thank you everyone !!!
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:UserName935
Comment Utility
OK, I got the sql to function by changing it to this:

Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.RecordSource = "SELECT T1.MOR_GROUP ,T1.CHARTDB_BIN as T1CHARTDB_BIN, T2.CHARTDB_BIN as T2CHARTDB_BIN, T1." & thismonth & " as T1Feb ,T2." & thismonth & " as T2Feb, (T1." & thismonth & " - T2." & thismonth & ")*100/T1." & thismonth & " as something " & _
"FROM " & tableone & " as T1 " & _
"Left JOIN " & tabletwo & "  as T2 " & _
"ON T1.MOR_GROUP = T2.MOR_GROUP " & _
"WHERE NZ(T1.CHARTDB_BIN, """") = NZ(T2.CHARTDB_BIN, """") " & _
" AND NZ(T1." & thismonth & ", """") <> NZ(T2." & thismonth & ", """");"

However, the percentage is not displaying itself in the subforms result set.  does any one have any idea why that last item in the select would not show in the result set?
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
however there are many records and fields that get displayed as a result of the query
I'm not sure what you mean by this. The suggestion I made was to create a single control, and use that to show your percentage. This would have no bearing on other controls, and it would show for all records on the form.

Did you add the new field named "something" to the subform's controls? Just adding a new field to a query would not automatically show on your form - you have to open the form in Design view and drag that new field to your form.

Note if you use the SQL route, you should also check to insure that T1.thismonth is not a 0 value, as this would result in a "Divide By Zero" error.
0
 

Author Closing Comment

by:UserName935
Comment Utility
Daa, I can not believe I forgot this point.
Thanks for the assistance...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now