英文:
How to validate data before update in the recordset in Ms access VBA
问题
在获取更多信息后,我认为有必要在将数据存储在Ms Access中的静态表之前进行数据验证。坦白地说,我对这个话题并不确定,这对我来说是新的,我只是试图看看它是否能够正常工作。
我想要检查在继续更新之前是否有数据,下面是完整的代码:
Set rs = db.OpenRecordset("tblEfdReceipts")
If lngStatus > 0 Then
ElseIf lngStatus < 0 Then
' 处理错误。
On Error Resume Next
End If
' 处理数据。
Set JSONS = JsonConverter.ParseJson(strData)
Z = 2
For Each item In JSONS
With rs
.AddNew
rs![TPIN] = item("TPIN")
rs![TaxpayerName] = item("TaxpayerName")
rs![Address] = item("Address")
rs![ESDTime] = item("ESDTime")
rs![TerminalID] = item("TerminalID")
rs![InvoiceCode] = item("InvoiceCode")
rs![InvoiceNumber] = item("InvoiceCode")
rs![FiscalCode] = item("FiscalCode")
rs![TalkTime] = item("TalkTime")
rs![Operator] = item("Operator")
rs![Taxlabel] = item("TaxItems")("TaxLabel")
rs![CategoryName] = item("TaxItems")("CategoryName")
rs![Rate] = item("TaxItems")("Rate")
rs![TaxAmount] = item("TaxItems")("TaxAmount")
rs![VerificationUrl] = item("TaxItems")("VerificationUrl")
rs![INVID] = Me.InvoiceID
rs.Update
End With
Z = Z + 1
Next
rs.Close
Set rs = Nothing
Set db = Nothing
Set JSONS = Nothing
我正在尝试这段代码,但我不确定如何正确地执行它,接收到的数据在检查之前永远不会成为实际数据。
**验证代码需要改进**
```vba
rs = Me.Recordset.Clone
If Me.Recordset.RecordCount = 0 Then '检查记录数
MsgBox "没有记录"
End If
如果数据存在,然后进行处理。
英文:
After getting more information here, I think there is need to validate the data before storing in a stagnant table in Ms access. I’m not sure about this topic frankly its new to me , I’m just try to see whether it will work.
I want to check whether Set rs = db.OpenRecordset("tblEfdReceipts")
has data before proceeding to update , below is the full code:
Set rs = db.OpenRecordset("tblEfdReceipts")
If lngStatus > 0 Then
ElseIf lngStatus < 0 Then
' Handle error.
On Error Resume Next
End If
' Process data.
Set JSONS = JsonConverter.ParseJson(strData)
Z = 2
For Each item In JSONS
With rs
.AddNew
rs![TPIN] = item("TPIN")
rs![TaxpayerName] = item("TaxpayerName")
rs![Address] = item("Address")
rs![ESDTime] = item("ESDTime")
rs![TerminalID] = item("TerminalID")
rs![InvoiceCode] = item("InvoiceCode")
rs![InvoiceNumber] = item("InvoiceCode")
rs![FiscalCode] = item("FiscalCode")
rs![TalkTime] = item("TalkTime")
rs![Operator] = item("Operator")
rs![Taxlabel] = item("TaxItems")("TaxLabel")
rs![CategoryName] = item("TaxItems")("CategoryName")
rs![Rate] = item("TaxItems")("Rate")
rs![TaxAmount] = item("TaxItems")("TaxAmount")
rs![VerificationUrl] = item("TaxItems")("VerificationUrl")
rs![INVID] = Me.InvoiceID
rs.Update
End With
Z = Z + 1
Next
rs.Close
Set rs = Nothing
Set db = Nothing
Set JSONS = Nothing
I’m trying this code but I’m not sure of how do it correctly, the received data is never part of the live until checked.
Validation code require improvements
rs = Me.Recordset.Clone
If Me.Recordset.RecordCount = 0 then 'checks for number of records
msgbox "There is no records"
End if
If the data is present then process it.
答案1
得分: 3
使用 DCount:
如果 DCount("*", "tblEfdReceipts") = 0 Then
' 表格没有记录。
Set rs = db.OpenRecordset("tblEfdReceipts")
' <snip>
Else
' 表格有记录。跳过。
End if
英文:
Use DCount:
If DCount("*", "tblEfdReceipts") = 0 Then
' Table has no records.
Set rs = db.OpenRecordset("tblEfdReceipts")
' <snip>
Else
' Table has records. Skip.
End if
答案2
得分: 2
作为一般规则,您可以检查EOF(文件末尾)。
因此:
Set rs = db.OpenRecordset("tblEfdReceipts")
if rs.EOF = True then
' 没有记录
End If
英文:
As a general rule, you can check EOF (end of file).
Thus:
Set rs = db.OpenRecordset("tblEfdReceipts")
if rs.EOF = True then
' no records
End If
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论