MS SQL 2008 / Access 2002 VBA - 检查DB以获取当前记录,如果不存在则输入它

时间:2015-07-23 16:12:13

标签: sql-server sql-server-2008 ms-access access-vba ms-access-2002

我正在尝试创建一个SQL字符串,该字符串将检查表Notification以获取当前记录条件(两个不同的ID值)。如果它找到包含这两个值的记录,则不会将此记录输入表中。如果找不到,它就会。

我已经尝试过使用VBA来解决这个问题,但是我似乎能够做到这一点的唯一方法是使用SQL,因为由于Access字段类型不一致导致的类型不匹配错误不一致SQL字段类型(IE:整数在SQL中正常,但是在Access VBA中导致溢出的值)。

我一直试图找到某种WHERE语句,让我检查表,看看AssetID和NotificationTypeID是否已经在表中。如果是,则忽略插入并继续前进。

我已经看过其他类型的SQL回答这个问题的例子,但是我不能让它们在VBA中工作。

更新的代码(注意:'我知道,AssetID应该是LONG。它是SQL中的Int,但是当在vba中设置为Int for Access时,我收到溢出消息 '当我尝试将其设置为long时,会出现类型不匹配的情况。字符串似乎在SQL中用于将值放入数据库中)

这仍然不适用于.AddNew。它应该,但由于某种原因返回无效操作错误。

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

2 个答案:

答案 0 :(得分:2)

考虑根据参数查询从临时iloc加载记录集。如果记录集为空,则不存在匹配的记录,因此您可以添加记录。

QueryDef

答案 1 :(得分:0)

首先非常感谢cars10和HansUp,你们都让我朝着正确的方向前进。

我决定退出QueryDefs,因为我还不熟悉它们。这是工作结果。

XAxis