Solved Form problems with OpenArgs (1 Viewer)

BeardedSith

Member
Local time
Today, 07:35
Joined
Feb 5, 2020
Messages
73
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.
 

apr pillai

AWF VIP
Local time
Today, 17:05
Joined
Jan 20, 2005
Messages
735
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.
Change the statement: rst.Clone to rst.Close. Besides that I think the code looks ok.
 

BeardedSith

Member
Local time
Today, 07:35
Joined
Feb 5, 2020
Messages
73
Change the statement: rst.Clone to rst.Close. Besides that I think the code looks ok.
There must be something else causing it. I changed it to rst.Close and I'm getting the same error:
1583843863100.png

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

could "acNormal" be the problem?
 

apr pillai

AWF VIP
Local time
Today, 17:05
Joined
Jan 20, 2005
Messages
735
Disable the On Error Goto statement;
Code:
'On Error GoTo Form_Load_Err
in the Form_Load() Event Procedure, to put the code in debug mode and try again. Disable the following lines also for test run.
Code:
'FilterList
'Call FormStartUp: Call IssueRewards
 

BeardedSith

Member
Local time
Today, 07:35
Joined
Feb 5, 2020
Messages
73
Disable the On Error Goto statement;
Code:
'On Error GoTo Form_Load_Err
in the Form_Load() Event Procedure, to put the code in debug mode and try again. Disable the following lines also for test run.
Code:
'FilterList
'Call FormStartUp: Call IssueRewards
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.
 

apr pillai

AWF VIP
Local time
Today, 17:05
Joined
Jan 20, 2005
Messages
735
The following Commands pair, Selects the Navigation Pane (or activates it) first, then the second Command Hides it.

Code:
'following Command selects the Navigation Pane
DoCmd.NavigateTo "acNavigationCategoryObjectType

'This Command Hides the Nagivation Pane
DoCmd.RunCommand acCmdWindowHide

The following Command will bring back the Navigation Pane:

Code:
DoCmd.SelectObject acTable, , True
 

BeardedSith

Member
Local time
Today, 07:35
Joined
Feb 5, 2020
Messages
73
The following Commands pair, Selects the Navigation Pane (or activates it) first, then the second Command Hides it.

Code:
'following Command selects the Navigation Pane
DoCmd.NavigateTo "acNavigationCategoryObjectType

'This Command Hides the Nagivation Pane
DoCmd.RunCommand acCmdWindowHide

The following Command will bring back the Navigation Pane:

Code:
DoCmd.SelectObject acTable, , True
Any reason to believe those command could bug out the form like I've been having problems with?
 

apr pillai

AWF VIP
Local time
Today, 17:05
Joined
Jan 20, 2005
Messages
735
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.
 

BeardedSith

Member
Local time
Today, 07:35
Joined
Feb 5, 2020
Messages
73
That did the trick! I put 'Call FormStartUp' after all records are being looked up and it worked just fine. Thanks a ton!
 

Users who are viewing this thread

Top Bottom