【发布时间】:2015-07-23 16:12:12
【问题描述】:
我正在尝试创建一个 SQL 字符串,该字符串将检查表 Notification 的当前记录条件(两个不同的 ID 值)。如果它找到具有这两个值的记录,它将不会将该记录输入到表中。如果它没有找到它,它会。
我已经尝试使用 VBA 来解决这个问题,但似乎我能够做到这一点的唯一方法是使用 SQL,因为 Access 字段类型与SQL 字段类型(IE:整数在 SQL 中可以,但在 Access VBA 中导致溢出的值)。
我一直在尝试找到某种 WHERE 语句,它可以让我检查表以查看 AssetID 和 NotificationTypeID 是否已经在表中。如果是,则忽略插入并继续。
我已经看到其他类型的 SQL 示例可以回答这个问题,但我无法让它们在 VBA 中工作。
更新的代码(注意:'我知道,AssetID 应该是长的。它是 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
【问题讨论】:
-
阻止我使用 VBA 解决此问题的一个问题是,有问题的 ID 是 SQL 数据库中的整数。当它通过 Access 时会出现问题,因为值超过 32000+,所以出现了溢出错误。我无法更改这些类型,因为它们是预先存在并自动递增的。
-
查看之前的帖子stackoverflow.com/questions/19573198/…。看起来您必须先检查是否存在,然后才能运行插入语句。
标签: sql-server sql-server-2008 ms-access vba ms-access-2002