I have a simple set up here a table Table2 with three Fields:
1) ID (PK)
2) CustomerName (Text)
3) ProductName (Text)
Below is a function that I have used to find out the total number of products actually bought by a customer.
I have used a DAO Recordset here opened it and looped through it. MySQL is the used to set the criteria of records to be opened. Once the recordset is opened using a loop and a counter we find out the total number of products the customer has bought.
Function syntax: ShopTotal("CustomerName")
Code:
Dim strCustomerName As String
Dim MyRecordSet As DAO.Recordset
Dim MySQL As String
Dim intCounter As Integer
Function ShopTotal(strCustomerName) As Integer
If Len(strCustomerName) > 0 Then
MySQL = "Select * From Table2 Where CustomerName='" & strCustomerName & "'"
Set MyRecordSet = CurrentDb.OpenRecordset(MySQL)
Do While Not MyRecordSet.EOF
intCounter = intCounter + 1
MyRecordSet.MoveNext
Loop
MyRecordSet.Close
Set MyRecordSet = Nothing
Else
Exit Function
End If
ShopTotal = intCounter
intCounter = 0
End Function