Results 1 to 12 of 12
  1. #1
    Mclaren is offline Competent Performer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164

    Inventory control

    HI i am trying to use the "Inventory Control" example as posted on Allen Browns website. i keep getting a "Argument not Optional Error when running the function.

    I have all the tables set up as described, i have created a form that has a text box and a combo box that passes the date and productid to the function, i run the function using a button. I cannot find what i am doing wrong. I emailed Allen Browns website but they cannot help with the sample.

    has anyone used this example successfully ? if so what am i doing wrong.

    Below is the code for my form :

    Option Compare Database

    Function OnHand(vProductID As Variant, Optional vAsOfDate As Variant) As Long
    On Error GoTo OnHand_Err

    'Purpose: Return the quantity-on-hand for a product.
    'Arguments: vProductID = the product to report on.
    ' vAsOfDate = the date at which quantity is to be calculated.
    ' If missing, all transactions are included.
    'Return: Quantity on hand. Zero on error.
    Dim db As DAO.Database 'CurrentDb()
    Dim rs As DAO.Recordset 'Various recordsets.
    Dim lngProduct As Long 'vProductID as a long.
    Dim strAsOf As String 'vAsOfDate as a string.
    Dim strSTDateLast As String 'Last Stock Take Date as a string.
    Dim strDateClause As String 'Date clause to use in SQL statement.
    Dim strSQL As String 'SQL statement.
    Dim lngQtyLast As Long 'Quantity at last stocktake.
    Dim lngQtyAcq As Long 'Quantity acquired since stocktake.
    Dim lngQtyUsed As Long 'Quantity used since stocktake.

    If Not IsNull(vProductID) Then
    'Initialize: Validate and convert parameters.
    Set db = CurrentDb()
    lngProduct = vProductID
    If IsDate(vAsOfDate) Then
    strAsOf = "#" & Format$(vAsOfDate, "mm\/dd\/yyyy") & "#"
    End If

    'Get the last stocktake date and quantity for this product.
    If Len(strAsOf) > 0 Then
    strDateClause = " AND (StockTakeDate <= " & strAsOf & ")"
    End If
    strSQL = "SELECT TOP 1 StockTakeDate, Quantity FROM tblStockTake " & _
    "WHERE ((ProductID = " & lngProduct & ")" & strDateClause & _
    ") ORDER BY StockTakeDate DESC;"

    Set rs = db.OpenRecordset(strSQL)
    With rs
    If .RecordCount > 0 Then


    strSTDateLast = "#" & Format$(!StockTakeDate, "mm\/dd\/yyyy") & "#"
    lngQtyLast = Nz(!Quantity, 0)
    End If
    End With
    rs.Close

    'Build the Date clause
    If Len(strSTDateLast) > 0 Then
    If Len(strAsOf) > 0 Then
    strDateClause = " Between " & strSTDateLast & " And " & strAsOf
    Else
    strDateClause = " >= " & strSTDateLast
    End If
    Else
    If Len(strAsOf) > 0 Then
    strDateClause = " <= " & strAsOf
    Else
    strDateClause = vbNullString
    End If
    End If

    'Get the quantity acquired since then.
    strSQL = "SELECT Sum(tblAcqDetail.Quantity) AS QuantityAcq " & _
    "FROM tblAcq INNER JOIN tblAcqDetail ON tblAcq.AcqID = tblAcqDetail.AcqID " & _
    "WHERE ((tblAcqDetail.ProductID = " & lngProduct & ")"
    If Len(strDateClause) = 0 Then
    strSQL = strSQL & ");"
    Else
    strSQL = strSQL & " AND (tblAcq.AcqDate " & strDateClause & "));"
    End If

    Set rs = db.OpenRecordset(strSQL)
    If rs.RecordCount > 0 Then
    lngQtyAcq = Nz(rs!QuantityAcq, 0)
    End If
    rs.Close

    'Get the quantity used since then.
    strSQL = "SELECT Sum(tblInvoiceDetail.Quantity) AS QuantityUsed " & _
    "FROM tblInvoice INNER JOIN tblInvoiceDetail ON " & _
    "tblInvoice.InvoiceID = tblInvoiceDetail.InvoiceID " & _
    "WHERE ((tblInvoiceDetail.ProductID = " & lngProduct & ")"
    If Len(strDateClause) = 0 Then
    strSQL = strSQL & ");"
    Else
    strSQL = strSQL & " AND (tblInvoice.InvoiceDate " & strDateClause & "));"
    End If

    Set rs = db.OpenRecordset(strSQL)
    If rs.RecordCount > 0 Then
    lngQtyUsed = Nz(rs!QuantityUsed, 0)
    End If
    rs.Close

    'Assign the return value
    OnHand = lngQtyLast + lngQtyAcq - lngQtyUsed
    End If

    Set rs = Nothing
    Set db = Nothing
    Exit Function

    OnHand_Exit:
    Exit Sub

    OnHand_Err:
    MsgBox Error$
    Resume OnHand_Exit
    End Function


    Private Sub cmd_Check_Stock_Click()

    vProductID = Me.cbo_Product
    vAsOfDate = Me.txtDate
    Me.txtStockOnHand = OnHand


    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Your problem is here:

    Me.txtStockOnHand = OnHand

    because the function is expecting you to tell it which product you want the on hand quantity of. It would look like this (without the two lines previous):

    Me.txtStockOnHand = OnHand Me.cbo_Product, Me.txtDate
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Mclaren is offline Competent Performer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    Okay, tahts giving me red text, so there is something wrong there, not sure what though.

  4. #4
    Mclaren is offline Competent Performer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    Got it accepting the function:


    Me.txtStockOnHand = OnHand (Me.cbo_Product, Me.txtDate)
    but now have a problem with these 2 lines :

    Dim db As DAO.Database 'CurrentDb()
    Dim rs As DAO.Recordset 'Various recordsets.
    i am using acess 2007

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    What's the problem? If memory serves, in 2007 the MS DAO reference is replaced by the Database Engine library or words to that effect. Make sure that's checked in Tools/References.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Mclaren is offline Competent Performer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    I am getting the following error:

    Compile error
    User-defined type not defined
    If i check both: MS DAO 3.51 and 3.6 Object Libraries : i have no luck, i then get a :

    Error in loading DLL

  7. #7
    Join Date
    May 2010
    Posts
    339
    after you check these go back and look at the reference window, it might say what your missing.
    MS DAO 3.51 and 3.6 Object Libraries

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Did you try the one I mentioned? In 2007 it's:

    Microsoft Office 12.0 Access database engine Object Library
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Mclaren is offline Competent Performer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    that worked but now got a "too few parameters entered. expected1" error. am going through the code now to see if there is a requirement for a 3rd or more parameter.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    On what line? I assume it's on one of these:

    Set rs = db.OpenRecordset(strSQL)

    If so, which one? Add this right before the line that errors:

    Debug.Print strSQL

    which will print out the finished SQL to the VBA Immediate window. If you can't spot the problem, post the SQL here. Oh, and you'll likely need to comment out this line:

    On Error GoTo OnHand_Err

    so that when the code breaks you can hit debug and be taken to the line causing the error. You'd want to un-comment it after you get it working.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    Mclaren is offline Competent Performer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    I got it, my tables used the word QTY instead of Quantity.

    It is now working a treat. Thanks guys for all the help.

  12. #12
    Join Date
    Mar 2012
    Posts
    1
    hi
    i want date wise opening and closing running balance report for accounting purposed

    thanks

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

Similar Threads

  1. Inventory Tracking
    By PUDrummer in forum Access
    Replies: 3
    Last Post: 10-10-2012, 05:42 AM
  2. Inventory Calculation
    By ser01 in forum Queries
    Replies: 1
    Last Post: 04-24-2010, 12:24 PM
  3. Inventory Entry Update help
    By Richard in forum Access
    Replies: 8
    Last Post: 03-12-2010, 01:32 PM
  4. Inventory Database
    By kram941 in forum Access
    Replies: 2
    Last Post: 11-09-2009, 04:28 PM
  5. Sending control to specific control
    By wasim_sono in forum Programming
    Replies: 2
    Last Post: 04-19-2007, 08:19 AM

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