简体   繁体   English

用NULL更新带有表单中文本字段的数值字段

[英]Update with NULL for a Numeric field with a textfield in a form

I want to be able to update a numeric column to different values and also be able clear values in a field (set the field to null). 我希望能够将数字列更新为不同的值,并且还希望清除字段中的值(将该字段设置为null)。 I am able to update the field to any number I want but whenever I delete the value and set it to nothing, it's giving me an error. 我可以将字段更新为所需的任何数字,但是每当我删除该值并将其设置为空时,都会给我一个错误。

Here's my code: 这是我的代码:

Private Sub txtPortNumber_AfterUpdate()

Dim portSQL As String

portSQL = "UPDATE Switches SET [Port Number] = " & Me.txtPortNumber & " WHERE ID = " & Me.txtID3 & ""

DoCmd.RunSQL (portSQL)

Me.Refresh

End Sub

How would you recommend me approaching this issue? 您如何建议我解决这个问题? I'm thinking that I need to write an IF-ELSE statement but I'm unsure of what to put. 我以为我需要写IF-ELSE声明,但不确定要输入什么。

txtID3 is a number and txtPortNumber is also numeric. txtID3是数字,而txtPortNumber也是数字。

Sorry I got started on this, I thought it was VB.NET and not Access. 抱歉,我是从此开始的,我以为是VB.NET而不是Access。 Let me know if this works: 让我知道这个是否奏效:

Private Sub txtPortNumber_AfterUpdate()
    Dim portSQL As String
    Dim portNumber as Integer
    Dim myID as Integer 

    If IsNumeric(Me.txtID3) Then 
        myID = CInt(Me.txtID3) 

        If IsNumeric(Me.txtPortNumber) Then
            portNumber = CInt(Me.txtPortNumber) 
            portSQL = "UPDATE Switches SET [Port Number] = " & portNumber & " WHERE ID = " & myID  & ""
        Else
            portSQL = "UPDATE Switches SET [Port Number] = NULL WHERE ID = " & myID  & ""
        End If 
        DoCmd.RunSQL (portSQL)

    End If
    Me.Refresh
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM