ACCESS VBA 中如何用数组解决这个问题

有一个表machinestate, 它有两个字段machine, state。现在要根据每条记录的state,把窗体中machine对应的图标显示出来。对第一台机,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 SelectEnd Sub问题是:我有上百台机。有没有办法用数组来解决问题,比如把machinestate中的每条记录,找到machine(如"A01" )的state,然后在case中的语句内使用Img✀A01✀.picture? 怎么写这个属性名才合法?
2026年09月23日 23:09
有1个网友回答
网友(1):

用recordset解决。 me.recordset用来提取当前窗体的记录集
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