Describing an empty list box

grenee

Registered User.
Local time
Today, 03:11
Joined
Mar 5, 2012
Messages
212
Good Day All,

I want to reference an empty listbox. I tried "" and Null but none of them works. The code is below. Both are ignored when this code is applied as an event and the listbox is empty. An error message comes up suggesting "invalid use of null"

Can this please be explained and corrected.


Code:
Private Sub Command0_Click()
Dim StartYear As String
 
 If Me.List3.Value = "" Then MsgBox "George"
 If Me.List3.Value = Null Then MsgBox "George"
 
  StartYear = Me.List3.Value
  DoCmd.OpenReport "Application Registration Graduation and Dropouts", acViewReport, , "[Year] =" & "'" & StartYear & "'"
 
 End Sub
 
A majority of the time, in Access, 'empty' Controls are actually Null, not Zero-Length Strings (""), so your first test is is not likely to trigger the Messagebox. To test for Null, in VBA, would be

If IsNull(Me.List3.Value) Then MsgBox "George"

There are a number of ways to test for both ZLSs and Nulls, one of which is

If Nz(Me.List3.Value, "") = "" Then MsgBox "George"

Linq ;0)>
 
If it is actually a list box as opposed to a text box AND the list box does not have a default value that IS in the list of possibilities then to determine that nothing has been selected you can test

IF {list-box-name}.Selected.Count = 0

The list-box can be single-select or multi-select, won't make a difference for my count test.

If you have a default value, I'm not sure how to tell the difference between a selected value that happens to be the default and the value asserted because the list box took the default.

If this had been done with a Combo Box, there is also a wrinkle to consider with the Not In List event and option, but that doesn't appear to apply to a list box.

In the case that the list-box is single-select, you can also test .ListIndex to be -1 if nothing has been selected.
 
Thanks for the explanations. They work fine.
 

Users who are viewing this thread

Back
Top Bottom