Hi John
I don't know what is causing your problem. I would not have implemented this
in the same way - everyone does it the way they have learned!
In general, I woudn't use DoCmd.DoMenuItem to delete a record from a subform
- mainly because I hadn't heard of it! I would create a recordset containing
the record I wanted to delete, delete it, and then requery the subform to
reflect the change.
To create the recordset, first create a SQL string which would select only
the record you want to delete, based on its primary key, for example:
Dim SQLString As String
SQLString = "SELECT [Person ID] FROM People WHERE [Person ID] = " &
[Forms]![Name of Form]![Name of Subform].Form![Name of Control Holding Person
ID]
This is much easier to do by creating the query in the query editor, going
to SQL View, and copying and pasting the SQL out.
To create the recordset itself:
Dim RecordToDelete As DAO.Recordset
Set RecordToDelete = CurrentDb.OpenRecordset("SQLString")
You need to set a reference to the DAO library in order to do this. In the
VB Editor, go to Tools - References and tick Microsoft DAO 3.6 Object
Library. If you're not familiar with DAO, I recommend it as it makes
operations on records much easier.
Then delete the record:
With RecordToDelete
.Delete
.Close
End With
It's important to use the Close method, as otherwise the recordset is not
released and clogs up memory, or something (I'm not clear on the
technicalities).
Then requery the subform to reflect the change:
[Forms]![Name of Form]![Name of Subform].Form.Requery
I'm sorry I can't help you resolve the specific problem you have, but the
approach I have described above has always worked for me.
Others may well have better ways of doing this but it's the only way I know!
Let me know how you get on.
Cheers
David
JohnB said:
Hi David. Sorry, but I did send you a holding reply yesterday - it seems to
have vanished.
Thanks for the suggestion but Iv'e just tried it without success. The code
I'm using is as follows, in the On Click event of the delete button. Just to
expand, this button is on a subform on form frmSECStudents and field
txtFirstName is the first field on frmSECStudents. When I use the command
button, I get the same results as before - the record is deleted and then I
get "No Current Record" and the focus goes to one of the fields on the
subform, as before.
By the way, I've discovered that I can't make all of the fields on the
subform invisable -if I do, the Delete button does nothing when clicked.
Unless I can find a different way to delete the record, I'll have to leave at
least one text field visable and make it tiny.
Anyway, here is the code I'm using. Am I doing this correctly? Thanks again,
JohnB
Private Sub cmdDeleteRecord_Click()
On Error GoTo Err_cmdDeleteRecord_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
[Forms]![frmSECStudents]![txtFirstName].SetFocus
Exit_cmdDeleteRecord_Click:
Exit Sub
Err_cmdDeleteRecord_Click:
MsgBox Err.Description
Resume Exit_cmdDeleteRecord_Click
End Sub
:
Hi John
I expect you're right, you probably can't leave the focus on an empty
subform. Setting focus is easy, use:
[Forms]![Name of Form]![Name of Control].SetFocus
Incidentally, an empty textbox can accept the focus, that won't be a problem.
Cheers
David
:
Hi.
I have a small form, being used as a subform, with a Delete Record command
button (created using the Wizard). I also have a couple of text boxes which I
will make invisable when I've finished the design. When the command button is
used, it will delete just one subform record per main form record. When I use
it, I always get a message 'No Current Record' once the delete has taken
place, maybe because the focus is going to one of the text boxes and there's
no record to display. Is there a way of stopping this message appearing?
Would setting the focus to one of the main form fields work and if so, how
would I do it? Im using Access 2000 on XP.
Thanks, JohnB