Getting a value from a listbox

T

takatam

Hi,

I have a listbox which contains 1 column for which the values a
populated from a query. The query returns only 1 row hence my listbox
is basically a 1x1 matrix.

I would now like to use the value of this cell to change the
background color of the listbox. But for some reason I am not able to
find the right way to access this value.

I tried the following:

Private Sub List65_AfterUpdate()
If Me.List65.Value = 1 Then
Me.List65.BackColor = 255
Else
Me.List65.BackColor = 4259584
End If
End Sub

Should I maybe be using the .ItemData property? While I can get the
top row using .ItemData(0), this will still return the whole row and
so I'm not sure how to then get the first element out of that.
I also tried the property .Column(0,0) but without success.

The suspicious thing is that when I set a breakpoint on the first line
of the subroutine, the vba does not stop there, indicating that it is
actually not executed. Just to rule out problems of other origin,
here's the (automatic) code for my Refresh-button:

Private Sub ButtonRefresh_Click()
On Error GoTo Err_ButtonRefresh_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70

Exit_ButtonRefresh_Click:
Exit Sub

Err_ButtonRefresh_Click:
MsgBox Err.Description
Resume Exit_ButtonRefresh_Click

End Sub

Any help is appreciated.

Thanks a lot,
T
 
D

Douglas J. Steele

You say that it appears that the event procedure doesn't actually appear to
be executing. Might the code have become disassociated from the event?

With the form open in design view, select the list box and look at the
Properties window. Does it say [Event Procedure] as the AfterUpdate
property? If so, when you click on the ellipsis (...) to the right, does it
take you into that code?

Incidentally, you'd be well advised not to use the DoMenuItem approach. All
you need is

Me.Refresh
 
T

takatam

Hi Douglas,

The association from the event to the code is given, I do arrive in
the right subroutine when I click on the ellipsis.

As far as the DoMenuItem approach is concerned, this is the code which
was automatically generated by access when I placed the button. I just
tried the Me.Refresh approach. While it works equally well (first
sight), I still don't arrive at the breakpoint set within the List65
subroutine.

Any other ideas what could be amiss?

Thanks,
Thomas


You say that it appears that the event procedure doesn't actually appear to
be executing. Might the code have become disassociated from the event?

With the form open in design view, select the list box and look at the
Properties window. Does it say [Event Procedure] as the AfterUpdate
property? If so, when you click on the ellipsis (...) to the right, does it
take you into that code?

Incidentally, you'd be well advised not to use the DoMenuItem approach. All
you need is

Me.Refresh

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)


I have a listbox which contains 1 column for which the values a
populated from a query. The query returns only 1 row hence my listbox
is basically a 1x1 matrix.
I would now like to use the value of this cell to change the
background color of the listbox. But for some reason I am not able to
find the right way to access this value.
I tried the following:
Private Sub List65_AfterUpdate()
If Me.List65.Value = 1 Then
Me.List65.BackColor = 255
Else
Me.List65.BackColor = 4259584
End If
End Sub
Should I maybe be using the .ItemData property? While I can get the
top row using .ItemData(0), this will still return the whole row and
so I'm not sure how to then get the first element out of that.
I also tried the property .Column(0,0) but without success.
The suspicious thing is that when I set a breakpoint on the first line
of the subroutine, the vba does not stop there, indicating that it is
actually not executed. Just to rule out problems of other origin,
here's the (automatic) code for my Refresh-button:
Private Sub ButtonRefresh_Click()
On Error GoTo Err_ButtonRefresh_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
Exit_ButtonRefresh_Click:
Exit Sub
Err_ButtonRefresh_Click:
MsgBox Err.Description
Resume Exit_ButtonRefresh_Click
Any help is appreciated.
Thanks a lot,
T
 
D

Douglas J. Steele

You do realize that the AfterUpdate event only fires when you select a value
from the list box using either the mouse or keyboard? If you select a value
through code, for instance, the AfterUpdate event does not fire.

You may need to put your code in another event if you're not actually
clicking on the list box.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


takatam said:
Hi Douglas,

The association from the event to the code is given, I do arrive in
the right subroutine when I click on the ellipsis.

As far as the DoMenuItem approach is concerned, this is the code which
was automatically generated by access when I placed the button. I just
tried the Me.Refresh approach. While it works equally well (first
sight), I still don't arrive at the breakpoint set within the List65
subroutine.

Any other ideas what could be amiss?

Thanks,
Thomas


You say that it appears that the event procedure doesn't actually appear
to
be executing. Might the code have become disassociated from the event?

With the form open in design view, select the list box and look at the
Properties window. Does it say [Event Procedure] as the AfterUpdate
property? If so, when you click on the ellipsis (...) to the right, does
it
take you into that code?

Incidentally, you'd be well advised not to use the DoMenuItem approach.
All
you need is

Me.Refresh

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)


I have a listbox which contains 1 column for which the values a
populated from a query. The query returns only 1 row hence my listbox
is basically a 1x1 matrix.
I would now like to use the value of this cell to change the
background color of the listbox. But for some reason I am not able to
find the right way to access this value.
I tried the following:
Private Sub List65_AfterUpdate()
If Me.List65.Value = 1 Then
Me.List65.BackColor = 255
Else
Me.List65.BackColor = 4259584
End If
End Sub
Should I maybe be using the .ItemData property? While I can get the
top row using .ItemData(0), this will still return the whole row and
so I'm not sure how to then get the first element out of that.
I also tried the property .Column(0,0) but without success.
The suspicious thing is that when I set a breakpoint on the first line
of the subroutine, the vba does not stop there, indicating that it is
actually not executed. Just to rule out problems of other origin,
here's the (automatic) code for my Refresh-button:
Private Sub ButtonRefresh_Click()
On Error GoTo Err_ButtonRefresh_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
Exit_ButtonRefresh_Click:
Exit Sub
Err_ButtonRefresh_Click:
MsgBox Err.Description
Resume Exit_ButtonRefresh_Click
Any help is appreciated.
Thanks a lot,
T
 
