Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

Current Stock Value

HI people,
I have taken the tip from Allen Browne's website regarding current stock values, and even copied his code for the function "OnHand".

I have built a small database to control stock items in my shop. (Please see the attached table-relationships image.)

I have a form with it's data source "tblItems" called "frmCurrStock" which opens after selecting a particular item from a combobox on an unbound form.

There is an unbound textbox on the frmCurrStock called txtStox along with all the bound fields including "pkItemID" (autonumber primary key)

I have tried for the past four hours to get the value of the "OnHand" function into the txtStox box when frmCurrStock opens. (Ihave amended the function to reflect my table names)

I would be very grateful if you could take a look at the attached code and point out my errors.

Thanx in advance for your time.

ps.
I have tried a number of ways with buttons and form load etc and I have ended up with an  OnOpen event of the form frmCurrStock  "Me.txtStox = onHand()" and I have run out of ideas.

Option Compare Database
Option Explicit

Function OnHand(vProductID As Variant, Optional vAsOfDate As Variant) As Long
    '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 (StoxDate <= " & strAsOf & ")"
        End If
        strSQL = "SELECT TOP 1 StoxDate, Quantity FROM tblStox " & _
            "WHERE ((fkItemID = " & lngProduct & ")" & strDateClause & _
            ") ORDER BY StoxDate DESC;"

        Set rs = db.OpenRecordset(strSQL)
        With rs
            If .RecordCount > 0 Then
                strSTDateLast = "#" & Format$(!StoxDate, "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(tblGudzInDtale.Quantity) AS QuantityAcq " & _
            "FROM tblGudzInNote INNER JOIN tblGudzInDtale ON " & _
            "tblGudzInNote.pkGudzInID = tblGudzInDtale.fkGudzInID " & _
            "WHERE ((tblGudzInDtale.fkItemID = " & lngProduct & ")"
        If Len(strDateClause) = 0 Then
            strSQL = strSQL & ");"
        Else
            strSQL = strSQL & " AND (tblGudzInNote.pkGudzInID " & 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(tblInvDtale.Quantity) AS QuantityUsed " & _
            "FROM tblInvNum INNER JOIN tblInvDtale ON " & _
            "tblInvNum.pkInvID = tblInvDtale.fkInvNumID " & _
            "WHERE ((tblInvDtale.fkItemID = " & lngProduct & ")"
        If Len(strDateClause) = 0 Then
            strSQL = strSQL & ");"
        Else
            strSQL = strSQL & " AND (tblInvNum.InvDate " & 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
End Function

Open in new window

Dbase.jpg
0
Stephen Byrom
Asked:
Stephen Byrom
  • 3
  • 3
1 Solution
 
Barry CunneyCommented:
The OnHand function as above looks for a ProductID as first parameter - Are you passing in a valid ProductID?
0
 
Stephen ByromAuthor Commented:
That's where I fall down.
I'm not sure how to get that parameter into the function without it failing. As I said, I've been trying for ages and keep failing. ( I am very new to functions)
0
 
Barry CunneyCommented:
Do you have any field on your form which contains the product id?
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
Stephen ByromAuthor Commented:
on the form "frmCurrStock" the primary key field of the item I need the stock for, I changed to "vProductID" as its name.
0
 
Barry CunneyCommented:
Me.txtStox = onHand(Me.vProductID)
0
 
Stephen ByromAuthor Commented:
OMG!! it's annoyingly easy when you know how.

Thank you so much for your time
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now