Using a variable for a listbox name

S

Sajit

I want to select between 2 list boxe content depending on which had received
the focus the last time. I am not able to use the variable name, last_sel
within the button click sub. How do I do this?

Option Compare Database
Public last_sel As String


Private Sub Command6_Click()

b = last_sel
If IsNull(Me!Text4) Then
Me!Text4 = Me!last_sel
Else
Me!Text4 = Me!Text4 & ", " & Me!last_sel
End If

End Sub

Private Sub List2_GotFocus()
last_sel = List2.Name
End Sub

Private Sub List7_GotFocus()
last_sel = List7.Name
End Sub
 
R

romain alim

Sajit said:
I want to select between 2 list boxe content depending on which had
received
the focus the last time. I am not able to use the variable name, last_sel
within the button click sub. How do I do this?

Option Compare Database
Public last_sel As String


Private Sub Command6_Click()

b = last_sel
If IsNull(Me!Text4) Then
Me!Text4 = Me!last_sel
Else
Me!Text4 = Me!Text4 & ", " & Me!last_sel
End If

End Sub

Private Sub List2_GotFocus()
last_sel = List2.Name
End Sub

Private Sub List7_GotFocus()
last_sel = List7.Name
End Sub
 
B

Bob Quintal

I want to select between 2 list boxe content depending on which
had received the focus the last time. I am not able to use the
variable name, last_sel within the button click sub. How do I do
this?

Option Compare Database
Public last_sel As String


Private Sub Command6_Click()

b = last_sel
If IsNull(Me!Text4) Then
Me!Text4 = Me!last_sel
Else
Me!Text4 = Me!Text4 & ", " & Me!last_sel
End If

End Sub

Private Sub List2_GotFocus()
last_sel = List2.Name
End Sub

Private Sub List7_GotFocus()
last_sel = List7.Name
End Sub

the Me! prefix cannot point to a variable, only an object such as a
textbox or command button.
Me!Text4 = last_sel
should work
 
S

Sajit

Thanks Bob,

Only that last_sel is only the name of the listbox. How do I point it to the
value selected in the list box. The equivalent of me!list2 or me!list7

I did this,

If last_sel = "List2" Then
If IsNull(Me!Text4) Then
Me!Text4 = Me!List2
Else
Me!Text4 = Me!Text4 & ", " & Me!List2
End If
Else
If IsNull(Me!Text4) Then
Me!Text4 = Me!List7
Else
Me!Text4 = Me!Text4 & ", " & Me!List7
End If
End If

But there must be a better way of refering to it?

Sajit
 
M

Marshall Barton

Sajit said:
I want to select between 2 list boxe content depending on which had received
the focus the last time. I am not able to use the variable name, last_sel
within the button click sub. How do I do this?

Option Compare Database
Public last_sel As String


Private Sub Command6_Click()

b = last_sel
If IsNull(Me!Text4) Then
Me!Text4 = Me!last_sel
Else
Me!Text4 = Me!Text4 & ", " & Me!last_sel
End If

End Sub

Private Sub List2_GotFocus()
last_sel = List2.Name
End Sub

Private Sub List7_GotFocus()
last_sel = List7.Name
End Sub


It's not clear what you are trying to do. Are you trying to
build a list of the list box names or the values selected in
the list boxes?

Bob covered the former case, but for the latter case use
this kind of syntax:

Me!Text4 = Me(last_sel)
or, more rigorously:
Me!Text4 = Me.Controls(last_sel)
 
S

Sajit

Thanks Marshall,

That is what I was looking for.
Wonder why it does not say that anywhere in the Access help.

Sajit
 
B

Bob Quintal

Thanks Bob,

Only that last_sel is only the name of the listbox. How do I point
it to the value selected in the list box. The equivalent of
me!list2 or me!list7

I did this,

If last_sel = "List2" Then
If IsNull(Me!Text4) Then
Me!Text4 = Me!List2
Else
Me!Text4 = Me!Text4 & ", " & Me!List2
End If
Else
If IsNull(Me!Text4) Then
Me!Text4 = Me!List7
Else
Me!Text4 = Me!Text4 & ", " & Me!List7
End If
End If

But there must be a better way of refering to it?

Sajit

Sorry, you are right. I would use the afterUpdate event of each
listbox to set the selected value to a variable, instead of the focus
event you used to store the name to the variable.

However, you could do Me!Text4 = Me.controls(last_sel)
as this will return the alue of the control in the variable.
 
M

Marshall Barton

Sajit said:
That is what I was looking for.
Wonder why it does not say that anywhere in the Access help.

Be careful about saying things like "does not say that
anywhere in the Access help". Just about everything is
explained somewhere in either the Access or VBA Help files,
the hurdle you have to leap is finding the explanation.

This particular topic is explained in the VBA Help topic
Controls Collection, which is a sensible place for the
explanation once you understand the Access Object Model.
 

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