Dynamic Button Captions

Posted on 2003-03-25
Medium Priority
Last Modified: 2010-05-01
I am building a database to keep an inventory of what computers we have in each office and what sort of computers.  So far, I have a form with a combo box which allows the user to select an office and a continuous subform which displays the network names of the computers in the selected office.  At the moment, I can use a text box to display each network name (each network name is a seperate record).  However, I am wanting the network name to appear on a button so that the user can press the button with the network name that they want to open a form with additional data.

I have the button created and at the moment have kept the textbox with the position name as I've a feeling it is needed.

The code that I have so far is

Private Sub CboLocation_AfterUpdate()
   ' Find the record that matches the control.
   Dim rs As Object

   Set rs = Me.Recordset.Clone
   rs.FindFirst "[ID] = " & Str(Me![CboLocation])
   Me.Bookmark = rs.Bookmark
   With Me.RecordsetClone.MoveFirst

   'Sets Focus on Textbox with the network name. Textbox is called Position
   If Not [Forms]![FrmMain]![sfrmPositions]!Position.Text = " " Then
   Do While Not RecordsetClone.EOF
   [Forms]![FrmMain]![sfrmPositions]!btnPosition.Caption = [Forms]![FrmMain]![sfrmPositions]!Position.Text
   While RecordsetClone.EOF
   [Forms]![FrmMain]![sfrmPositions]!btnPosition.Caption = " "
   [Forms]![FrmMain]![sfrmPositions]!btnPosition.Enabled = False
   End If
   End With
   End Sub

If there are no records for the office then this code will leave the button blank which is what I want. If there are records then the code will set the button caption for each record as the text in the position textbox for the first record.

I've a feeling I'm getting close to the answer but just missing something.
If anyone can help then that will be great.
Question by:gp15
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 8201971
to change a caption of a button

cmdThisButton.Caption = "INSERT YOUR CAPTION HERE"

Author Comment

ID: 8202060
The problem I'm having is more with the "INSERT YOUR CAPTION HERE" part of your answer.

I want this part to display results from a field on a continuous form. For example if I had a table for phones with columns Model, No.of Tones and Colour and had the following records

Model        Tones      Colour
-----        -----      ------
Nokia         10        black
Motorola      20        Blue
Vodafone      15        Red

Then I would want my continuous form to display each of these records with the model name as a button to open a new form.

So my form would look something like:

Item Type [Drop Down List - Phones]


|Nokia|  10   Black

|Motorola|  20   Blue

|Vodafone|  15   Red

I have tried making the field name a hyperlink but to the next form but this does not always work because the cursor for editing the field value appears when you click on it.

Expert Comment

ID: 8202084
so they aren't buttons but an item in a list?

to get the text like that

cmdThisButton.Caption = "|" & model & "| " & tones & " " & colour

to add them as an item in a list box

thisListBox.AddItem "|" & model & "| " & tones & " " & colour
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 8202179
Sorry, I think I confused you.

I was trying to do an example of how I wanted my screen to look but it didn't work cos the format changed.

What I was trying to "draw" was a form with a dropdown list (e.g. of many items such as phones, PC's, T.V.s, etc) and when the user selects an item from the list (e.g. Phones) then the appropriate records would be shown. The models (e.g. Nokia, Motorolla) were supposed to have boxes round them to show I want only the models as buttons. The other fields are just text boxes. I tried the code

cmdThisButton.Caption = "|" & model  

and this just displayed | in the button. I am only using VBA provided with Access2000 and do not have a copy of Visual Basic. Does this matter?

Expert Comment

ID: 8202205
ok - so as I read it - you need to manage an array of buttons and load the button captions - based on which item is selected in the combo box.

You mention leaving the buttons blank (this implies a known max number of buttons) or of course this coud be done by adding and deleting buttons as each 'category' demands.

I'm sure someone elese will beat me to the code, but why not think about a treeview, and handle the nodeclick events ?

If no-one else coes in here, and this /is/ what you are after, I'll drop something in for you.

Expert Comment

ID: 8203584
I also think a TreeView would be the right choice here... Loading dynamic buttons and positioning them would take more time and look ugly.

Author Comment

ID: 8203616
OK so how do you do treeview?

Expert Comment

ID: 8204670
TreeView displays items as an hierarchical list. E.g. Folders on the left of Windows Explorer. It has simple usage:

Node = Tree.Add([Relative], [Relationship], [Key], [Text], [Image], [Selected], [SelectedImage])

Node denotes the new node added to the tree.

Tree denotes the TreeView object.

Relative is the node in relation with the new added. If pass this argument the node will be created as a base node.

Relationship is the type of relation between the new and the relative node. For example to create child node you pass the tvwChild value.

Key is the unique key to identify the node. This value must be unique in the TreeView.

Text is the displayed text of the node.

Image is the index or key of an image in the TreeView's ImageList.

SelectedImage is the index or key of the image displayed when node is selected.

For example for your case, you may construct the tree as follows:

    Dim Eq As Node
    Dim Ph As Node
    Set Eq = Tree.Nodes.Add(, , "Eq", "Equipment")
    Set Ph = Tree.Nodes.Add(Eq, tvwChild, "Ph", "Phones")
    Tree.Nodes.Add Ph, tvwChild, "Nokia", "Nokia"
    Tree.Nodes.Add Ph, tvwChild, "Motorola", "Motorola"
    Tree.Nodes.Add Ph, tvwChild, "Vodafone", "Vodafone"
    Eq.Expanded = True
    Ph.Expanded = True

Expert Comment

ID: 8207066
Right about the ugly part.  But your original way for reference:

Basically you want to create a dynamic array of buttons.

Drop one button on your form in the first button position, let's call it txtButtonArray, set the index property to 0.

Then to add button #2,

     Load txtButtonArray(1)

then position it where you want it through code so,
    txtButtonArray(1).Top = txtButtonArray(0).Top + 200
    txtButtonArray(1).Left = txtButtonArray(0).Left

    txtButtonArray(1).Visible = True


Accepted Solution

skyDaemon earned 150 total points
ID: 8207072
Oh yeah,

And then set the captions the standard way:

txtButtonArray(0).Caption = "x"
txtButtonArray(1).Caption = "y"

Expert Comment

ID: 8207290
I think he has his 50 pts worth now !

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question