Solved

Current Stock Value

Posted on 2011-03-18
6
640 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 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