Solved

Current Stock Value

Posted on 2011-03-18
6
636 Views
Last Modified: 2012-08-14
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
Comment
Question by:Stephen Byrom
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35165867
The OnHand function as above looks for a ProductID as first parameter - Are you passing in a valid ProductID?
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 35165941
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
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35165971
Do you have any field on your form which contains the product id?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:Stephen Byrom
ID: 35165976
on the form "frmCurrStock" the primary key field of the item I need the stock for, I changed to "vProductID" as its name.
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 35165993
Me.txtStox = onHand(Me.vProductID)
0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 35166039
OMG!! it's annoyingly easy when you know how.

Thank you so much for your time
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now