0% found this document useful (0 votes)
6 views20 pages

Option Compare Database

The document contains a set of functions and enumerations for managing customer orders, invoices, and inventory transactions in a database application. It includes functionalities for creating invoices, checking order statuses, managing inventory transactions, and handling purchase orders. Additionally, it provides error handling and wrapper functions for common database operations.

Uploaded by

nibro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Option Compare Database

The document contains a set of functions and enumerations for managing customer orders, invoices, and inventory transactions in a database application. It includes functionalities for creating invoices, checking order statuses, managing inventory transactions, and handling purchase orders. Additionally, it provides error handling and wrapper functions for common database operations.

Uploaded by

nibro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Option Compare Database

Option Explicit

Public Enum CustomerOrderStatusEnum

New_CustomerOrder = 0

Invoiced_CustomerOrder = 1

Shipped_CustomerOrder = 2

Closed_CustomerOrder = 3

End Enum

Function CreateInvoice(OrderID As Long, Amt As Currency, InvoiceID As Long) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Invoices") Then

With rsw.Recordset

If Not rsw.AddNew Then Exit Function

![Order ID] = OrderID

![Amount Due] = Amt

If rsw.Update Then

.Bookmark = .LastModified

InvoiceID = ![Invoice ID]

CreateInvoice = True

End If

End With

End If
End Function

Function IsInvoiced(OrderID As Long) As Boolean

IsInvoiced = DCountWrapper("[Invoice ID]", "Invoices", "[Order ID]=" & OrderID) > 0

End Function

Function PrintInvoice(OrderID As Long) As Boolean

DoCmd.OpenReport "Invoice", acViewPreview, , "[Order ID]=" & OrderID, acDialog

End Function

Function SetStatus(OrderID As Long, Status As CustomerOrderStatusEnum) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Orders", "[Order ID] = " & OrderID) Then

With rsw.Recordset

If Not .EOF Then

.Edit

![Status ID] = Status

SetStatus = rsw.Update

End If

End With

End If

End Function
Function Delete(OrderID As Long) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Orders", "[Order ID] = " & OrderID) Then

Delete = rsw.Delete

End If

End Function

Option Compare Database

Option Explicit

Private Enum DomainFunctionWrapperEnum

DLookup_Wrapper

DCount_Wrapper

DSum_Wrapper

DMax_Wrapper

DMin_Wrapper

DAvg_Wrapper

End Enum

Private Function DomainFunctionWrapper(DomainFunction As DomainFunctionWrapperEnum, _

Expr As String, _

Domain As String, _

Optional Criteria As String) As Variant

On Error GoTo ErrorHandler


Select Case DomainFunction

Case DLookup_Wrapper

DomainFunctionWrapper = DLookup(Expr, Domain, Criteria)

Case DCount_Wrapper

DomainFunctionWrapper = DCount(Expr, Domain, Criteria)

Case DSum_Wrapper

DomainFunctionWrapper = DSum(Expr, Domain, Criteria)

Case DMax_Wrapper

DomainFunctionWrapper = DMax(Expr, Domain, Criteria)

Case DMin_Wrapper

DomainFunctionWrapper = DMin(Expr, Domain, Criteria)

Case DSum_Wrapper

DomainFunctionWrapper = DSum(Expr, Domain, Criteria)

Case DAvg_Wrapper

DomainFunctionWrapper = DAvg(Expr, Domain, Criteria)

Case Else

' Unexpected DomainFunction argument

Debug.Assert False

End Select

Done:

Exit Function

ErrorHandler:

Debug.Print Err.Number & " - " & Err.Description


' Resume statement will be hit when debugging

If eh.LogError("DomainFunctionWrapper", _

"DomainFunction = " & DomainFunction, _

"Expr = " & Expr, _

"Domain = " & Domain, _

"Criteria = '" & Criteria & "'") Then Resume

End Function

'--------------------------------------------------------

' DLookupWrapper is just like DLookup only it will trap errors.

'--------------------------------------------------------

Public Function DLookupWrapper(Expr As String, Domain As String, Optional Criteria As String) As


Variant

