Solved

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

Posted on 2014-04-21
6
1,139 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
ID: 40013289
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
ID: 40014522
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
ID: 40016207
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:UserName935
ID: 40016226
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
ID: 40017147
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
ID: 40027831
Daa, I can not believe I forgot this point.
Thanks for the assistance...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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 specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

911 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

17 Experts available now in Live!

Get 1:1 Help Now