MS Access 中更新查询的间歇性问题

Intermittent problem with update query in MS Access

我有一个 UPDATE 查询间歇性地以意外方式运行。上下文是我有一个 table 用于存储 phone 数字和 user_id (user_id 是主用户 table 的外键)。 phone 列的类型为数字。我正在通过主窗体上的按钮更新 phone 号码,事件处理程序粘贴在下面。

Private Sub button_add_phone_Click()

Dim phone_query As String
Dim phone_number As String
Dim user_query As String
Dim user_id As Integer
Dim user_rs As DAO.recordset
Dim curr_db As DAO.Database

Set curr_db = CurrentDb()

If Not IsNull(Form!hidden_user_id) Then
    user_id = Form!hidden_user_id
Else
    MsgBox "Select a user to proceed."
    Exit Sub
End If

phone_number = Form!Phone

user_query = "select * from AVIT_Phone where user_id = " & user_id
Set user_rs = curr_db.OpenRecordset(user_query, dbOpenSnapshot, dbSeeChanges)

If user_rs.EOF Then
    phone_query = "insert into AVIT_Phone (user_id, phone) values (" & user_id & ", '" & phone_number & "')"
Else
    phone_query = "update AVIT_Phone set phone = " & phone_number & " where user_id = " & user_id
End If

curr_db.execute phone_query, dbSQLPassThrough

MsgBox "Phone number updated for user " & Form!text_name

curr_db.Close

Set user_rs = Nothing
Set curr_db = Nothing

End Sub

我遇到的间歇性问题是,更新查询偶尔会将 phone 设置为 NULL,而不是文本框 (Form!phone) 中的值。

我最初以为是格式错误或类型转换错误,但它的间歇性表明还有其他事情正在发生。

如果您在代码中看到任何会导致此行为的内容,请告诉我。

在这种情况下,phone 数字作为 10 位值输入,这导致数据类型为数字的 phone 数字列溢出。该列的字段大小为 Long Integer,其值范围为 -2,147,483,648 至 +2,147,483,647。

解决方案是按照 June7 的建议将 phone 数字存储为文本字段而不是数字字段。