DLookupWrapper = DomainFunctionWrapper(DLookup_Wrapper, Expr, Domain, Criteria)

End Function

'--------------------------------------------------------

' DLookupStringWrapper is just like DLookup wrapped in an Nz

' This will always return a String.

'--------------------------------------------------------

Public Function DLookupStringWrapper(Expr As String, Domain As String, Optional Criteria As String,


Optional ValueIfNull As String = "") As String

DLookupStringWrapper = Nz(DLookupWrapper(Expr, Domain, Criteria), ValueIfNull)


End Function

'--------------------------------------------------------

' DLookupNumberWrapper is just like DLookup wrapped in

' an Nz that defaults to 0.

'--------------------------------------------------------

Public Function DLookupNumberWrapper(Expr As String, Domain As String, Optional Criteria As String,


Optional ValueIfNull = 0) As Variant

DLookupNumberWrapper = Nz(DLookupWrapper(Expr, Domain, Criteria), ValueIfNull)

End Function

'--------------------------------------------------------

' DCountWrapper is just like DCount only it will trap errors.

'--------------------------------------------------------

Public Function DCountWrapper(Expr As String, Domain As String, Optional Criteria As String) As Long

DCountWrapper = DomainFunctionWrapper(DCount_Wrapper, Expr, Domain, Criteria)

End Function

'--------------------------------------------------------

' DMaxWrapper is just like DMax only it will trap errors.

'--------------------------------------------------------

Public Function DMaxWrapper(Expr As String, Domain As String, Optional Criteria As String) As Long

DMaxWrapper = DomainFunctionWrapper(DMax_Wrapper, Expr, Domain, Criteria)


End Function

'--------------------------------------------------------

' DMinWrapper is just like DMin only it will trap errors.

'--------------------------------------------------------

Public Function DMinWrapper(Expr As String, Domain As String, Optional Criteria As String) As Long

DMinWrapper = DomainFunctionWrapper(DMin_Wrapper, Expr, Domain, Criteria)

End Function

'--------------------------------------------------------

' DSumWrapper is just like DSum only it will trap errors.

'--------------------------------------------------------

Public Function DSumWrapper(Expr As String, Domain As String, Optional Criteria As String) As Long

DSumWrapper = DomainFunctionWrapper(DSum_Wrapper, Expr, Domain, Criteria)

End Function

'--------------------------------------------------------

' DAvgWrapper is just like DAvg only it will trap errors.

'--------------------------------------------------------

Public Function DAvgWrapper(Expr As String, Domain As String, Optional Criteria As String) As Long

DAvgWrapper = DomainFunctionWrapper(DAvg_Wrapper, Expr, Domain, Criteria)

End Function
Option Compare Database

Option Explicit

Public Enum InventoryTransactionTypeEnum

Purchase_TransactionType = 1

Sold_TransactionType = 2

Hold_TransactionType = 3

End Enum

Type InventoryTransaction

ProductID As Long 'Product being added or removed to inventory

TransactionType As InventoryTransactionTypeEnum '1=Purchase; 2=Sale; 3=Hold; 4=Waste;

Quantity As Long 'Quanitity specifed for purchase, sale, hold, etc.

QuantityGranted As Long 'Actual Quanity Granted; may be less than specfied

InventoryID As Long 'Inventory Transaction ID returned to the caller

AllOrNothing As Boolean 'All or nothing flag for product allocations

Comments As String

End Type

Public Enum OrderItemStatusEnum

None_OrderItemStatus = 0

OnHold_OrderItemStatus = 1

Invoiced_OrderItemStatus = 2

Shipped_OrderItemStatus = 3

OnOrder_OrderItemStatus = 4
NoStock_OrderItemStatus = 5

End Enum

Private Const m_cNew_InventoryID = -1

Public Property Get NewInventoryID() As Long

NewInventoryID = m_cNew_InventoryID

End Property

Function AddPurchase(PurchaseOrderID As Long, ProductID As Long, Qty As Long, ByRef InventoryID As


Long) As Boolean

Dim IT As InventoryTransaction

IT.TransactionType = Purchase_TransactionType

IT.ProductID = ProductID

