Solved

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

Posted on 2014-04-21
6
1,215 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 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