Solved Listbox

kavehrad

New member
Local time
Today, 03:29
Joined
Jun 14, 2020
Messages
17
Hi all
Can I have a list box in a form that looks like the attached image and I can search for records that begin with each letter?

Thanks
 

Attachments

  • Listbox.png
    Listbox.png
    6.8 KB · Views: 174
I don't think so.
However you could have 26 text boxes above your listbox with code to filter the list by first letter.
Or use a tab control with 26 tab pages
Or use cascading combo boxes
 
Use textboxes or buttons.
 
Set listbox RowSource property. Code in each button Click event:

Me.Listboxname.RowSource = "SELECT * FROM table WHERE somefield LIKE 'A*';"
 
Set listbox RowSource property. Code in each button Click event:

Me.Listboxname.RowSource = "SELECT * FROM table WHERE somefield LIKE 'A*';"
I have tried to do that but it doesn´t work. I attach my DB here. Maybe I did something wrong
 

Attachments

Heres an example. I only did up to letter f but it should give you the idea.
Note that I changed your table name and listbox name. I wouldnt use special characters in names and wouldn't name a listbox the same as a field.
 

Attachments

It just occurred to me after posting an easier way to code this.

There is a function as so
Code:
Private Function SetRowSource()

Me.lbxNamn.RowSource = "SELECT * FROM tblKund WHERE Namn LIKE """ & Screen.ActiveControl.Tag & "*"""

End Function
each button has a letter in its Tag property.

if you select all the buttons at once and type in the onlick event
=SetRowSource()
the code will run when you click the button

see this example
 

Attachments

@Tera

Just create a label containing the alphabet and use a fixed width font. (Name it 'lblAlpha')

Then use the code in the link:
Code:
Private Sub lblAlpha_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As  Single)
  Me.Filter = "fldLastName LIKE '" & Chr(64 - Int(-X / Me.lblAlpha.Width * 26)) & "*'"
  Me.FilterOn = True
End Sub
 
Last edited:
I just tried it. I never would have thought of it. Its a keeper for someday.
 
@kavehrad

See if this does what you want.
If you start clicking from A, your sample file works until letter H
The letter I doesn't filter the listbox.
From J it filters the previous letter. J filters I, K shows J etc.

Once this happens, nothing filters correctly anymore.

This is what I receive after clicking Y.

2020-06-15_10-07-57.jpg
 
Last edited:
What font are you using? It must be a Fixed-Width font.

Make sure the label width doesn't have any space after the 'Z'
 
What font are you using? It must be a Fixed-Width font.

Make sure the label width doesn't have any space after the 'Z'
I didn't use any font. I simply opened your form in sample file attached in #17

Edit : to be sure, I opened the form in design view and changed the font to Courier New. Still the same.
 
I used a font called Consolas.

I guess you don't have it installed (The one in your screenshot is different).

Pick a fixed-width font.

Remember to size the label to fit.
 

Users who are viewing this thread

Back
Top Bottom