IT.Quantity = Qty

IT.InventoryID = m_cNew_InventoryID

If EditTransaction(IT, , PurchaseOrderID) Then

AddPurchase = True

InventoryID = IT.InventoryID

End If

End Function
Function RemovePurchase(lInventoryID As Long)

MsgBoxOKOnly CannotRemovePostedInventory

End Function

Function GetQtyAvailable(ProductID As Long) As Long

GetQtyAvailable = GetInventoryQuantity("[Qty Available]", ProductID)

End Function

Function GetQtyOnHand(ProductID As Long) As Long

GetQtyOnHand = GetInventoryQuantity("[Qty On Hand]", ProductID)

End Function

Function GetQtyToReorder(ProductID As Long) As Long

GetQtyToReorder = GetInventoryQuantity("[Qty To Reorder]", ProductID)

End Function

Function GetQtyOnBackOrder(ProductID As Long) As Long

GetQtyOnBackOrder = GetInventoryQuantity("[Qty On Back Order]", ProductID)

End Function
Private Function GetInventoryQuantity(FieldName As String, ProductID As Long) As Long

GetInventoryQuantity = DLookupNumberWrapper(FieldName, "Inventory", "[Product ID] = " &


ProductID)

End Function

Function RequestHold(OrderID As Long, IT As InventoryTransaction) As Boolean

IT.TransactionType = Hold_TransactionType

If (IT.InventoryID = m_cNew_InventoryID) Then

RequestHold = AddHold(OrderID, IT)

Else

RequestHold = ModifyHold(IT)

End If

End Function

Function AddHold(OrderID As Long, IT As InventoryTransaction) As Boolean

Dim QtyAvailable As Long

Dim QtyToHold As Long

Dim QtyRequested As Long

' Intialize Inventory quantities


QtyAvailable = GetQtyAvailable(IT.ProductID)

QtyRequested = IT.Quantity

QtyToHold = 0

' Check if we have sufficient Inventory

If QtyRequested > QtyAvailable Then

If Not IT.AllOrNothing Then

QtyToHold = QtyAvailable

End If

Else

QtyToHold = QtyRequested

End If

' Execute the Hold

If QtyToHold > 0 Then

IT.TransactionType = Hold_TransactionType

IT.Quantity = QtyToHold

AddHold = EditTransaction(IT, OrderID)

IT.Quantity = QtyRequested

Else

IT.QuantityGranted = 0

End If

End Function
Function ModifyHold(IT As InventoryTransaction) As Boolean

Dim ChangeInQuantity As Long

Dim IT_Existing As InventoryTransaction

' Get Information on Previous Hold

IT_Existing.InventoryID = IT.InventoryID

If GetTransaction(IT_Existing) Then

ChangeInQuantity = IT.Quantity - IT_Existing.Quantity

' Determine if we have sufficient Inventory to increase Hold

If ChangeInQuantity < 0 Or ChangeInQuantity < GetQtyAvailable(IT.ProductID) Then

IT.Quantity = IT.Quantity

If EditTransaction(IT) Then

IT.QuantityGranted = IT.Quantity

ModifyHold = True

Else

IT.QuantityGranted = IT_Existing.Quantity

End If

End If

End If

End Function
Function HoldToSold(InventoryID As Long) As Boolean

Dim IT As InventoryTransaction

IT.InventoryID = InventoryID

If GetTransaction(IT) Then

IT.TransactionType = Sold_TransactionType

If EditTransaction(IT) Then

HoldToSold = True

End If

End If

End Function

Function RemoveHold(InventoryID As Long) As Boolean

RemoveHold = DeleteTransaction(InventoryID)

End Function

Function GetTransaction(IT As InventoryTransaction) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Inventory Transactions", "[Transaction ID] = " & IT.InventoryID) Then

With rsw.Recordset

If Not .EOF Then

IT.ProductID = ![Product ID]

IT.Quantity = ![Quantity]
IT.TransactionType = ![Transaction Type]

IT.Comments = Nz(![Comments])

GetTransaction = True

End If

End With

End If

End Function

