我有一个记录集TBLSOURCETARGETLINK,我想在更新后事件上更新。
我还需要确保在添加新记录之前不存在重复项。 如果记录存在,我只想更新记录并添加新记录。
我正在努力与VBA和如何做到这一点的顺序。
请有人帮忙。
VBA目前看起来像这样:
Private Sub IsSource_AfterUpdate()
Dim VbaTableID As Integer
Dim VbaSystemID As Integer
Dim VbaFieldID As Integer
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim tabledef As String
Dim strcriteria As String
Dim rstSourceTarget As Recordset
Set rstSourceTarget = CurrentDb.OpenRecordset(Name:="tblsouretargetlink", Type:=RecordsetTypeEnum.dbOpenDynaset)
If IsNull(Me!TableID) Then
Else
VbaTableID = Me!TableID
End If
If IsNull(Me!SystemID) Then
Else
VbaSystemID = Me!SystemID
End If
If IsNull(Me!FieldID) Then
Else
Vbafield = Me!FieldID
End If
strcriteria = "[systemid] = '" & Me.SystemID & "' And [tableid] = " & Me.TableID And [FieldID] = " & Me.fieldid"
rs.FindFirst strcriteria
If .NoMatch Then
With rstSourceTarget
.AddNew
![SystemID] = VbaSystemID
![SourceTable] = VbaTableID
![SourceField] = VbaFieldID
.Update
End With
Else
With rstSourceTarget
.Edit
![IsSource] = -1
.Update
End If
End Sub
谢谢
答案 0 :(得分:0)
试试这段代码:
Private Sub IsSource_AfterUpdate()
Dim VbaTableID As Integer
Dim VbaSystemID As Integer
Dim VbaFieldID As Integer
Dim db As DAO.Database
Dim strCriteria As String
Dim rstSourceTarget As Recordset
Set db = CurrentDb
' don't use CurrentDb for opening recordsets, it's dynamic, you can lose reference to database.
Set rstSourceTarget = db.OpenRecordset(Name:="tblsouretargetlink", Type:=RecordsetTypeEnum.dbOpenDynaset)
If Not IsNull(Me!TableID) Then
VbaTableID = Me!TableID
End If
If Not IsNull(Me!SystemID) Then
VbaSystemID = Me!SystemID
End If
If Not IsNull(Me!fieldid) Then
VbaFieldID = Me!fieldid
End If
strCriteria = "[systemid] = '" & Me.SystemID & "' And [tableid] = " & Me.TableID & " And [FieldID] = " & Me.fieldid
With rstSourceTarget
.FindFirst strCriteria
If .NoMatch Then
.AddNew
![SystemID] = VbaSystemID
![SourceTable] = VbaTableID
![SourceField] = VbaFieldID
.Update
Else
.Edit
![IsSource] = -1
.Update
End If
End With
End Sub
有很多错误,所以首先添加
Option Explicit
在每个模块的开头并重新编译项目(Debug-> Compile)