The following example uses the Split() function to calculate totals from a list box.
The following fields were used as a Row Source to a list box (lstTotal):
poid: ID
Ct: # of entries
Qty: quantity for each order
Catid: Catagory ID
An example record:
poid: 3
Ct: 12
Qty: 1,2,1,3,1,2,1,1,5,1,3,1
Catid: 4167-G,7166-G,4169-H,6141WT-G,4170WT-G,T-2311-J,4493PS-H,7520-H,4483-E,4484-J,4487WT-H,2218-G
For record 3 there were 12 items ordered, the quantity for each item given in the Qty string, and the category id for each item given as Catid. In the example below the year is retrieved and the price is looked up from a separate table dependent on the year ordered. Finally a query is used to update the Total for the order using [poid] and [Total] as hidden textboxes on the form.
Not rocket science but thought it might be helpful for anyone looking to use the Split() function.
The following fields were used as a Row Source to a list box (lstTotal):
poid: ID
Ct: # of entries
Qty: quantity for each order
Catid: Catagory ID
An example record:
poid: 3
Ct: 12
Qty: 1,2,1,3,1,2,1,1,5,1,3,1
Catid: 4167-G,7166-G,4169-H,6141WT-G,4170WT-G,T-2311-J,4493PS-H,7520-H,4483-E,4484-J,4487WT-H,2218-G
For record 3 there were 12 items ordered, the quantity for each item given in the Qty string, and the category id for each item given as Catid. In the example below the year is retrieved and the price is looked up from a separate table dependent on the year ordered. Finally a query is used to update the Total for the order using [poid] and [Total] as hidden textboxes on the form.
Code:
Dim i As Integer
Dim intct As Integer
Dim myct As Integer
Dim lngpoid As Long
Dim dteYr As Date
Dim ctlSource As ListBox
Dim intCurrentRow As Integer
Dim intqty As Integer
Dim strToProcess As String
Dim strqty As String
Dim strCat As String
Dim dblLookUp As Double
Private Sub Command0_Click()
DoCmd.SetWarnings False
Set ctlSource = Forms![frmTotal]![lstItems]
For intCurrentRow = 0 To ctlSource.ListCount - 1
[Total] = 0
[poid] = ctlSource.Column(0, intCurrentRow)
intct = ctlSource.Column(1, intCurrentRow)
strqty = ctlSource.Column(2, intCurrentRow)
strToProcess = ctlSource.Column(3, intCurrentRow)
dteYr = DLookup("DateOrdered", "tblChemOrder", "poid=" & [poid])
If Year(dteYr) = 2008 Then
For i = 0 To intct - 1
strCat = Split(strToProcess, ",")(i)
intqty = Split(strqty, ",")(i)
dblLookUp = DLookup("UC2008", "tblChemicals", "CatNumber='" & strCat & "'")
[Total] = [Total] + intqty * dblLookUp
Next i
ElseIf Year(dteYr) = 2007 Then
For i = 0 To intct - 1
strCat = Split(strToProcess, ",")(i)
intqty = Split(strqty, ",")(i)
dblLookUp = DLookup("UC2007", "tblChemicals", "CatNumber='" & strCat & "'")
[Total] = [Total] + intqty * dblLookUp
Next i
ElseIf Year(dteYr) = 2006 Then
For i = 0 To intct - 1
strCat = Split(strToProcess, ",")(i)
intqty = Split(strqty, ",")(i)
dblLookUp = DLookup("UC2006", "tblChemicals", "CatNumber='" & strCat & "'")
[Total] = [Total] + intqty * dblLookUp
Next i
End If
DoCmd.OpenQuery "qyUpdateTotal"
Next
DoCmd.SetWarnings True
End Sub
Not rocket science but thought it might be helpful for anyone looking to use the Split() function.