T

takatam

Hi Douglas,

No, I was actually not aware of this, I expected the refresh to
trigger this event.

So which event should I be using then for this?

Thanks a lot,
T

You do realize that the AfterUpdate event only fires when you select a value
from the list box using either the mouse or keyboard? If you select a value
through code, for instance, the AfterUpdate event does not fire.

You may need to put your code in another event if you're not actually
clicking on the list box.

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)


Hi Douglas,
The association from the event to the code is given, I do arrive in
the right subroutine when I click on the ellipsis.
As far as the DoMenuItem approach is concerned, this is the code which
was automatically generated by access when I placed the button. I just
tried the Me.Refresh approach. While it works equally well (first
sight), I still don't arrive at the breakpoint set within the List65
subroutine.
Any other ideas what could be amiss?
Thanks,
Thomas
You say that it appears that the event procedure doesn't actually appear
to
be executing. Might the code have become disassociated from the event?
With the form open in design view, select the list box and look at the
Properties window. Does it say [Event Procedure] as the AfterUpdate
property? If so, when you click on the ellipsis (...) to the right, does
it
take you into that code?
Incidentally, you'd be well advised not to use the DoMenuItem approach.
All
you need is
Me.Refresh
--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)

Hi,
I have a listbox which contains 1 column for which the values a
populated from a query. The query returns only 1 row hence my listbox
is basically a 1x1 matrix.
I would now like to use the value of this cell to change the
background color of the listbox. But for some reason I am not able to
find the right way to access this value.
I tried the following:
Private Sub List65_AfterUpdate()
If Me.List65.Value = 1 Then
Me.List65.BackColor = 255
Else
Me.List65.BackColor = 4259584
End If
End Sub
Should I maybe be using the .ItemData property? While I can get the
top row using .ItemData(0), this will still return the whole row and
so I'm not sure how to then get the first element out of that.
I also tried the property .Column(0,0) but without success.
The suspicious thing is that when I set a breakpoint on the first line
of the subroutine, the vba does not stop there, indicating that it is
actually not executed. Just to rule out problems of other origin,
here's the (automatic) code for my Refresh-button:
Private Sub ButtonRefresh_Click()
On Error GoTo Err_ButtonRefresh_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
Exit_ButtonRefresh_Click:
Exit Sub
Err_ButtonRefresh_Click:
MsgBox Err.Description
Resume Exit_ButtonRefresh_Click
End Sub
Any help is appreciated.
Thanks a lot,
T
 
T

takatam

Hi,

just found the solution. I'm placing the code into the Button_Refresh
routine, then it's taken care of.

Thanks for pointing it out,
T


Hi Douglas,

No, I was actually not aware of this, I expected the refresh to
trigger this event.

So which event should I be using then for this?

Thanks a lot,
T

You do realize that the AfterUpdate event only fires when you select a value
from the list box using either the mouse or keyboard? If you select a value
through code, for instance, the AfterUpdate event does not fire.
You may need to put your code in another event if you're not actually
clicking on the list box.
news:30bbe9fc-01dd-49c9-948d-22967e9dbc49@k13g2000hse.googlegroups.com...
Hi Douglas,
The association from the event to the code is given, I do arrive in
the right subroutine when I click on the ellipsis.
As far as the DoMenuItem approach is concerned, this is the code which
was automatically generated by access when I placed the button. I just
tried the Me.Refresh approach. While it works equally well (first
sight), I still don't arrive at the breakpoint set within the List65
subroutine.
Any other ideas what could be amiss?
Thanks,
Thomas
On 18 Aug., 16:19, "Douglas J. Steele"
You say that it appears that the event procedure doesn't actually appear
to
be executing. Might the code have become disassociated from the event?
With the form open in design view, select the list box and look at the
Properties window. Does it say [Event Procedure] as the AfterUpdate
property? If so, when you click on the ellipsis (...) to the right, does
it
take you into that code?
Incidentally, you'd be well advised not to use the DoMenuItem approach.
All
you need is
Me.Refresh
--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)

Hi,
I have a listbox which contains 1 column for which the values a
populated from a query. The query returns only 1 row hence my listbox
is basically a 1x1 matrix.
I would now like to use the value of this cell to change the
background color of the listbox. But for some reason I am not able to
find the right way to access this value.
I tried the following:
Private Sub List65_AfterUpdate()
If Me.List65.Value = 1 Then
Me.List65.BackColor = 255
Else
Me.List65.BackColor = 4259584
End If
End Sub
Should I maybe be using the .ItemData property? While I can get the
top row using .ItemData(0), this will still return the whole row and
so I'm not sure how to then get the first element out of that.
I also tried the property .Column(0,0) but without success.
The suspicious thing is that when I set a breakpoint on the first line
of the subroutine, the vba does not stop there, indicating that it is
actually not executed. Just to rule out problems of other origin,
here's the (automatic) code for my Refresh-button:
Private Sub ButtonRefresh_Click()
On Error GoTo Err_ButtonRefresh_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
Exit_ButtonRefresh_Click:
Exit Sub
Err_ButtonRefresh_Click:
MsgBox Err.Description
Resume Exit_ButtonRefresh_Click
End Sub
Any help is appreciated.
Thanks a lot,
T
 

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

Top