1. Change "Private KeyCurrent As Integer" to "Dim KeyCurrent As Integer" (Use
the Private... only if you put this line in the General section at the top of
your module, not in the Click sub itself). Make sure that this declaration
matches the data type of your primary key. I am assuming the primary key for
the table that is the RecordSource of this form is the StudentID field and
that there is a control on the form that is bound to this field (if there is
not, create one and hide it so that the value will be readily accessible but
will not confuse the user). If StudentID is an integer, make this "Dim
KeyCurrent As Integer"; if StudentID is a string, make this "Dim KeyCurrent
As String" instead. If StudentID is a Long Integer, use "Dim KeyCurrent As
Long". You might even get away with just "Dim KeyCurrent".
2. "PrimaryKeyName" was just a placeholder - replace it with the name of the
control on the current form that is bound to the primary key in the
underlying table; probably "StudentID".
So, it should probably look like this:
Private Sub Command25_Click()
On Error GoTo Err_Command25_Click
Dim KeyCurrent As Integer
KeyCurrent = StudentID
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Me.Requery
Me.RecordsetClone.FindFirst "[StudentID] = " & StudentID
Me.Bookmark = Me.RecordsetClone.Bookmark
KeyCurrent = Null 'clears the value after finding the record
Exit_Command25_Click:
Exit Sub
Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click
End Sub
If StudentID is a string instead of an Integer, then the FindFirst line
should read thus:
Me.RecordsetClone.FindFirst "[StudentID] = '" & StudentID & "'"
JohnB said:
Hi Brian. I think I've added your code correctly, see below, but I get a
Compile Error when I try to use it. It says "Invalid Attribute in Sub or
Function" and when I click OK, the word "Private" is highlighted. Perhaps I
should say that I'm trying this out on a sample Access 97 mdb at home but
want to use it on an Access 2000 mdb in work. Can you see what's wrong?
Thanks again, JohnB
Private Sub Command25_Click()
On Error GoTo Err_Command25_Click
Private KeyCurrent As Integer 'make the type match the primary key data type
KeyCurrent = PrimaryKeyName
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Me.Requery
Me.RecordsetClone.FindFirst "[StudentID] = " & KeyCurrent
Me.Bookmark = Me.RecordsetClone.Bookmark
KeyCurrent = Null 'clears the value after finding the record
Exit_Command25_Click:
Exit Sub
Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click
End Sub
Bookmark. Record the current record's primary key (to a variable) before
running your code, then return to it after the code runs.
Put this in the General portion of the form's module (sets the variable
name/type):
Private KeyCurrent as Integer 'make the type match the primary key data
type
Code for the button click:
Private Button1_Click()
KeyCurrent = PrimaryKeyName
<Enter your other code here>
Me.RecordsetClone.FindFirst "[PrimaryKeyName] = " & KeyCurrent
Me.Bookmark = Me.RecordsetClone.Bookmark
KeyCurrent = Null 'clears the value after finding the record
End Sub
PrimaryKeyName is the name of the control on the form that is bound to the
primary key field.
:
Hi.
I have the following code in a command buttons On Click event. The form
that
it sits on has a control sorce showing two tables in a one-to-many
relationship. When the button is clicked, the many sided record is
deleted,
which is what I want. I added the Me.Requery line to get the 'one' sided
record to reshow and this works but the records are reset to show the
first
record. Is there any way I can requery the main recordset while staying
on
the record that was being viewed when the command button was clicked?
Thanks for any help, JohnB
Private Sub Command25_Click()
On Error GoTo Err_Command25_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Me.Requery
Exit_Command25_Click:
Exit Sub
Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click
End Sub