As a sample method there is a Text Box and a Command Button on the first Form. Employee Code is entered into the Text Box and the user clicks on the Command Button to Open the Employees Form. The Command Button-Click Event opens the Employees Form and passes the EmployeeID as OpenArgs. If the Text Box is empty then the Employees Form is open normally.
Code:
Private Sub Command2_Click()
Dim i, frm As Access.Form
i = Nz(Me![Text0], "")
If Len(i) > 0 Then
DoCmd.OpenForm "Employees", acNormal, , , , , i
Else
DoCmd.OpenForm "Employees", acNormal
End If
End Sub
On the Employees Form_Load() Event, if employee id is received as OpenArgs then the recordsetclone is searched for a match and if found make that record current on the form, by copying the the recordset.bookmark to the Employees Form's bookmark. This action will make the searched record current on the Form.
If OpenArgs is Null then the Employees Form creates a new record on the Form.
Code:
Private Sub Form_Load()
Dim EmpID, rst As Recordset
EmpID = Nz(Me.OpenArgs, 0)
If EmpID > 0 Then
Set rst = Me.RecordsetClone
rst.FindFirst "EmployeeID = " & EmpID
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
End If
rst.Close
Else
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
End If
End Sub
Using your code (which fixed the problem with the Report BTW - THANKS!) is causing some other odd problems. Here is exactly what I'm using:
Code:
Private Sub Form_Load()
On Error GoTo Form_Load_Err
FilterList
Call FormStartUp: Call IssueRewards
Dim CusID, rst As Recordset
CusID = Nz(Me.OpenArgs, 0)
If CusID > 0 Then
Set rst = Me.RecordsetClone
rst.FindFirst "ID = " & CusID
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
End If
rst.Clone
Else
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
End If
Form_Load_Exit:
Exit Sub
Form_Load_Err:
MsgBox Error$
Resume Form_Load_Exit
End Sub
I get an error stating that the Command or action 'GoToRecord' isn't available now. When I click Ok on the error message, the form opens to the first record again.
This error pops up using this code to open the form:
Code:
Private Sub cmdCustomers_Click()
DoCmd.OpenForm "frmRewards", acNormal
End Sub
I have a feeling I'm being an idiot and typed out something wrong somewhere.
Using your code (which fixed the problem with the Report BTW - THANKS!) is causing some other odd problems. Here is exactly what I'm using:
Code:
Private Sub Form_Load()
On Error GoTo Form_Load_Err
FilterList
Call FormStartUp: Call IssueRewards
Dim CusID, rst As Recordset
CusID = Nz(Me.OpenArgs, 0)
If CusID > 0 Then
Set rst = Me.RecordsetClone
rst.FindFirst "ID = " & CusID
If Not rst.NoMatch Then
Me.Bookmark = rst.Bookmark
End If
rst.Clone
Else
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
End If
Form_Load_Exit:
Exit Sub
Form_Load_Err:
MsgBox Error$
Resume Form_Load_Exit
End Sub
I get an error stating that the Command or action 'GoToRecord' isn't available now. When I click Ok on the error message, the form opens to the first record again.
This error pops up using this code to open the form:
Code:
Private Sub cmdCustomers_Click()
DoCmd.OpenForm "frmRewards", acNormal
End Sub
I have a feeling I'm being an idiot and typed out something wrong somewhere.
There must be something else causing it. I changed it to rst.Close and I'm getting the same error:
After clicking "Ok", frmRewards opens, but it opens to the first record in the table.
This is the code behind the Command Button used to open the form to "new":
Code:
Private Sub cmdCustomers_Click()
DoCmd.OpenForm "frmRewards", acNormal
End Sub
Worked perfectly. Did a little step-by-step debugging and found that this is the function causing the problems:
Code:
Public Function FormStartUp()
DoCmd.NavigateTo "acNavigationCategoryObjectType"
'DoCmd.RunCommand acCmdWindowHide
End Function
The idea of this is to hide the menu windows (F11 menu) when a form starts up. It obviously doesn't like the non-commented-out line "DoCmd.NavigateTo "acNavigationCategoryObjectType". I picked this little function up from somewhere else and personally I never used the first line, so I'm not entirely sure what it even does lol.
When the Form is open it goes through several phases of Actions/Events internally, like the following:
Open → Load → Resize → Activate → Current
One of the actions in these phases of actions is loading the Form's RecordsetClone with BookMark for each record. This process is interrupted by calling other Sub-Routines at the beginning. I think this is the reason for the Error.
You can overcome this problem by shifting those Sub-Routine calls at the end of Form_Load() Event Procedure.