Calling an adodb recordset in a text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Digital Oatmeal
    New Member
    • Mar 2012
    • 4

    Calling an adodb recordset in a text box

    I am trying to call a function from a text box on a subform that returns a summed total of a single calculated field. I need to reproduce this function in multiple subforms so it is encapsulated as possible. Here's the code:

    Code:
    Function Receipts(ByVal CHEMICAL As String) As ADODB.Recordset
        Dim sql As String
        Dim beginningDate As Date
        Dim endingDate As Date
        Dim chemicalID As String
        
        beginningDate = DateAdd("h", 6, [Forms]![frmProduction]![txtBeginningDate])
        endingDate = DateAdd("h", 30, [Forms]![frmProduction]![txtEndingDate])
        chemicalID = CHEMICAL
    
    sql = "SELECT (Sum(Abs([tblReceiptScaleData]![WeightIn1]-[tblReceiptScaleData]![WeightOut2]+[tblReceiptScaleData]![WeightIn2]-[tblReceiptScaleData]![WeightOut1]))) AS OffloadQty " _
        & "FROM ItemMasterList INNER JOIN (sPULINH INNER JOIN tblReceiptScaleData ON sPULINH.Pono = tblReceiptScaleData.OrderNumber) ON ItemMasterList.SAGEItemKey = sPULINH.Itemkey " _
        & "WHERE (((ItemMasterList.OperationsDescription) = " & chemicalID & ") AND ((tblReceiptScaleData.TimeOut2) >= " & beginningDate & ") AND ((tblReceiptScaleData.TimeOut2) <= " & endingDate & ")); "
    Set Receipts = New ADODB.Recordset
    Receipts.Open sql, CurrentProject.Connection, , adLockOptimistic
    'Receipts = rs("OffloadQty")
       Set Receipts = Nothing
    The commented out code is from trying to have the function return a variant and using "rs" as the recordset and opening rs and setting Receipts to rs.

    As it is I am getting "Syntax error(missing operator) in query expression" and lists out the WHERE clause of my sql.

    With it set up using rs as the recordset and returning a variant it gives me "Run-time error '91': Object variable or With block variable not set."


    Thanks ahead of time for any help.
    Attached Files
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32634

    #2
    Originally posted by Digital Oatmeal
    Digital Oatmeal:
    As it is I am getting "Syntax error(missing operator) in query expression" and lists out the WHERE clause of my sql.
    In that case I suggest you post the WHERE clause of your SQL for someone to look at.

    Comment

    • Digital Oatmeal
      New Member
      • Mar 2012
      • 4

      #3
      Rookie mistake. Now I have this:
      Code:
      SELECT (Sum(Abs([tblReceiptScaleData]![WeightIn1]-[tblReceiptScaleData]![WeightOut2]+[tblReceiptScaleData]![WeightIn2]-[tblReceiptScaleData]![WeightOut1]))) AS OffloadQty
      FROM ItemMasterList INNER JOIN (sPULINH INNER JOIN tblReceiptScaleData ON sPULINH.Pono=tblReceiptScaleData.OrderNumber) ON ItemMasterList.SAGEItemKey=sPULINH.Itemkey
      WHERE (((ItemMasterList.OperationsDescription) ='BD') And ((tblReceiptScaleData.TimeOut2)>=#3/27/2012 6:00:00 AM#) And ((tblReceiptScaleData.TimeOut2)<=#3/28/2012 6:00:00 AM#));
      The sql works...I was missing the "#" signs on the dates.

      But now I have "Object variable or With block variable not set" error on the line where the recordset is opened.
      Last edited by NeoPa; Mar 27 '12, 11:27 PM. Reason: Added mandatory [CODE] tags for you

      Comment

      • Digital Oatmeal
        New Member
        • Mar 2012
        • 4

        #4
        SOLVED!!!!!

        Here's the code:
        Code:
        Function Receipts(ByVal CHEMICAL As String) As Variant
            Dim sql As String
            Dim beginningDate As Date
            Dim endingDate As Date
            Dim chemicalID As String
        
            beginningDate = DateAdd("h", 6, [Forms]![frmProduction]![txtBeginningDate])
            endingDate = DateAdd("h", 30, [Forms]![frmProduction]![txtEndingDate])
            chemicalID = CHEMICAL
        
        sql = "SELECT (Sum(Abs([tblReceiptScaleData]![WeightIn1]-[tblReceiptScaleData]![WeightOut2]+[tblReceiptScaleData]![WeightIn2]-[tblReceiptScaleData]![WeightOut1]))) AS OffloadQty " _
            & "FROM ItemMasterList INNER JOIN (sPULINH INNER JOIN tblReceiptScaleData ON sPULINH.Pono=tblReceiptScaleData.OrderNumber) ON ItemMasterList.SAGEItemKey=sPULINH.Itemkey " _
            & "WHERE (((ItemMasterList.OperationsDescription) ='" & chemicalID & "') And ((tblReceiptScaleData.TimeOut2)>=#" & beginningDate & "#) And ((tblReceiptScaleData.TimeOut2)<=#" & endingDate & "#)); "
        Dim rs As New ADODB.Recordset
        rs.Open sql, CurrentProject.Connection, , adLockOptimistic
            If rs.EOF And rs.BOF Then
                rs.Close
            End If
        Receipts = rs("OffloadQty")
           Set rs = Nothing
        
        End Function
        Last edited by NeoPa; Mar 27 '12, 11:28 PM. Reason: Added mandatory [CODE] tags for you.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32634

          #5
          You solved it all by yourself in the end. Good for you.

          Sometimes that can happen when you go to the effort of posting a question. Your brain is somehow stimulated to continue working on it, even when you're doing other things. Telling someone about it can have a similar effect. Very helpful, even if that person has no idea what you're on about I find.

          Comment

          • Digital Oatmeal
            New Member
            • Mar 2012
            • 4

            #6
            Don't give me all the credit. I did have a little help from my friends.

            < SNIP >
            Last edited by NeoPa; Apr 1 '12, 11:24 AM. Reason: Links to competing forum sites are not allowed.

            Comment

            Working...