gp15
asked on
Dynamic Button Captions
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
[Forms]![FrmMain]![sfrmPos itions].Se tFocus
With Me.RecordsetClone.MoveFirs t
'Sets Focus on Textbox with the network name. Textbox is called Position
[Forms]![FrmMain]![sfrmPos itions]!Po sition.Set Focus
If Not [Forms]![FrmMain]![sfrmPos itions]!Po sition.Tex t = " " Then
Do While Not RecordsetClone.EOF
[Forms]![FrmMain]![sfrmPos itions]!bt nPosition. Caption = [Forms]![FrmMain]![sfrmPos itions]!Po sition.Tex t
RecordsetClone.MoveNext
Loop
Else
While RecordsetClone.EOF
[Forms]![FrmMain]![sfrmPos itions]!bt nPosition. Caption = " "
[Forms]![FrmMain]![sfrmPos itions]!bt nPosition. Enabled = False
Wend
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.
Thanks
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
[Forms]![FrmMain]![sfrmPos
With Me.RecordsetClone.MoveFirs
'Sets Focus on Textbox with the network name. Textbox is called Position
[Forms]![FrmMain]![sfrmPos
If Not [Forms]![FrmMain]![sfrmPos
Do While Not RecordsetClone.EOF
[Forms]![FrmMain]![sfrmPos
RecordsetClone.MoveNext
Loop
Else
While RecordsetClone.EOF
[Forms]![FrmMain]![sfrmPos
[Forms]![FrmMain]![sfrmPos
Wend
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.
Thanks
ASKER
Hazgod,
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]
Buttons
|
v
______
|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.
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]
Buttons
|
v
______
|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.
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
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
ASKER
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?
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?
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.
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.
I also think a TreeView would be the right choice here... Loading dynamic buttons and positioning them would take more time and look ugly.
ASKER
OK so how do you do treeview?
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
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think he has his 50 pts worth now !
cmdThisButton.Caption = "INSERT YOUR CAPTION HERE"