Error 2105 cannot go to specified record

Gasman

Enthusiastic Amateur
Local time
Today, 22:04
Joined
Sep 21, 2011
Messages
17,342
Hi all,

I have this piece of code in two forms. The table name and form name would differ, but the rest is teh same.
Code:
Private Sub Form_Load()
Dim lngID As Long

If Not IsNull(Me.OpenArgs) Then ' Passing in the client ID means we want a new record.
    Me.ClientID = Me.OpenArgs
    lngID = Nz(DLookup("ClientID", "tblTransfer", "ClientID = " & Me.ClientID), 0)
    If lngID = 0 Then
        DoCmd.GoToRecord , , acNewRec
    End If
End If
End Sub
I have two buttons on a form to open these two forms. Up until now it had been working fine.
If the child record is not there, it allows me to add a new record. If a record is there it opens to the correct record(s)

Code:
Private Sub cmdTransfer_Click()
' Save the record first so the name appears in next form
If Me.Dirty Then
    Me.Dirty = False
End If
'If IsNull(DLookup("ClientID", "tblTransfer", "ClientID = " & Me.ClientID)) Then
    'DoCmd.OpenForm "frmTransfer", , , , , acDialog, Me.ClientID
'Else
    'DoCmd.OpenForm "frmTransfer", , , "ClientID = " & Me.ClientID, , acDialog
'End If
'Now check in form being called
DoCmd.OpenForm "frmTransfer", , , "ClientID = " & Me.ClientID, , acDialog, Me.ClientID
End Sub
As you can see I tried another method, which I've commented out as I preferred doing the check in the called form, eliminating a lot of checking in any calling form.

If I'd just created this I'd understand, but I entered a pile of data last week using this process, and unaware of making any changes to break this. :banghead:

The forms are based on tables, so a nonupdatable query is not the culprit
Just looking for tips as where to look to get to the bottom of this. If I click End to the error message the form opens normally I can continue to enter data for the new record?

Just when you think you are getting somewhere, something like this happens.:confused:

If someone has a better method, I'm open to ideas? I just wanted a button to call a form. If the child record does not exist for the parent, then add a new record with the parent id. If it does exist, then open, but still allow for new records with the parentID to be added.

TIA

Edit: Just realised a did a repair install on Office due to the Outlook 2007 spellcheck bug after the Windows 10 Creators update, but that is all I can think of. Not changed any tables etc.
 
I would check the value of Me.ClientID being passed - it could be "" which isn't null...
 
This line ...
Code:
   DoCmd.GoToRecord , , acNewRec
... is not explicit about the target of the command. You could try ...
Code:
   DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
... and name the object type, and name the form, because if you are switching forms and opening forms programmatically (and using acDialog) it's tough to be certain which form is that active form. But another strategy you can take is to set the DataEntry property of the form if you want it to open in "Add Mode", like...

Code:
Private Sub Form_Load()
   If IsNumeric(Me.OpenArgs) Then
[COLOR="Green"]      ' set to DataEntry mode if ClientID exists in tblTransfer[/COLOR]
      Me.DataEntry = DCount("*", tblTransfer", "ClientID = " & Me.OpenArgs)
      If Me.DataEntry Then Me.ClientID = Me.OpenArgs
   End If
End Sub

And as per Minty's comment, that is why--if I am expecting a Long Integer ID as an OpenArgs parameter--I check it using IsNumeric() rather than IsNull().

hth
Mark
 
Will try in the morning.
ClientID is definitely present and being passed correctly, so will try the explicit command and then the DataEntry option.

Thank you both.
 
Ok,
I tried option 1 with acdataform and form name. That made no difference, still had the error message.
Then tried the datamode option as below. This works for the data entry, but when records already exist, it just opens at the first record in the file. It does not appear to take any notice of the where criteria in the form call.? I've taken out the acdialog in that code for now and left that parameter blank.


Code:
Private Sub Form_Load()
Dim lngID As Long

'If Not IsNull(Me.OpenArgs) Then ' Passing in the client ID means we want a new record.
'    Me.ClientID = Me.OpenArgs
'    lngID = Nz(DLookup("ClientID", "tblTransfer", "ClientID = " & Me.ClientID), 0)
'    If lngID = 0 Then
'        DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
'    End If
'End If
   If IsNumeric(Me.OpenArgs) Then
      ' set to DataEntry mode if ClientID exists in tblTransfer
      Me.DataEntry = (DCount("*", "tblTransfer", "ClientID = " & Me.OpenArgs) = 0)
      'Me.DataEntry = 1
      If Me.DataEntry Then Me.ClientID = Me.OpenArgs
   End If

End Sub
[
 
OK, I *think* I have found the problem and if right, do not need the code anyway.:banghead:

This is what appears to be happening. I picked this up from https://access-programmers.co.uk/forums/showthread.php?t=236278 specifically post #2
'So, if you have a macro or VBA which is attempting to move to a new record and it is already on a new record, then that error can occur. You can put in error handling to stop that or you can fix the code that is doing it'

My code is calling the form with criteria of the ClientID, so if record exists, I want to see them, but at the same time still might want to add new records. If no records exist in the table for that client then I wanted to start adding a record.

Now it appears that due to the criteria, and no records existing, Access automatically goes into new record mode?

Does this make sense?, as I have now removed all the code for the moment from the Load event and it works as I intended it to.
I get a new record with the correct client ID if none exist. I get that from the current event
Code:
Private Sub Form_Current()
If Me.NewRecord Then
    Me.ClientID = Me.OpenArgs
End If
End Sub
and I get the linked records if they do exist.?

Why it was working perfectly fine last week I really have no idea.:banghead:


 

Users who are viewing this thread

Back
Top Bottom