Not sure what you mean by "no average is calculated"
Are you sure that the value you need to be averaging is the third column of
your list box? (remember that the Column collection starts numbering at 0)
Are you sure that you've got the ColumnCount property of the list box set to
at least 3?
Are you sure you've got all of the periods in there correctly? Without the
With structure, the code would be
Private Sub cboNamen_AfterUpdate()
Dim dblLicenceFee As Double
Dim varSelected As Variant
dblLicenceFee = 0#
If Me![cboNamen].ItemsSelected.Count = 0 Then
MsgBox "No licence selected!", vbInformation
Else
For Each varSelected In Me![cboNamen].ItemsSelected
dblLicenceFee = dblLicenceFee + _
CDbl(Nz(Me![cboNamen].Column(2, varSelected), 0))
Next varSelected
Me![txtAvgLicence] = _
dblLicenceFee / Me![cboNamen].ItemsSelected.Count
End If
End Sub
--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
(no e-mails, please!)
"Ronald" <wit@yahoo> wrote in message
news:3606AB55-0CF6-4941-B3EE-(E-Mail Removed)...
> Hi Douglas.
>
> Thanks for your code! Any enhancements are always appreciated!
> But it does not function correctly, the result is always 0.
> In immediate mode the .ItemsSelected.Count does display the correct number
> of items selected. But after the For Each line is processed the code
> immediately goes to the 'Me![txtAvgLicence] = dblLicenceFee /
> .ItemsSelected.Count' line, so no average is calculated.
>
> Also my initial question still stands. Why do I get an 'Invalid use of
> Null'
> error while the table does not contain any empty fields? You use the Nz
> function to trap the error but then you only get a wrong answer and don't
> get
> an indication anything is wrong.
> Why when I press F5 (or F8) after the 'Invalid use of Null' error occurs,
> code continues without any error! If there really is a Null value the
> error
> should reappear!
> I have a database dedicated to this error to prove it.
>
> Any additional help or suggestion is very welcome.
>
> Ronald.
>
>
> "Douglas J. Steele" wrote:
>
>> The code's ok, but it could be better:
>>
>> Private Sub cboNamen_AfterUpdate()
>>
>> Dim dblLicenceFee As Double
>> Dim varSelected As Variant
>>
>> dblLicenceFee = 0#
>>
>> With Me![cboNamen]
>> If .ItemsSelected.Count = 0 Then
>> MsgBox "No licence selected!", vbInformation
>> Else
>> For Each varSelected In .ItemsSelected
>> dblLicenceFee = dblLicenceFee + CDbl(Nz(.Column(2, varSelected),
>> 0))
>> Next varSelected
>> Me![txtAvgLicence] = dblLicenceFee / .ItemsSelected.Count
>> End If
>> End With
>> 1
>> End Sub
>>
>>
>> --
>> Doug Steele, Microsoft Access MVP
>> http://www.AccessMVP.com/DJSteele
>> (no e-mails, please!)
>>
>> "Ronald" <wit@yahoo> wrote in message
>> news:F1999383-751D-4C0A-B538-(E-Mail Removed)...
>> > Hi all.
>> >
>> > This code:
>> >
>> > Private Sub cboNamen_AfterUpdate()
>> >
>> > Dim intRowCount As Integer
>> > Dim dblLicenceFee As Double
>> > Dim intSelectCount As Integer
>> >
>> > dblLicenceFee = 0#
>> > intSelectCount = 0
>> >
>> > For intRowCount = 0 To Me![cboNamen].Recordset.RecordCount - 1
>> > If (Me![cboNamen].Selected(intRowCount) = True) Then
>> > dblLicenceFee = dblLicenceFee + CDbl(Me![cboNamen].Column(2,
>> > intRowCount))
>> > intSelectCount = intSelectCount + 1
>> > End If
>> > Next intRowCount
>> >
>> > If (intSelectCount = 0) Then
>> > MsgBox "No licence selected!", vbInformation
>> > Else
>> > Me![txtAvgLicence] = dblLicenceFee / intSelectCount
>> > End If
>> >
>> > End Sub
>> >
>> > I put in the event you can see of a multi selectable combobox (Access
>> > 2007).
>> > The code is OK, all records are filled, but in certain specific
>> > circumstances (I know exactly when) I get an 'Invalid use of Null'
>> > error
>> > in
>> > the line that adds the LicenceFee. But when I go to error correction
>> > and
>> > press F5, the code continues and completes without any error.
>> > I tried requerying the control, build in a loop (in case of a timing
>> > issue),
>> > using other events, but I cannot get it fixed.
>> > I feel it might be a bug in Access. I have created a specific database
>> > to
>> > check the behavior. How can I get it to someone to confirm the behavior
>> > and
>> > maybe get it to Microsoft?
>> >
>> > Thanks,
>> >
>> > Ronald.
>> >
>>
>>
>> .
>>