Access 2016 - VBA - 根据相邻(同一行)单元格内的值填充空白字段/列数据

时间:2018-04-26 17:50:33

标签: vba ms-access

img1

这是MS Access 2016中的表格(在我的表单中)。

我希望在VBA中有代码:

1。)扫描所有“Tag_Value”字段

2.。)扫描时,如果找到我指示的值,那么将“文本字符串”(例如“看门狗定时器”)插入到找到相应值的同一行的“ErrorDescription”字段中< / p>

似乎很简单,对吧?我希望它是...这是使用Excel的蛋糕,但我需要我的表格使用的表格与MySQL服务器(用于不断更新),而不是Excel。

我已经设法让它使用IF语句写入第一行(_OnLoad和_Click [过滤器按钮]),但派对停在那里。

非常感谢任何帮助。提前感谢您的时间!

知道了!

见下面的代码:

Dim d As Database
Dim r As Recordset
Dim Tag_Value As Field, ErrorDescription As Field

Set d = CurrentDb()
Set r = d.OpenRecordset("alarmlogwithdescs")
Set Tag_Value = r.Fields("Tag_Value")
Set ErrorDescription = r.Fields("ErrorDescription")
r.LockEdits = True
r.MoveFirst

    While Not r.EOF
        If [Tag_Value] = 7194 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Watchdog Timer"
        r.Update
        End If

        If [Tag_Value] = 3483 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Controller Fault1"
        r.Update
        End If

        If [Tag_Value] = 6816 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Controller Fault2"
        r.Update
        End If

        If [Tag_Value] = 3105 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Controller Fault3"
        r.Update
        End If

        If [Tag_Value] = 6438 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "HMI Fault2"
        r.Update
        End If

        If [Tag_Value] = 2727 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "HMI Fault2"
        r.Update
        End If

        If [Tag_Value] = 6060 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "HMI Fault3"
        r.Update
        End If

        If [Tag_Value] = 2349 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Motor Overheating"
        r.Update
        End If

        If [Tag_Value] = 5682 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Motor Failed to Start"
        r.Update
        End If

        If [Tag_Value] = 1971 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Motor Failed to Stop"
        r.Update
        End If

    r.MoveNext

    Wend

    r.Close

2 个答案:

答案 0 :(得分:0)

Dim d As Database
Dim r As Recordset
Dim Tag_Value As Field, ErrorDescription As Field

Set d = CurrentDb()
Set r = d.OpenRecordset("alarmlogwithdescs")
Set Tag_Value = r.Fields("Tag_Value")
Set ErrorDescription = r.Fields("ErrorDescription")
r.LockEdits = True
r.MoveFirst

    While Not r.EOF
        If [Tag_Value] = 7194 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Watchdog Timer"
        r.Update
        End If

        If [Tag_Value] = 3483 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Controller Fault1"
        r.Update
        End If

        If [Tag_Value] = 6816 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Controller Fault2"
        r.Update
        End If

        If [Tag_Value] = 3105 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Controller Fault3"
        r.Update
        End If

        If [Tag_Value] = 6438 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "HMI Fault2"
        r.Update
        End If

        If [Tag_Value] = 2727 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "HMI Fault2"
        r.Update
        End If

        If [Tag_Value] = 6060 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "HMI Fault3"
        r.Update
        End If

        If [Tag_Value] = 2349 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Motor Overheating"
        r.Update
        End If

        If [Tag_Value] = 5682 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Motor Failed to Start"
        r.Update
        End If

        If [Tag_Value] = 1971 & IsNull([ErrorDescription]) Then
        r.Edit
        ErrorDescription = "Motor Failed to Stop"
        r.Update
        End If

    r.MoveNext

    Wend

    r.Close

答案 1 :(得分:0)

数据不应该是硬编码的。

因此,创建一个表来保存这些警报代码描述:

[Tag_Value]  ErrorDescription 
       3188  "Watchdog Timer"
       6522  "Controller Fault1"

etc. 

然后使用查询加入[Tag_Value],以检索错误说明。