G
Guest
Dirk helped extensively in designing a subform that I use to create bills of
material. This form simply associates one txtProfileID with many. Here’s how
it’s coded:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' Pick up previous associated profile, if any.
' If the old value is Null, the following assignment will result in
' a zero-length string.
mstrOldAssociations = ("'" + Me.cbProfilesAssociations.OldValue + "'") &
vbNullString
End Sub
Private Sub Form_AfterUpdate()
With CurrentDb
' Delete the old association, if it existed.
If Len(mstrOldAssociations) > 0 Then
.Execute _
"DELETE * FROM tblPKProfilesAssociations " & _
"WHERE ProfilesAssociations='" & Me!txtProfileID & "' " & _
"AND txtProfileID=" & mstrOldAssociations
End If
' Create a reciprocal record to match the record being saved.
.Execute _
"INSERT INTO tblPKProfilesAssociations " & _
"(ProfilesAssociations, txtProfileID) " & _
"VALUES ('" & Me!txtProfileID & "', '" &
Me.cbProfilesAssociations & "')"
End With
Private Sub Form_Delete(Cancel As Integer)
' Capture the old association from each deleted record.
mstrOldAssociations = mstrOldAssociations & ",'" &
Me.cbProfilesAssociations & "'"
End Sub
Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
If MsgBox("Would you like to delete this record?", _
vbQuestion + vbYesNoCancel + vbDefaultButton2) = vbYes Then
Cancel = False
Response = acDataErrContinue
Else
Cancel = True
End If
End Sub
Private Sub Form_AfterDelConfirm(Status As Integer)
' Don't do anything unless the delete was successful.
If Status = acDeleteOK Then
' We will have created a comma-separated list of the IDs to be
deleted.
CurrentDb.Execute _
"DELETE * FROM tblPKProfilesAssociations " & _
"WHERE ProfilesAssociations='" & Me.Parent!txtProfileID & "'
" & _
"AND txtProfileID IN (" & Mid$(mstrOldAssociations, 2) & ")"
End If
mstrOldAssociations = vbNullString
End Sub
All of this works fine, however, I’ve come across another design challenge.
When I delete a txtProfileID using the Access delete record button it prompts
if I want to delete all related records. Of course, this works fine, too. The
challenge is that this results in records leftover on the
tblPKProfilesAssociations. For example, deleting txtProfileID 12345 which is
associated with 200000 results in the record:
txtProfileID 12345 | AssociatedProfile 200000
being deleted from tblPKProfilesAssociations BUT it leaves the record:
txtProfileID 200000 | AssociatedProfile 12345
YIKES!
I feel like I'm painted into a corner. Any way to resolve this? As always,
any help would greatly be appreciated!
THANKS!
material. This form simply associates one txtProfileID with many. Here’s how
it’s coded:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' Pick up previous associated profile, if any.
' If the old value is Null, the following assignment will result in
' a zero-length string.
mstrOldAssociations = ("'" + Me.cbProfilesAssociations.OldValue + "'") &
vbNullString
End Sub
Private Sub Form_AfterUpdate()
With CurrentDb
' Delete the old association, if it existed.
If Len(mstrOldAssociations) > 0 Then
.Execute _
"DELETE * FROM tblPKProfilesAssociations " & _
"WHERE ProfilesAssociations='" & Me!txtProfileID & "' " & _
"AND txtProfileID=" & mstrOldAssociations
End If
' Create a reciprocal record to match the record being saved.
.Execute _
"INSERT INTO tblPKProfilesAssociations " & _
"(ProfilesAssociations, txtProfileID) " & _
"VALUES ('" & Me!txtProfileID & "', '" &
Me.cbProfilesAssociations & "')"
End With
Private Sub Form_Delete(Cancel As Integer)
' Capture the old association from each deleted record.
mstrOldAssociations = mstrOldAssociations & ",'" &
Me.cbProfilesAssociations & "'"
End Sub
Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
If MsgBox("Would you like to delete this record?", _
vbQuestion + vbYesNoCancel + vbDefaultButton2) = vbYes Then
Cancel = False
Response = acDataErrContinue
Else
Cancel = True
End If
End Sub
Private Sub Form_AfterDelConfirm(Status As Integer)
' Don't do anything unless the delete was successful.
If Status = acDeleteOK Then
' We will have created a comma-separated list of the IDs to be
deleted.
CurrentDb.Execute _
"DELETE * FROM tblPKProfilesAssociations " & _
"WHERE ProfilesAssociations='" & Me.Parent!txtProfileID & "'
" & _
"AND txtProfileID IN (" & Mid$(mstrOldAssociations, 2) & ")"
End If
mstrOldAssociations = vbNullString
End Sub
All of this works fine, however, I’ve come across another design challenge.
When I delete a txtProfileID using the Access delete record button it prompts
if I want to delete all related records. Of course, this works fine, too. The
challenge is that this results in records leftover on the
tblPKProfilesAssociations. For example, deleting txtProfileID 12345 which is
associated with 200000 results in the record:
txtProfileID 12345 | AssociatedProfile 200000
being deleted from tblPKProfilesAssociations BUT it leaves the record:
txtProfileID 200000 | AssociatedProfile 12345
YIKES!
I feel like I'm painted into a corner. Any way to resolve this? As always,
any help would greatly be appreciated!
THANKS!