我有一个名为frmPUCFinalize的表单,其中有四个按钮,名为btn1,btn1,btn2,btn4,还有一个表(tblStatus),其中4个标题名称存储在名为" Button"的单个字段中。
我想使用循环或其他方法将frmPUCFinalize的所有4个按钮标题替换为表字段(字段名称:按钮)中的每个记录集。
我尝试了以下代码,但无法成功。
Private Sub Form_Load()
Dim rst As Recordset
Dim mSQL As String
mSQL = "SELECT tblStatus.Button FROM tblStatus WHERE (((tblStatus.RoleID)=4) AND ((tblStatus.Form)='frmPUCFinalize')) ORDER BY tblStatus.Button;"
Set rst = CurrentDb.OpenRecordset(mSQL)
x = 1
Y = 4 'maximun 4 buttons
rst.MoveFirst
Do While (rst.BOF = False And rst.EOF = False) And x < Y + 1
Me("btn" & x).Caption = rst!Button
x = x + 1
rst.MoveNext
Loop
rst.Close
End Sub
答案 0 :(得分:1)
试试这个:
Private Sub Form_Load()
Dim rst As Recordset
Dim mSQL As String
Dim idx As Long
mSQL = "SELECT tblStatus.Button FROM tblStatus WHERE (((tblStatus.RoleID)=4) AND ((tblStatus.Form)='frmPUCFinalize')) ORDER BY tblStatus.Button;"
Set rst = CurrentDb.OpenRecordset(mSQL)
If rst.EOF Then
rst.Close
Exit Sub
End If
With rst
.MoveLast
.MoveFirst
End With
For idx = 1 to rst.RecordCount
Me.Controls("btn" & idx).Caption = rst![Button]
rst.MoveNext
Next idx
rst.Close
End Sub