Inconsistency in passing a value to another form (openargs)

MrNymm

New Member
Joined
Oct 5, 2016
Messages
5
Hi,

I'm working on a project which contains three forms:

The first form ("frmMain") contains a number of buttons, one of which serves to open the second form in DataEntry mode.

The second form ("frmAudits") contains a few text-, combo- and checkboxes, all bound to the table tblAudits. It also contains a listbox and three buttons, cmdAdd, cmdEdit and cmdDelete. The listbox is unbound. Its rowsource is programmatically set to a query for all related records in another table (tblIssues), i.e. records with a Audit_foreign_key matching the ID of the current record in tblAudits.

When the user clicks cmdEdit, the third userform ("frmIssues") is opened to the record selected in the listbox:

Code:
DoCmd.OpenForm "frmIssues", , , "[Audit_foreign_key]=" & Me.lstIssues.Value, acFormEdit, acDialog
    Me.AllowDeletions = False
    Me.AllowAdditions = False
    Me.AllowEdits = True

When the user clicks cmdAdd, the third userform is opened in DataEntry mode:

Code:
[If Me.NewRecord Then Me.Dirty = False 'not doing this results in an error message on frmIssues, because of referential integrity. Is there a better way to solve this?
    DoCmd.OpenForm "frmIssues", , , , acFormAdd, acDialog, Me.ID/CODE]

The "Me.ID" serves to pass the ID of the current record in tblAudit to be used in the Load event of the third form:

[CODE]If Me.DataEntry Then
    If Me.OpenArgs <> vbNullString Then
        Me.Controle_FK = Me.OpenArgs
    Else
        MsgBox "An unexpected error has occurred.", vbCritical + vbOKOnly, "Need to fix this!"
        DoCmd.Close acForm, Me.Name, acSaveNo
    End If
Else
    Me.AllowDeletions = False
    Me.AllowAdditions = False
    Me.AllowEdits = True
End If


For some reason, the behavior of the cmdAdd button is inconsistent. Sometimes, I open the second form, click the button - either right away or after doing some other action on the form, like completing a textbox - and the third form will load in DataEntry mode as expected. But other times, I get the "Need to fix this!" error message, because the me.ID that gets passed to the third form is Null. Yet clicking the button a second time, even right after getting the error, always works. Also, when i step through the code in debugging mode, the issue never presents itself.

Any thoughts on what is causing this?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
1. This belongs to the Access Questions section.

2. While debugging the steps - check the actual values that are passed over.

3. You only save the record is it is a new one - what if it is just changed, but not new?

4. I must admit - I am a bit confused? You open the Form in Data Entry Mode (ONLY FOR ADDING NEW RECORDS - it avoids loading the full recordset). But from what you say I think you are trying to open and edit existing records?
However It seems like you know what you're doing.
I believe ID is of type long. Normally you would get Null if the record is new and is not yet saved.
Try to add a line and see what the result will be - can you get a pattern - when does this problem appear: Something like:
Code:
Debug.Print Me.ID, Me.recordset.EOF, me.recordset.bof, Me.NewRecord
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,839
Members
449,193
Latest member
MikeVol

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top