Button Values

P

Pieter

Hi, please help.

I'm new to programming. I have a form with a standard amount of buttons
created on it (40 buttons). I would like the caption property of the buttons
to be setup when the form is loaded. The caption values will be captured in a
table and will change on a monthly base. Also, if there are only 37 or 21
value fields in the table, only the first 37 or 21 buttons should be visible.
I would also help if the buttons are alphabetically displayed.

Could anyone help? I'm in the dark
 
B

banem2

Hi, please help.

I'm new to programming. I have a form with a standard amount of buttons
created on it (40 buttons). I would like the caption property of the buttons
to be setup when the form is loaded. The caption values will be captured in a
table and will change on a monthly base. Also, if there are only 37 or 21
value fields in the table, only the first 37 or 21 buttons should be visible.
I would also help if the buttons are alphabetically displayed.

Could anyone help? I'm in the dark  

Button caption is set with: cmdButton.Caption = "&Button Name".
Ampersand is used for keyboard shortcut, in this case ALT+B.

I will create recordset with SQL to sort table data and loop through
recordset while writing button captions. Something like this (you need
DAO)

Private Sub Form_Load()
Dim ctl As Control, intButtonID As Integer
On Error Resume Next

'Disable all buttons
For Each ctl In Me.Controls
If ctl.Type = acCommandButton Then
ctl.Enabled = False
End If
Next

'Create labels
intButtonID = 1
For Each ctl In Me.Controls
If ctl.Type = acCommandButton Then
ctl.Caption = fGetCaption(intButtonID)
intButtonID = intButtonID + 1
End If
Next
End Sub

Function fGetCaption(intButtonID)
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * FROM myTable ORDER BY
myField")
If rst.RecordCount > 0 Then
Do Until rst.EOF
If rst.AbsolutePosition + 1 = intButtonID Then
fGetCaption = rst!myField
rst.Close
Exit Function
End If
rst.MoveNext
Loop
End If
End Function

Regards,
Branislav Mihaljev, Microsoft Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top