ABCDEFGHI....Command Box's Lookup

I need to have an Alphabetical Lookup Routine with
a Bar accross the top of the page.
A B C D    etc

The lookup up is a Access Database

I realise i can have a combo or list box doing this, and already have this working ok., but, i would also like to have the above as well.

Is there an easy way of doing this, without manually generating 26 command buttons  etc....

And also, having a common routine  eg (AlphaClicked), which will know which Button has been clicked, and then do the lookup.

Would appreciate some code to do the above.

Hope someone can give me some assistance.
Who is Participating?
GebhartBobConnect With a Mentor Commented:
    Here's a neat solution to your problem:

     1. Put a single command button on your, a little tiny one, with just the letter "A" on it. Put it exactly where you want the button to appear in the finished product. Set font, font size, bold, etc. for this "A" button, however you want all the buttons to look. The "A" button is the model for all 26 buttons. Call the button "cmdLetter" and set Index to 0, so it'll be considered an array by VB.

     2. Now write this simple little function and call it from Form_Load, setting LETTER_SPACING to the distance you want between buttons:

Private Sub createLetterButtons_Click()
   Const LETTER_SPACING As Integer = 100
   Dim i As Integer
   Dim leftPos As Integer

   leftPos = cmdLetter(0).left
   For i = 1 To 25
      leftPos = leftPos + cmdLetter(0).width + LETTER_SPACING
      Load cmdLetter(cmdLetter.UBound + 1)
      With cmdLetter(cmdLetter.UBound)
         .Caption = Chr(i + 65)
         .left = leftPos
         .Visible = True
      End With
   Next i
End Sub

     The command buttons will appear, all 26 of them, no problem. To respond to a user's selection of one of them, this is all you need:

Private Sub cmdLetter_Click(index As Integer)
   MsgBox "You clicked: " & cmdLetter(index).Caption
End Sub
Use a toolbar


For a common routine use 26 command buttons in an array and use the .caption parameter to instigate the source of your query.


You can accomplish this by doing the following:

1) Add a button to your form
2) Name the button "cmdLetter" (without the quotes)
3) Set the caption of this new button to 'A'
4) Resize the button as desired
5) Position this button where you want it to be
6) Set the Index property of this button to 0 (zero)
7) Add the following line of code to your form's Load Event:


8) Add the following Subroutine to your form:

Private Sub SetUpLetterButtons()
   Dim idx   As Integer
   Dim Btn   As Control
   Dim Width As Single
   Dim Left  As Single

   With cmdLetter(0)
      Left = .Left
      Width = .Width
   End With

   For idx = 1 To 25
      Load cmdLetter(idx)
      With cmdLetter(idx)
         .Move Left + (idx * Width)
         .Caption = Chr(64 + idx)
         .Visible = True
      End With
   Next 'idx
End Sub

9) Add the following subroutine to your form:

Private Sub UnloadLetterButtons()
   Dim idx As Integer

   For idx = 1 To cmdLetter.UBound
      Unload cmdLetter(idx)
   Next 'idx
End Sub

10) Add the following line of code to your Form's Unload Event:


-Dennis Borg
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Small correction:

      >.Caption = Chr(64 + idx)

This really should read:

      .Caption = Chr(65 + idx)

-Dennis Borg
Manually generating 26 command buttons  is not big problem - if you have one control with index=0, you don't have to press 'Paste' 25 times, like:

    aaaaaaaaaaaaaaaaaaaaaaaaaa (I pressed 'a' only once :))

To position controls horizontally, use menu Format, Horizontal Spacing, Remove.
kennedymrAuthor Commented:
Appreciate all the helpfull comments, very much.
Appreciate the help from dennisborg,also,this method works fine, as well.
Allways hard to work out who to give points with several working examples.!!!

Thanks again   Kennedymr
I don't understand ... GebhartBob's solution was exactly the same as mine.
For DennisBorg---

     My solution was indeed exactly the same as yours, but I promise, it was arrived at completely independently. I didn't even know you had responded when I posted my answer. If there's a way to split the points, I'd be glad to.


>but I promise, it was arrived at completely independently.

I am certain it was; I wasn't accusing you of cheating.

I was just surprised at the selected answer.

>If there's a way to split the points, I'd be glad to.

Nah, that's not necessary. I appreciate the offer though. I'm not really in it for the points.

It's not really a big deal to me, and I don't think it was intentional. I just don't understand why.

-Dennis Borg
kennedymrAuthor Commented:
Sorry to get in this position.
I did not notice the comments were so close in content.
How am i able to give some points to Dennis

Regards  Kennedymr
>Sorry to get in this position.
>I did not notice the comments were so close in content.
>How am i able to give some points to Dennis

Kennedy, don't worry about it. As I had said in my last post, it's not the points I was thinking about.

I just did not understand why it happened that way. I do not believe it was intentional on your part.


-Dennis Borg
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.