我正在尝试创建一个 SQL 字符串,它将检查表 Notification 的当前记录条件(两个不同的 ID 值)。如果它找到具有这两个值的记录,它将不会将该记录输入到表中。如果它没有找到它,它会。
我已经尝试使用 VBA 来解决这个问题,但似乎我能够做到这一点的唯一方法是使用 SQL,因为 Access 字段类型与 SQL 字段类型不同导致的常量类型不匹配错误(IE:整数在 SQL 中可以,但在 Access VBA 中导致溢出的值)。
我一直在尝试找到某种 WHERE 语句,它可以让我检查表以查看 AssetID 和 NotificationTypeID 是否已经在表中。如果是,则忽略插入并继续。
我已经看到其他类型的 SQL 示例可以回答这个问题,但我无法让它们在 VBA 中工作。
更新的代码(注意:'我知道,AssetID 应该是 LONG。它是 SQL 中的一个 Int,但是当在 vba 中为 Access 设置为 Int 时,我收到一条溢出消息'当我尝试将其设置为 long 时,有一个类型不匹配。字符串目前似乎可以在 SQL 中用于将值放入数据库中)
这仍然不适用于 .AddNew。它应该,但由于某种原因返回一个 Invalid Operation 错误。
Dim Response As Integer
Dim strSQL As String
Dim delSQL As String
Dim NotTypeID As String
Dim NotDate As Date
Dim AssetId As String
Dim rcdCount As Integer
Dim i As Integer
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rcd As DAO.Recordset
Dim strSelect As String
strGroup = ReturnGroup
'Check user credentials before showing notifications
If InStr(1, strGroup, "Not_admins") Or InStr(1, strGroup, "Admins") Then
DoCmd.SetWarnings True
'Check the Storage Location query, see if there is a reason to notify the user
If DCount("*", "qry_UnknownStorageLoc") > 0 Then
'Getting the record count from the query
rcdCount = DCount("*", "qry_UnknownStorageLoc")
'This is the popup message box that is shown to the user when Fassetrack loads
'Response = MsgBox("Notice: " & DCount("*", "qry_UnknownStorageLoc") & " record(s) which contain an unknown storage location", vbInformation + vbOKOnly, "UnknownStorage")
strSQL = "SELECT AssetID FROM qry_UnknownStorageLoc"
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
i = 1
'Loop through to gather all the records for this notification type
'and add them to the Notifications table
With rst
Do Until .EOF
'Set the AssetID value, then move to the next record in the query
AssetId = rst!AssetId
'NotTypeID is the id of the notification type in the NotificationType table
NotTypeID = 1
rst.MoveNext
'Setting the notification date to the last date the record was modified with
'the logic being the last edit triggered the notification. When the record is
'corrected and/or acknowledged, it will no longer trigger a notification.
'Null checking to ensure no errors occur
If (IsNull(DLookup("modifiedon", "qry_UnknownStorageLoc"))) Then
NotDate = 0
Else
NotDate = DLookup("modifiedon", "qry_UnknownStorageLoc")
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
strSelect = "SELECT n.NotificationTypeID, n.NotificationDate, n.AssetID" & vbCrLf & _
"FROM Notifications AS n" & vbCrLf & _
"WHERE n.NotificationTypeID = [pType] AND n.NotificationDate = [pDate] AND n.AssetID = [pID];"
Debug.Print strSelect
Set qdf = db.CreateQueryDef(vbNullString, strSelect)
With qdf
.Parameters("pType").Value = NotTypeID
.Parameters("pDate").Value = NotDate
.Parameters("pID").Value = AssetId
Set rs = .OpenRecordset(dbOpenDynaset, dbSeeChanges)
End With
With rs
If .BOF And .EOF Then
.AddNew
!NotificationTypeID.Value = NotTypeID
!NotificationDate.Value = NotDate
!AssetId.Value = AssetId
.Update
End If
.Close
End With
i = i + 1
Loop
End With
'Close and clear the recordset
rst.Close
Set rst = Nothing
End If