Robert,
I think you don't need your function at all (but I've included it
here for comparison purposes). Your function, as I used it, is shown
here...
Option Compare Database
Option Explicit
'SELECT records from the temp table and order by Utility Type
'strSQLUSAGELIST = "SELECT UTILITY_TYPE As Utility, " _
& "UOM, " _
& "SUM(SQLConvertUOM(UTILITY_TYPE, UOM, " _
& "EXISTING_USAGE)) As Existing, " _
& "SUM(SQLConvertUOM(UTILITY_TYPE, UOM, " _
& "PROPOSED_USAGE)) As Proposed " _
& "FROM [TEMPTABLEA] " _
& "GROUP BY UTILITY_TYPE, UOM " _
& "ORDER BY UTILITY_TYPE;"
'****************************************
'This function gets the conversion factor
'for the passed usage to convert it to
'native units.
'
Public Function SQLConvertUOM( _
UtilType As String, _
UOMPassed As String, _
UsagePassed As Double) _
As Double
Dim strSQLUSAGE As String
Dim wsUSAGE As DAO.Workspace
Dim dbUSAGE As DAO.Database
Dim rsUSAGE As DAO.Recordset
strSQLUSAGE = "SELECT CONVERSION_FACTOR " _
& "FROM [UNITS CONVERSION TABLE] " _
& "WHERE" _
& " UTILITY_CODE = '" & UtilType _
& "' AND UNIT = '" & UOMPassed & "';"
Set wsUSAGE = DBEngine.Workspaces(0)
Set dbUSAGE = wsUSAGE.OpenDatabase( _
CurrentProject.FullName)
Set rsUSAGE = dbUSAGE.OpenRecordset( _
strSQLUSAGE, _
dbOpenDynaset, _
dbSeeChanges)
If rsUSAGE.RecordCount = 0 Then
rsUSAGE.Close
Exit Function
Else
SQLConvertUOM = UsagePassed _
* CDbl(rsUSAGE("CONVERSION_FACTOR"))
End If 'rsUSAGE.RecordCount...
rsUSAGE.Close
End Function 'SQLConvertUOM
... and I call it (and then ignore the result) in the [Q_UsageList]
Query shown below.
I added some stuff to your [TEMPTABLEA] Table, since a single record
doesn't give the Sum() function much exercise. Its name caused me to
yield to temptation and rename its fields to match the names of the
matching fields in [UNITS CONVERSION TABLE].
I originally added Autonumber fields to both Tables to serve as primary
keys, then decided this message would be easier to understand without
fiddling with that -- but I normally would do that in my own database,
and would define a Lookup property on that value when used as a foreign
key. In fact, what I did here was to declare [UTILITY_CODE] and [UNIT]
to jointly form the primary key of the [UNITS CONVERSION TABLE] Table.
(In [TEMPTABLEA], the same-named fields form a foreign key linking to
[UNITS CONVERSION TABLE].)
[TEMPTABLEA] Table Datasheet View:
UTILITY_CODE UNIT EXISTING_USAGE PROPOSED_USAGE
------------ ---- -------------- --------------
E MWH 600 350
E KWH 1500 1500
G MBTU 300 200
E KWH 1000 350
Although I was suspicious of the value for "G BTU", thinking it to be
inaccurate by about 3 orders of magnitude, I left it alone here -- it
doesn't materially affect the operation of the database, but I suggest
you check it.
[UNITS CONVERSION TABLE] Table Datasheet View:
UTILITY_CODE UNIT CONVERSION_FACTOR
------------ ---- -----------------
E KWH 0.001
E MWH 1
G BTU 0.001
G MBTU 1
W GAL 0.001
W KGAL 1
The following Query is inspired by the string constant strSQLUSAGELIST
from your Module, which I figured could just as easily be a named Query,
saved it as such, and then modified the heck out of it.
Fields [ExistingF] and [ProposedF] involve calls to SQLConvertUOM(}, and
fields [ExistingT] and [ProposedT] instead refer directly to [UNITS
CONVERSION TABLE]![CONVERSION_FACTOR]. [ExistingF] and [ProposedF] are
included just for comparison purposes, so that by running tests on more
extensive data you can gain a warm fuzzy feeling that the vaues returned
by the Table are just as useful as those returned by the function. (Or
you can peruse the design to convince yourself that it makes sense. Or
both.)
[Q_UsageList] SQL:
SELECT [TEMPTABLEA]![UTILITY_CODE] AS Utility,
TEMPTABLEA.UNIT,
Sum(SQLConvertUOM([TEMPTABLEA]![UTILITY_CODE],
[TEMPTABLEA]![UNIT],[EXISTING_USAGE]))
AS ExistingF,
Sum([TEMPTABLEA]![EXISTING_USAGE]
*[UNITS CONVERSION TABLE]![CONVERSION_FACTOR])
AS ExistingT,
Sum(SQLConvertUOM([TEMPTABLEA]![UTILITY_CODE],
[TEMPTABLEA]![UNIT],[PROPOSED_USAGE]))
AS ProposedF,
Sum([TEMPTABLEA]![PROPOSED_USAGE]
*[UNITS CONVERSION TABLE]![CONVERSION_FACTOR])
AS ProposedT
FROM TEMPTABLEA INNER JOIN [UNITS CONVERSION TABLE]
ON (TEMPTABLEA.UTILITY_CODE
= [UNITS CONVERSION TABLE].UTILITY_CODE)
AND (TEMPTABLEA.UNIT = [UNITS CONVERSION TABLE].UNIT)
GROUP BY [TEMPTABLEA]![UTILITY_CODE], TEMPTABLEA.UNIT
ORDER BY [TEMPTABLEA]![UTILITY_CODE];
For my sample data, this produces sums of records sorted by (converted)
unit of measure, preparatory to being summed in another Query. Despite
appearances, all the units in [ExistingT], &c., are expressed in the
standard unit for the given utility code.
[Q_UsageList] Query Datasheet View:
Utility UNIT ExistingF ExistingT ProposedF ProposedT
------- ---- --------- --------- --------- ---------
E KWH 2.50 2.50 1.85 1.85
E MWH 600.00 600.00 350.00 350.00
G MBTU 300.00 300.00 200.00 200.00
Now I define a new Query to express the sums of these values.
[Q_UsageListSum] SQL:
SELECT Q_UsageList.Utility,
Sum([Q_UsageList]![ExistingT]) AS SumOfExisting,
Sum([Q_UsageList]![ProposedT]) AS SumOfProposed,
[UNITS CONVERSION TABLE].UNIT
FROM [UNITS CONVERSION TABLE]
INNER JOIN Q_UsageList
ON [UNITS CONVERSION TABLE].UTILITY_CODE
= Q_UsageList.Utility
WHERE ((([UNITS CONVERSION TABLE].CONVERSION_FACTOR)=1))
GROUP BY Q_UsageList.Utility,
[UNITS CONVERSION TABLE].UNIT;
This just adds up the converted values and displays the unit in which
the totals are expressed, and it makes no use of the SQLConvertUOM()
function.
[Q_UsageListSum] Query Datasheet View:
Utility SumOfExisting SumOfProposed UNIT
------- ------------- ------------- ----
E 602.50 351.85 MWH
G 300.00 200.00 MBTU
Although I haven't looked too closely at your revised SQL statement, it
does appear to be malformed, as B.SABER_UOM seems to be undefined. I
assume it's a typo, but wasn't sure what you meant there.
Concerning losing your database, I'd be nervous, too, but I assume
you're making frequent backups. If, after deploying your database, you
learn that some user couldn't open his copy, you can recover from your
backups and send a new copy to the user. Users should be warned to back
up what they do, too. I sometimes include in my databases code to dump
the contents of Tables to text files (which are not normally subject to
subtle corruption -- if they're bad, it's usually immediately obvious),
and then ZIP the text to save space.
I think I have the correct SQL statement now. I should have been selecting B.
NATIVE_UOM instead of B.UNIT and I need to add a second subquery. Here is
[quoted text clipped - 54 lines]