By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Dynamic buttons on Access Form

P: 9
If any assistance can be provided would be appeciated.

I am attempting to populate a form with buttons with button captions which have been assigned through code.

buttons names are btn1, btn2, btn3...etc

The code indicates a query - Query1 which gives a dataset which is populated from the products table with product name and price.

The problem I have encountered is I am unable to change from btn1 to btn2 and so on from within the loop, can anyone shed any light on this.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.  
  4. Dim db As Database
  5. Dim qdf As QueryDef
  6. Dim prm As Parameter
  7. Dim rs As Recordset
  8.  
  9. Dim strProduct As String
  10. Dim strPrice As String
  11.  
  12. Set db = CurrentDb
  13. Set qdf = db.QueryDefs("Query1")
  14.  
  15.     For Each prm In qdf.Parameters
  16.             prm.Value = Eval(prm.Name)
  17.     Next prm
  18.  
  19. Set rs = qdf.OpenRecordset(dbOpenDynaset)
  20.  
  21.  
  22.             rs.MoveFirst
  23.  
  24.         Do Until rs.EOF
  25.  
  26.         strProduct = rs![Product]
  27.         strPrice = rs![Price]
  28.  
  29.         btn1.Caption = strProduct & " " & strPrice
  30.  
  31.             rs.MoveNext
  32.  
  33.         Loop
  34.  
  35. End Sub
  36.  
May 18 '07 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,347
Dim a variable to hold the button number, it will increment with each loop.

You can then use Me.Controls("btn" & IntegerVariable).Caption
May 18 '07 #2

P: 9
Thanks, that has worked a treat, simple really after you discover how it is done.
Cheers
May 18 '07 #3

Rabbit
Expert Mod 10K+
P: 12,347
Glad to help, good luck.
May 18 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.