Solved

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

Posted on 2014-04-21
6
1,184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 41

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

734 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