PDA

View Full Version : GoToRecord Runtime Error 2105



Monobi
09-22-2009, 03:20 AM
Hi all,

For the past week or so I have been creating an Access database a small amount each day. For the past two days I've been debugging the code which I have written and am down to the last bug.

I have a main form displaying the Purchase Order's date, ID, corresponding supplier and a subform displaying the items on the order.


Private Sub txtPO_BeforeUpdate(Cancel As Integer)
Dim Message
'Check if the change exists
If Not IsNull(DLookup("[txtPO]", "[Purchase Orders]", _
" [PO Number] = " & [txtPO].Value)) Then
DoCmd.GoToRecord , , acGoTo, [txtPO].Value 'On change, set that number to display
Else
'If not, inform user and reset box
Message = MsgBox("Specified Purchase Order does not exist.")
Cancel = True
End If
End Sub


txtPO is a text box set to the autonumber primary key of the first table (the second being the document items.)

I get a runtime error 2105 - "Cannot go to specified record." When debugging, the record exists, the syntax follows orders... I'm wondering if there is something unmentioned going wrong?

Thanks for any help,
Ben

CreganTur
09-22-2009, 05:17 AM
Welcome to the forum- it's always good to see new members.


I get a runtime error 2105 - "Cannot go to specified record." When debugging, the record exists, the syntax follows orders... I'm wondering if there is something unmentioned going wrong?


If by "when debugging" you mean that you are stepping through your code using the F8 key, then this error is normal. VBA locks out the GoToRecord command when you are debugging, which means you will always get this error. If you run your code normally, without any breakpoints and without debugging, you will see that it works as expected and moves to the record you want.

Regarding the code snippet you posted, I have one thing to point out:
Dim Message
...
Message = MsgBox("Specified Purchase Order does not exist.")
...

Why are you creating a variable named Message? You aren't using it to check the value of the messagebox... in fact there is nothing to test with this message box- by default you are only giving your users the Ok button to use. This code will work without the unused variable:


Private Sub txtPO_BeforeUpdate(Cancel As Integer)
'Check if the change exists
If Not IsNull(DLookup("[txtPO]", "[Purchase Orders]", _
" [PO Number] = " & [txtPO].Value)) Then
DoCmd.GoToRecord , , acGoTo, [txtPO].Value 'On change, set that number to display
Else
'If not, inform user and reset box
MsgBox("Specified Purchase Order does not exist.")
Cancel = True
End If
End Sub

Monobi
09-22-2009, 05:29 AM
Hey Randy,

Thanks for the help!

I took out the message box variable and still works like a charm when I try to search for a non-existent record.

However, by debugging I mean running each piece of code I have written to test the bugs and remove them.
For this particular code I input a number into the text box and press enter - the text box has been set to hold numbers. I was unaware of being able to continue once an error has occurred by pressing F8, thanks!

I have only started VBA coding in the last fortnight but it seems to be going well.. This is the first major problem I've had and I'm almost done.

Thanks again!
Ben