B
Bob
Hi folks,
I have a continuous subform on a main data entry form.
Each entry on the subform contains two controls - a combo-box and a textbox
beside it.
The combobox allows the user to select a WebComType (Web Communication
Type - eg work email, home email etc). The box beside it is for entering
the actual email address etc.
Once a user has inserted a record into the linked table (tblWebComs), I want
the user to be able to delete the record by simply clearing the text box.
For this purpose, I have inserted the following into the BeforeUpdate event
for the combo-box:
Private Sub txtWebComType_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtWebComType) Then
Dim strMsg As String
Dim strTitle As String
Dim strSQL As String
strMsg = "Would you like to delete this entry?"
strTitle = "Delete WebCommunication Record"
If MsgBox(strMsg, vbQuestion Or vbYesNo Or vbDefaultButton2,
strTitle) _
= vbYes Then
CurrentDb.Execute "DELETE * FROM WebComs WHERE
[WebComs].[WebComID]=" & Me.ContactWebComs_WebComID & "", , dbFailOnError
Else
Cancel = True
Me.Undo
End If
End If
End Sub
The subform is linked to my main form (Clients) using this query string
auto-generated by Access:
SELECT [ContactWebComs].[ContactID], [ContactWebComs].[WebComID] AS
ContactWebComs_WebComID, [WebComs].[WebComID] AS WebComs_WebComID,
[WebComs].[WebComType], [WebComs].[WebComDetails] FROM WebComs INNER JOIN
ContactWebComs ON [WebComs].[WebComID]=[ContactWebComs].[WebComID];
The Child/Master link fields is ContactsID which appears in my contacts
table (the record source for the main form) and my junction table
(ContactWebComs).
When I run the BeforeUpdate code, I experience two problems:
(1) When I clear the combo-box and shift the focus (eg to the text box
beside it), the message box pops up to confirm the delete as expected. But
after that, nothing happens until I change focus again (eg tab to the next
row).
(2) Once I change focus a second time, a message pops up saying that the
record has been deleted - but as soon as I press okay on this message box,
Access just shuts down - no error messages or anything, the entire
application just shuts down.
After I restart the application and check the entries in tblWebComs, I can
see that the record I selected was in fact deleted.
I've tried a couple of variations for delete command.
For instance, I've tried hard coding the WebComID to delete like so:
CurrentDb.Execute "DELETE * FROM WebComs WHERE
[WebComs].[WebComID]=20", dbFailOnError
Again, the record is actually deleted, but I get exactly the same errors.
I've also tried running the following code instead of the currentdb.execute
line:
strSQL = "DELETE * " & _
"FROM WebComs " & _
"WHERE [WebComs].[WebComID]=" & Me.ContactWebComs_WebComID
& ""
DoCmd.SetWarnings False ' Turn off Access warning messages
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True ' Warning set on again
This results in exactly the same problems - and a correctly deleted record.
If I run the above code without the SetWarnings lines, the following
happens:
(a) my custom message box to popup;
(b) then Access's own message box warning that I am about to delete a record
from the specified table pops up;
(c) then nothing happens until I change focus to another part of the form;
(d) then a box pops up saying that "record is deleted"
(e) then all the rows in my subform disappear except for the blank "new
record" line
(f) a check of the table confirms that the record was correctly deleted.
The difference here is that Access does not shut down, but I can't stop
access from deleting all my rows. If I try to do incorporate a Me.Requery
after the outside "If ... Then" statement, Access complains that the line is
preventing it from saving anything to the database. If I try a
"Me.txtWebComType.Requery" line instead, I get an error saying that "You
must save the current field before you run a query action". If I insert a
"Me.txtWebComType.Requery" line in the AfterUpdate event instead, I end up
with the same problems described above.
I all out of ideas. Does anyone else have any pointers as to what might be
wrong with my code, or what might be causing these problems?
I should mention that the DoCmd code above worked fine when was
(incorrectly) deleting entries in my junction table (ContactWebComs) instead
of my WebComs table. (Please note, I have cascade update and delete
selected for all relationships).
TIA
Bob
I have a continuous subform on a main data entry form.
Each entry on the subform contains two controls - a combo-box and a textbox
beside it.
The combobox allows the user to select a WebComType (Web Communication
Type - eg work email, home email etc). The box beside it is for entering
the actual email address etc.
Once a user has inserted a record into the linked table (tblWebComs), I want
the user to be able to delete the record by simply clearing the text box.
For this purpose, I have inserted the following into the BeforeUpdate event
for the combo-box:
Private Sub txtWebComType_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtWebComType) Then
Dim strMsg As String
Dim strTitle As String
Dim strSQL As String
strMsg = "Would you like to delete this entry?"
strTitle = "Delete WebCommunication Record"
If MsgBox(strMsg, vbQuestion Or vbYesNo Or vbDefaultButton2,
strTitle) _
= vbYes Then
CurrentDb.Execute "DELETE * FROM WebComs WHERE
[WebComs].[WebComID]=" & Me.ContactWebComs_WebComID & "", , dbFailOnError
Else
Cancel = True
Me.Undo
End If
End If
End Sub
The subform is linked to my main form (Clients) using this query string
auto-generated by Access:
SELECT [ContactWebComs].[ContactID], [ContactWebComs].[WebComID] AS
ContactWebComs_WebComID, [WebComs].[WebComID] AS WebComs_WebComID,
[WebComs].[WebComType], [WebComs].[WebComDetails] FROM WebComs INNER JOIN
ContactWebComs ON [WebComs].[WebComID]=[ContactWebComs].[WebComID];
The Child/Master link fields is ContactsID which appears in my contacts
table (the record source for the main form) and my junction table
(ContactWebComs).
When I run the BeforeUpdate code, I experience two problems:
(1) When I clear the combo-box and shift the focus (eg to the text box
beside it), the message box pops up to confirm the delete as expected. But
after that, nothing happens until I change focus again (eg tab to the next
row).
(2) Once I change focus a second time, a message pops up saying that the
record has been deleted - but as soon as I press okay on this message box,
Access just shuts down - no error messages or anything, the entire
application just shuts down.
After I restart the application and check the entries in tblWebComs, I can
see that the record I selected was in fact deleted.
I've tried a couple of variations for delete command.
For instance, I've tried hard coding the WebComID to delete like so:
CurrentDb.Execute "DELETE * FROM WebComs WHERE
[WebComs].[WebComID]=20", dbFailOnError
Again, the record is actually deleted, but I get exactly the same errors.
I've also tried running the following code instead of the currentdb.execute
line:
strSQL = "DELETE * " & _
"FROM WebComs " & _
"WHERE [WebComs].[WebComID]=" & Me.ContactWebComs_WebComID
& ""
DoCmd.SetWarnings False ' Turn off Access warning messages
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True ' Warning set on again
This results in exactly the same problems - and a correctly deleted record.
If I run the above code without the SetWarnings lines, the following
happens:
(a) my custom message box to popup;
(b) then Access's own message box warning that I am about to delete a record
from the specified table pops up;
(c) then nothing happens until I change focus to another part of the form;
(d) then a box pops up saying that "record is deleted"
(e) then all the rows in my subform disappear except for the blank "new
record" line
(f) a check of the table confirms that the record was correctly deleted.
The difference here is that Access does not shut down, but I can't stop
access from deleting all my rows. If I try to do incorporate a Me.Requery
after the outside "If ... Then" statement, Access complains that the line is
preventing it from saving anything to the database. If I try a
"Me.txtWebComType.Requery" line instead, I get an error saying that "You
must save the current field before you run a query action". If I insert a
"Me.txtWebComType.Requery" line in the AfterUpdate event instead, I end up
with the same problems described above.
I all out of ideas. Does anyone else have any pointers as to what might be
wrong with my code, or what might be causing these problems?
I should mention that the DoCmd code above worked fine when was
(incorrectly) deleting entries in my junction table (ContactWebComs) instead
of my WebComs table. (Please note, I have cascade update and delete
selected for all relationships).
TIA
Bob