Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-21
6
Medium Priority
?
1,236 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 85
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

670 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