Function EditTransaction(IT As InventoryTransaction, Optional CustomerOrderID, Optional


PurchaseOrderID) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Inventory Transactions", "[Transaction ID] = " & IT.InventoryID) Then

With rsw.Recordset

If IT.TransactionType <= 0 Then

Exit Function

ElseIf IT.InventoryID = m_cNew_InventoryID Then

rsw.AddNew

ElseIf .EOF Then

Exit Function

Else

rsw.Edit

![Transaction Modified Date] = Now()

End If

![Product ID] = IT.ProductID

![Quantity] = IT.Quantity
![Transaction Type] = IT.TransactionType

![Comments] = IIf(IT.Comments = "", Null, IT.Comments)

If Not IsMissing(CustomerOrderID) Then ![Customer Order ID] = CustomerOrderID

If Not IsMissing(PurchaseOrderID) Then ![Purchase Order ID] = PurchaseOrderID

EditTransaction = rsw.Update

If IT.InventoryID = m_cNew_InventoryID Then

rsw.Recordset.Bookmark = rsw.Recordset.LastModified

IT.InventoryID = ![Transaction ID]

End If

End With

End If

End Function

Function DeleteTransaction(InventoryID As Long) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Inventory Transactions", "[Transaction ID] = " & InventoryID) Then

DeleteTransaction = rsw.Delete

End If

End Function

Function RestockProduct(ProductID As Long) As Boolean

Dim SupplierID As Long

Dim QtyToOrder As Long


Dim PurchaseOrderID As Long

Dim UnitCost As Long

QtyToOrder = GetQtyToReorder(ProductID)

If QtyToOrder > 0 Then

SupplierID = FindProductSupplier(ProductID)

If SupplierID > 0 Then

' Generate new Purchase Order if necessary

If PurchaseOrderID = 0 Then

If Not PurchaseOrders.Create(SupplierID, GetCurrentUserID(), -1, PurchaseOrderID) Then

Exit Function

End If

End If

' Set unit cost to standard cost for product

UnitCost = GetStandardCost(Nz(ProductID, 0))

' Add product line item to Purchase Order

If Not PurchaseOrders.CreateLineItem(PurchaseOrderID, ProductID, UnitCost, QtyToOrder) Then

Exit Function

End If
Else

' Suggested Enhancement: Handle case where product does not have a supplier

End If

End If

RestockProduct = True

End Function

Function FindProductSupplier(ProductID As Long) As Long

FindProductSupplier = DLookupNumberWrapper("[Supplier IDs].Value", "Products", "[ID]=" &


ProductID)

End Function

Function GetRestockingPurchaseOrder(SupplierID) As Long

GetRestockingPurchaseOrder = DLookupNumberWrapper("[Purchase Order ID]", "Purchase Orders",


"[Supplier ID]=" & SupplierID & " AND [Status ID] < 2")

End Function

Function FillBackOrders(ProductID) As Boolean

Dim rsw As New RecordsetWrapper

If rsw.OpenRecordset("Order Details", "[Product ID] =" & ProductID & " AND [Status ID] = " &
OnOrder_OrderItemStatus) Then
With rsw.Recordset

Dim IT As InventoryTransaction

While Not .EOF

' Back Order Products should not be associated with any Inventory at this point

If IsNull(![Inventory ID]) Then

IT.Quantity = ![Quantity]

IT.ProductID = ![Product ID]

IT.InventoryID = m_cNew_InventoryID

If FillBackOrder(![Order ID], IT) Then

.Edit

![Status ID] = OnHold_OrderItemStatus

![Inventory ID] = IT.InventoryID

.Update

MsgBoxOKOnly FilledBackOrderedProduct, ![Order ID]

End If

End If

rsw.MoveNext

Wend

End With

FillBackOrders = True

End If

End Function
Function FillBackOrder(OrderID As Long, IT As InventoryTransaction) As Boolean

IT.TransactionType = Hold_TransactionType

IT.Comments = InsertString(FillBackOrderedProduct, CStr(OrderID))

If GetQtyAvailable(IT.ProductID) >= IT.Quantity Then

FillBackOrder = EditTransaction(IT)

End If

End Function

You might also like