listbox control - why is it null???

  • Thread starter Thread starter Rick A
  • Start date Start date
R

Rick A

I have this code...
If Me!lstQuickEntry.ItemsSelected.Count > 0 Then
Set ctl = Me!lstQuickEntry
... (there's a lot more but I left it out because I don't think it is needed)


The listbox - lstQuickEntry - is populated and the first row is selected upon form open.

If I execute the code after the form opens the "ctl" is set to null. At least that's what the code is saying.

If I click on the listbox before I execute the code "ctl" functions as expected.

Why? Why must I click on the listbox to eliminate "null"? I'm missing something simple I'm sure..

Access 2K and beyond. Right now I'm on Access 2003.

Thanks,
 
Hi Rick,

I use the following to inspect the values in a particular column and row,
then SET the Selected property for the row to highlight it. With a listbox
you must loop through all the values to find the one that is selected.

If you have a look at the code it will give you an idea on how to proceed
with what you want to do. One thing to remember is that the column name may
appear as the first value found in Nz(ctl.Column(0, varItem), 0), you will
need to loop until you find the value you need. Use a break point at Next to
see what each row returns.

One thing to remember, I have found it necessary to actually select the
first row of a listbox on form open on occassion.

<snip>
' Inspect all the rows in the listbox to find a
' match for the IDInvoice passed
For varItem = 0 To ctl.ListCount
If IDInvoice = Nz(ctl.Column(0, varItem), 0)
Then
ctl.Selected(varItem) = True
lngIDInvoice = ctl.Column(0, varItem)
Else
ctl.Selected(varItem) = False
End If
Next



I have this code...
If Me!lstQuickEntry.ItemsSelected.Count > 0 Then
Set ctl = Me!lstQuickEntry
... (there's a lot more but I left it out because I don't think it is
needed)


The listbox - lstQuickEntry - is populated and the first row is selected
upon form open.

If I execute the code after the form opens the "ctl" is set to null. At
least that's what the code is saying.

If I click on the listbox before I execute the code "ctl" functions as
expected.

Why? Why must I click on the listbox to eliminate "null"? I'm missing
something simple I'm sure..

Access 2K and beyond. Right now I'm on Access 2003.

Thanks,
 
Are you correctly selecting the first row upon the form opening? Something like Me.lstQuickEntry.Selected(0)=True?

Sam
I have this code...
If Me!lstQuickEntry.ItemsSelected.Count > 0 Then
Set ctl = Me!lstQuickEntry
... (there's a lot more but I left it out because I don't think it is needed)


The listbox - lstQuickEntry - is populated and the first row is selected upon form open.

If I execute the code after the form opens the "ctl" is set to null. At least that's what the code is saying.

If I click on the listbox before I execute the code "ctl" functions as expected.

Why? Why must I click on the listbox to eliminate "null"? I'm missing something simple I'm sure..

Access 2K and beyond. Right now I'm on Access 2003.

Thanks,
 
Yes, I think I've set it correctly - Here's the code

If Me!lstDogEntry.ListCount > 1 Then
Me!lstDogEntry.Selected(1) = True
Me!lstDogEntry.SetFocus
Else
Me!lstQuickEntry.Selected(0) = True ' <-------
Me!lstQuickEntry.SetFocus
End If

What were you thinking?

--
Rick

Are you correctly selecting the first row upon the form opening? Something
like Me.lstQuickEntry.Selected(0)=True?

Sam
I have this code...
If Me!lstQuickEntry.ItemsSelected.Count > 0 Then
Set ctl = Me!lstQuickEntry
... (there's a lot more but I left it out because I don't think it is
needed)


The listbox - lstQuickEntry - is populated and the first row is selected
upon form open.

If I execute the code after the form opens the "ctl" is set to null. At
least that's what the code is saying.

If I click on the listbox before I execute the code "ctl" functions as
expected.

Why? Why must I click on the listbox to eliminate "null"? I'm missing
something simple I'm sure..

Access 2K and beyond. Right now I'm on Access 2003.

Thanks,
 
Terry,

Thanks. I have something similar to what you suggested.

The issue is the fact that the listbox is null until I click on it even when
I run the code below. Why?

Here's the code that I run on form open.

Me!lstQuickEntry.Selected(0) = True
Me!lstQuickEntry.SetFocus

When I do
set ctl = me!lstQuickEntry without first selecting the control a quick watch
of ctl shows null
if I first click on the listbox ctl shows a value.

The listbox is set to Extended. I've tried simple too. In case this helps.
 
I don't understand your logic here. Currently the first row in lstQuickEntry
is selected only when lstDogEntry is empty (i.e. its ListCount=0).

Assuming lstQuickEntry has data why not just select its first row outside
your IF block?

Sam
 
Sam,

What I'm struggling with is why when I set a control variable to the listbox
the "set" returns null until I click on the listbox.

dim ctl as control
set ctl = me!lstQuickEntry

this results in ctl = null until I click on the listbox. Then it works. I
figured I missed something easy.

I'm okay with how to select items in the listbox.

I don't know how else to explain my question.

--
Rick Allison
Sam D said:
I don't understand your logic here. Currently the first row in
lstQuickEntry is selected only when lstDogEntry is empty (i.e. its
ListCount=0).

Assuming lstQuickEntry has data why not just select its first row outside
your IF block?

Sam
 
I'm suggesting that your code is not actually causing a row to be initially
selected in lstQuickEntry, hence the Null value. Looking at your code it
appears this is the case unless lstDogEntry has no data.

Sam
Rick A said:
Sam,

What I'm struggling with is why when I set a control variable to the
listbox the "set" returns null until I click on the listbox.

dim ctl as control
set ctl = me!lstQuickEntry

this results in ctl = null until I click on the listbox. Then it works.
I figured I missed something easy.

I'm okay with how to select items in the listbox.

I don't know how else to explain my question.
 
What's the Multiselect property set to? If it's not None, then you can't
simply refer to the control to determine the value of the selected row.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


I have this code...
If Me!lstQuickEntry.ItemsSelected.Count > 0 Then
Set ctl = Me!lstQuickEntry
... (there's a lot more but I left it out because I don't think it is
needed)


The listbox - lstQuickEntry - is populated and the first row is selected
upon form open.

If I execute the code after the form opens the "ctl" is set to null. At
least that's what the code is saying.

If I click on the listbox before I execute the code "ctl" functions as
expected.

Why? Why must I click on the listbox to eliminate "null"? I'm missing
something simple I'm sure..

Access 2K and beyond. Right now I'm on Access 2003.

Thanks,
 
Rick A said:
Terry,

Thanks. I have something similar to what you suggested.

The issue is the fact that the listbox is null until I click on it
even when I run the code below. Why?

Here's the code that I run on form open.

Me!lstQuickEntry.Selected(0) = True

Instead of that, since this must be a single-select list box, try this:

With Me!lstQuickEntry
.Value = .ItemData(Abs(.ColumnHeads))
End With

Is this list box bound to a field, or is it unbound? If it's bound, the
form's Open event may be too early to get/set its value. In that case,
try the form's Load event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top