永发信息网

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

答案:1  悬赏:10  手机版
解决时间 2021-03-27 14:15
  • 提问者网友:皆是孤独
  • 2021-03-27 03:01
ACCESS VBA 中如何用数组解决这个问题
最佳答案
  • 五星知识达人网友:几近狂妄
  • 2021-03-27 03:40
用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追问高手,我刚学不久,recordset还在努力研究之中。可能我的问题没表达明白。示例如附,上百台机器都要显示各自的状态,但如果我按你的代码,看起来只是A01的图片会变啊。
大侠请指点。

追答刚弄清楚你的意思,附件里用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对象来引用图片,根据图片名选择图片,再改变图片属性
 
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