vba - Access 2016 - VBA - Filling an Empty Field/Column with Data Based on the Value Within an Adjacent (same row) Cell
问题描述
Here is my table (within my form) in MS Access 2016.
I wish to have code in VBA which:
1.) Scans all of the "Tag_Value" field
2.) While scanning, IF the values I indicate are found, THEN insert a "text string" (such as "Watchdog Timer") into the "ErrorDescription" field on the same row that the corresponding value is found on
Seems simple, right? I hope it is... It's cake using Excel, but I need the table used by my form to be linked with a MySQL server (for constant updates), not Excel.
I have managed to get it to write to the very first row using IF statements (_OnLoad and _Click [of the filter button]), but the party stops there.
Any help would be greatly appreciated. Thanks in advance for your time!
Got it!
See code below:
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
解决方案
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
推荐阅读
- java - 在 Java 中使用递归查找最大 Int
- javascript - 如何访问在子地图(嵌套地图)中映射的父元素
- go - 选择频道时遇到死锁
- javascript - 如何使用路由器从一个组件访问传递的数据到另一个 ReactJS
- node.js - 如何使用firebase函数返回保存在firebase存储中的文件的json内容
- git - git fetch:如何查看每个分支的下载数据
- r - 过滤在 r 中事件“A”的时间范围内发生的事件
- javascript - 有没有办法在输入之前修改搜索菜单以隐藏菜单?
- laravel - 在 Laravel 中,我应该将模型的实用程序放在哪里?
- javascript - 有没有办法发出 API 请求,并且在发出请求之前只获取满足特定条件的数据?