ACCESS VBA 中如何用数组解决这个问题
对第一台机,VBA代码如下:
Private Sub refreshstatus()
Select Case DLookup("STATE", "MACHINESTATE", "MACHINE= 'A01'")
Case 0
ImgA01.Picture = "running"
Case 1 To 2
ImgA01.Picture = "standby"
Case 3 To 4
ImgA01.Picture = "stop"
Case 5 To 10
ImgA01.Picture = "fault"
End Select
End Sub
问题是:我有上百台机。有没有办法用数组来解决问题,比如把machinestate中的每条记录,找到machine(如"A01" )的state,然后在case中的语句内使用Img'A01'.picture? 怎么写这个属性名才合法? 展开
sub setall()
dim rs as dao.recordset
set rs = me.recordset
with rs
.movefirst
do until .eof
refreshstatus .fieds("STATE").value
.movenext
loop
end with
set rs=nothing
end sub
Private Sub refreshstatus(intState as integer)
Select Case intState
Case 0
ImgA01.Picture = "running"
Case 1 To 2
ImgA01.Picture = "standby"
Case 3 To 4
ImgA01.Picture = "stop"
Case 5 To 10
ImgA01.Picture = "fault"
End Select
End Sub
刚弄清楚你的意思,附件里用Form1做了个示例,点击按钮运行代码。
代码简要解释如下:
Private Sub Command6_Click()
SetAll
End Sub
'通过点击按钮调用下面的过程
Sub SetAll()
Dim rs As DAO.Recordset
Set rs = Me.MachineStatus.Form.Recordset
With rs
.MoveFirst
Do Until .EOF
refreshstatus .Fields("Machine").Value, .Fields("State").Value
.MoveNext
Loop
End With
Set rs = Nothing
End Sub
'依次根据机器名和状态值调用子过程给与机器同名的图片赋值
Private Sub refreshstatus(strMachine As String, intState As Integer)
Dim ctr As Control, strName As String
strName = "Img" & strMachine
Set ctr = Me.Controls(strName)
With ctr
Select Case intState
Case 0
.Picture = "running.jpg"
Case 1 To 2
.Picture = "standby.jpg"
Case 3 To 4
.Picture = "stop.jpg"
Case 5 To 10
.Picture = "fault.jpg"
End Select
End With
Set ctr = Nothing
End Sub
'使用control对象来引用图片,根据图片名选择图片,再改变图片属性