Results 1 to 4 of 4
  1. #1
    Chissy is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2011
    Posts
    2

    Unhappy DSum problem, please help!!

    Hi,



    I'm stuck with the DSum syntax and can't seem to move forward;

    I have a table called RawMeatStock & would like to be able to move different types of stock items (meat) from one location (freezers) to another. In order to do that, I first want to check whether the weight the user wants to move is less than or equal to that in the specified location. I am using a form with combo boxes for the user to select the meat item, old location and new location, and a text box to enter the weight to be moved.

    After doing a little bit of research, I decided to use DSum. I'm using the following DSum query to calculate the sum of the weight of the stock item being moved:


    WeightSum = DSum("[weight]", "RawMeatStock", "[RawMeatStock.code] = '" & Me.cboRawCode & "' " And [RawMeatStock.location] = " '" & Me.CboOldLoc & "'")

    I have also tried using variables to store the selection:

    txtTest.Value = DSum("weight", "RawMeatStock", "([code] = '" & [rawCode] & "' " And [location] = " '" & [oldLoc] & "' )")

    I have tried moving the " ' " around as suggested in different posts in the forum but none of them seem to work, I get this error every time:

    "Run-time error '2456'

    Microsoft Access can't find the field '|' referred to in your expression"

    Even when I use raw data like this:

    txtTest.Value = DSum("weight", "RawMeatStock", "([code] = '" & [B71 Kidney] & "' " And [location] = " '" & [C1 Chiller1] & "' )")

    Please can somebody tell me what I'm doing wrong!!


    *EDIT*

    Alternatively, I could use a query for this, however, i do not know how to save the query result, could someone help with that too?

    Query:

    Function GetWeightSum() As Integer
    'This function gets the total weight of a meat type existing in the specified location
    Dim rawCode As String
    Dim oldLoc As String
    Dim newLoc As String
    Dim moveWeight As String
    rawCode = cboRawCode.AfterUpdate
    oldLoc = CboOldLoc.AfterUpdate
    newLoc = CboNewLoc.AfterUpdate
    moveWeight = txtWeight.Value

    Dim db As Database
    Dim myRS As Recordset
    Dim WeightSum As String

    Set db = CurrentDb()


    Set myRS = db.OpenRecordset("SELECT Sum(weight) AS TheSum FROM RawMeatStock WHERE code='" & rawCode & "' And location='" & oldLoc & "';")
    If Not myRS.EOF Then
    WeightSum = myRS.Fields("RawMeatStock.weight") 'I get an error on this line
    Else
    WeightSum = ""
    End If
    myRS.Close
    GetWeightSum = WeightSum

    Set myRS = Nothing
    Set db = Nothing

    End Function
    Last edited by Chissy; 08-05-2011 at 03:44 AM. Reason: More Information

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    WeightSum = myRS.Fields("TheSum")

    You're giving your dsum an alias in your SQL statement, you just need to call that aliased field name when you open your recordset.

  3. #3
    Chissy is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2011
    Posts
    2
    Hi rpeare,

    Thanks for the response, I still got an error after changing that, this is what's working now:

    WeightSum = myRS.Fields("TheSum").Value

    Any idea how to get DSum to work? It would save me a whole lot of coding

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Can you post an example of your database just flush out any sensitive information you have if there is any and post a zipped up file.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with Sum and DSum
    By objNoob in forum Reports
    Replies: 5
    Last Post: 12-05-2011, 01:55 AM
  2. issues with DSum
    By ozdemirozgur in forum Access
    Replies: 3
    Last Post: 05-08-2011, 02:48 PM
  3. How do I use the DSum
    By Ironclaw in forum Access
    Replies: 1
    Last Post: 08-25-2010, 07:35 AM
  4. Replies: 1
    Last Post: 05-05-2010, 01:54 AM
  5. Help with dsum
    By bjsbrown in forum Reports
    Replies: 6
    Last Post: 02-06-2010, 09:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums