在Ms Access VBA中,在更新记录集之前如何验证数据。

huangapple go评论80阅读模式
英文:

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(&quot;tblEfdReceipts&quot;) has data before proceeding to update , below is the full code:

Set rs = db.OpenRecordset(&quot;tblEfdReceipts&quot;)
    If lngStatus &gt; 0 Then
    ElseIf lngStatus &lt; 0 Then
        &#39; Handle error.
        On Error Resume Next
    End If
        &#39; Process data.
  Set JSONS = JsonConverter.ParseJson(strData)
    Z = 2
  For Each item In JSONS
           With rs
         
            .AddNew
            rs![TPIN] = item(&quot;TPIN&quot;)
            rs![TaxpayerName] = item(&quot;TaxpayerName&quot;)
            rs![Address] = item(&quot;Address&quot;)
            rs![ESDTime] = item(&quot;ESDTime&quot;)
            rs![TerminalID] = item(&quot;TerminalID&quot;)
            rs![InvoiceCode] = item(&quot;InvoiceCode&quot;)
            rs![InvoiceNumber] = item(&quot;InvoiceCode&quot;)
            rs![FiscalCode] = item(&quot;FiscalCode&quot;)
            rs![TalkTime] = item(&quot;TalkTime&quot;)
            rs![Operator] = item(&quot;Operator&quot;)
            rs![Taxlabel] = item(&quot;TaxItems&quot;)(&quot;TaxLabel&quot;)
            rs![CategoryName] = item(&quot;TaxItems&quot;)(&quot;CategoryName&quot;)
            rs![Rate] = item(&quot;TaxItems&quot;)(&quot;Rate&quot;)
            rs![TaxAmount] = item(&quot;TaxItems&quot;)(&quot;TaxAmount&quot;)
            rs![VerificationUrl] = item(&quot;TaxItems&quot;)(&quot;VerificationUrl&quot;)
            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 &#39;checks for number of records
   msgbox &quot;There is no records&quot; 
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(&quot;*&quot;, &quot;tblEfdReceipts&quot;) = 0 Then
    &#39; Table has no records.
    Set rs = db.OpenRecordset(&quot;tblEfdReceipts&quot;)

    &#39; &lt;snip&gt;
Else
    &#39; 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(&quot;tblEfdReceipts&quot;)
if rs.EOF = True then
   &#39; no records
End If

huangapple
  • 本文由 发表于 2020年1月3日 22:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定