Graham, It's been awhile since I have had time to mess with this problem,
sorry for the lapse.
In response to your question, I have not created another Dsum function that
I know of. This is not a larger project and I am not much of a Vbasic
programmer! I did try changing the call to Access.DSum instead but same
results. I would like to try your function but I am getting a syntax error.
Access does not explane what is wrong. The debugger puts a yellow arrow at
the beginning of the words Public Function and the Set rs line is in red.
This is the ENTIRE block of code for the Bin form:
Public Function SumMyTotalWeight(lngLoadNumber As Long) As Long
' or "As Double" as required
Dim rs As DAO.Recordset
Set rs = DBEngine(0)(0).OpenRecordset( _
"Select Sum([Total Weight]) from bin where [LoadNumber]=" _
& lngLoadNumber", dbOpenSnapshot)
If rs.RecordCount > 0 Then SumTotalWeight = rs(0)
rs.Close
Set rs = Nothing
End Function
Private Sub Combo43_Change()
If Combo43 = "brine" Then [Empty weight] = 1100
If Combo43 = "field" Then [Empty weight] = 59
End Sub
Private Sub Combo47_Click()
Refresh
'Text82 = DSum("[Total weight]", "bin", "[LoadNumber] = " & [Combo47])
'Text82 = Access.DSum("[Total weight]", "bin", "[LoadNumber] = 99")
Text82 = SumMyTotalWeight(Combo47)
Text84 = DCount("[Total weight]", "bin", "[LoadNumber] = 99")
'Text84 = DCount("[Total weight]", "bin", "[LoadNumber] = " & [Combo47])
End Sub
Private Sub Command55_Click()
Text20 = Date
End Sub
Private Sub Form_Current()
[Bin Number].SetFocus
End Sub
Graham Mandeno said:
Hi again Dan,
I'm sorry - I've tried to reproduce this behaviour using a couple of
different tables, but I cannot.
Is it possible that your project has a function named DSum which is being
called in preference to the built-in one? Try calling Access.DSum instead,
just to check.
You could also try writing your own function, just to see if you get a
different result:
Public Function SumTotalWeight(lngLoadNumber as Long) as Long
' or "As Double" as required
Dim rs as DAO.Recordset
Set rs = DBEngine(0)(0).OpenRecordset( _
"Select Sum([Total Weight]) from bin where [LoadNumber]=" _
& lngLoadNumber", dbOpenSnapshot)
If rs.RecordCount > 0 Then SumTotalWeight = rs(0)
rs.Close
Set rs = Nothing
End Function
Then, change your line of code to:
Text82 = SumTotalWeight( Combo47 )
Please report back - I'm interested to know the result.
--
Good Luck!
Graham Mandeno [Access MVP]
Auckland, New Zealand
Dancy said:
Graham, Thank you for responding;
your suggestions did not help because I forgot to mention that LoadNumber
is a long integer type variable.
these three lines produce identical results:
Text82 = DSum("[Total weight]", "bin", "[LoadNumber] = " & [Combo47])
Text82 = DSum("[Total weight]", "bin", "[LoadNumber] = [Combo47]")
Text82 = DSum("[Total weight]", "bin", "[LoadNumber] = 99")
DSum is being constrained some what as there are over 3000 bin records but
only two with load number 99. One with a total weight of 500 and the
other
450. Dsum still reports a grand total of 1000 or 900 depending on which
record is current.
thank you!
Dan
Graham Mandeno said:
Hi Dancy
I'm surprised it returns anything at all, as Jet will have no idea what
[Combo47] is when it's processing the SQL.
You should include the *value* of the combobox, not its *name*. Try
this:
Text82 = DSum("[Total weight]", "bin", "[LoadNumber] = " & [Combo47])
Text84 = DCount("[Total weight]", "bin", "[LoadNumber] = " &
[Combo47])
If [LoadNumber] is a text field in your table, then you must enclose the
value in quotes:
... "[LoadNumber] = '" & [Combo47] & "'")
--
Good Luck!
Graham Mandeno [Access MVP]
Auckland, New Zealand
I am trying to show a total for a group of records on a form. The form
is
for data entry into my BIN table. Each BIN record in the table has a
load
assigned. Each bin record has a total weight field stored in the BIN
table.
What I want to know is the total load weight (the sum of the total
weight
field for each record with that load number) each time I assign a load
number to a bin record. I have the following code segment in VB.
Private Sub Combo47_Click()
Refresh
Text82 = DSum("[Total weight]", "bin", "[LoadNumber] = [Combo47]")
Text84 = DCount("[Total weight]", "bin", "[LoadNumber] = [Combo47]")
End Sub
The above works, but Dsum is returning the wrong value. In my test
senario
I have two bin records assigned to load "99" (combo47 will equal "99").
The
first record has a total weight of 500 and the second 450.
Dcount is correct and tells me 2 records via text84. Dsum tells me
1000
or
900 depending on which record is current. The curren record being
added
twice.
This will not do! So what have I messed up?