By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 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+
2 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
You have posted in the Articles section.

I am moving this question to the Access forum.

ADMIN
May 18 '07 #2

JConsulting
Expert 100+
P: 603
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.  

how about

Expand|Select|Wrap|Line Numbers
  1. Dim db As DataBase
  2. Dim qdf As QueryDef
  3. Dim prm As Parameter
  4. Dim rs As Recordset
  5. Dim I As Long
  6. Dim strProduct As String
  7. Dim strPrice As String
  8.  
  9. Set db = CurrentDb
  10. Set qdf = db.QueryDefs("Query1")
  11.  
  12.     For Each prm In qdf.Parameters
  13.             prm.Value = Eval(prm.Name)
  14.     Next prm
  15.  
  16. Set rs = qdf.OpenRecordset(dbOpenDynaset)
  17.     rs.MoveLast
  18.     rs.MoveFirst
  19.  
  20.     For I = 1 To rs.RecordCount - 1
  21.  
  22.         strProduct = rs![Product]
  23.         strPrice = rs![Price]
  24.  
  25.         Me("btn" & I).Caption = strProduct & " " & strPrice
  26.  
  27.    Next I
  28.    rs.Close
  29.    Set rs = Nothing
  30.  
May 18 '07 #3

Post your reply

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