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
  • 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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.


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

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

607 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