Full Version: Go To New Record Error?
UtterAccess Forums > Microsoft® Access > Access Forms
Sam_R
I'm using Access 2007
I have a form displaying fields from a table. I want to create a command button allowing the user to create a new record. Macros won't work because I need to set some conditions that will check for specific blank fields on the currently displayed record. I'll add the appropriate IF-THEN statement later, but now I simply want it to go to a new record using VBA.
CODE
DoCmd.Close acForm, "FormName"
DoCmd.OpenForm "FormName"
DoCmd.GoToRecord , , , acNewRec

The above code generates a run-time error 2105: You can't go to the specified record. I guess I could simply go through and set all the displayed fields to null, but there are 12 fields on the form and so that seems like the long way around. And setting the fields to null would destroy data when I later add buttons that allow the user to browse the records.
Doug Steele
Where is that code running? Wherever you've got it defines the scope for the GoToRecord. In other words, despite the fact that you've just opened form "FormName", your GoToRecord has no connection to that form.
Sam_R
The code is running under the click event of my "Create New Record" command button.
So, what you are saying is that I need to specify somehow the form name in the DoCmd.GoToRecord statement?
Sam_R
I think I got it:
!--c1-->
CODE
DoCmd.GoToRecord acDataForm, "FormName", acNext

Thanks for pointing me in the right direction, Doug.
vtd
Why do you you need to close your Form and then re-open the same Form straight away?
There should not be any reason to do so... If you need Access to perform some process that is achieved by closing and opening the Form, it is almost certainly possible to do by some other code more efficient than the posted code that closes and then re-opening the same Form straight away.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.