Solved

Current Stock Value

Posted on 2011-03-18
6
637 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

912 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

19 Experts available now in Live!

Get 1:1 Help Now