【发布时间】:2015-02-23 15:31:12
【问题描述】:
寻求帮助。我想要一个最多包含 20 个按钮的表单。我希望他们的显示名称来自我销售的产品的访问数据库表。我可以将表单连接到我的数据库并让表中的第一条记录显示在第一个按钮上,但是当我对第二个按钮执行相同操作时,它只会再次显示第一条记录。有没有办法将按钮名称指向表中的第二条记录?我不想将产品名称硬编码到代码中,因为它们将来可能会改变:-)
提前致谢
Public Class f
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'LoftDataSet.Services' table. You can move, or remove it, as needed.
Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services)
'Loop through all the rows that are in the dataset
For Each dr As DataRow In LoftDataSet.Services.Rows
Dim btn As New Button 'Instantiate a button
btn.Text = dr("service_name").ToString 'UserName is a field in my Users Table
btn.Size = New Size(60, 40)
btn.Tag = dr("ID") 'Here we set the tag to the primary key (ID)
'Since we're using a flowlayoutpanel, we don't need to worry about setting the location property
FlowLayoutPanel1.Controls.Add(btn) 'Add the button to the flow layout panel
AddHandler btn.Click, AddressOf UserClick 'Here we give the button a handler for the click event
Next
End Sub
'Here we write our method for the click event of the button(s) we created
Private Sub UserClick(ByVal sender As Object, ByVal e As EventArgs)
'We set a filter to the binding source passing it ID=<and whatever is stored in the tag property>
ServicesBindingSource.Filter = "ID = " & DirectCast(sender, Button).Tag.ToString
End Sub
【问题讨论】:
-
没有代码就无法知道您的代码出了什么问题。此外,如果它是 VB.NET,则它不是 access-vba,反之亦然。见How to Ask
-
我正在使用 vb studio,我已经连接到 .mdb 并且连接正常
-
您已经问过这个问题,尽管您在该问题中的数据在文本文件中。您需要显示您的代码....我假设您正在读取数据,但没有遍历每条记录以填充您的按钮...因此您在所有按钮中都获得了第一条记录...
-
好的,我已经粘贴了代码。谢谢
-
我的代码对我有帮助吗?
标签: vb.net forms ms-access button connection