Link to home
Start Free TrialLog in
Avatar of Joel Pomerantz
Joel PomerantzFlag for United States of America

asked on

code to set focus on open to placeholder record on datasheet subform

Hello! I have a subform that opens in datasheet view. I'm looking for a way to set the focus on open to the placeholder record so that users don't accidentally overwrite previous data.  I'm assuming this would be a pretty simple code but I don't know what you "call" the blank "placeholder" record :) thanks!!
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Private Sub Form_Load()
Docmd.RunCommand acCmdRecordsGoToNew
End Sub

mx
Avatar of Joel Pomerantz

ASKER

cool! ok, that works perfectly to set it when the form orriginally loads - but this datasheet is a subform linked to parent form by ID number... is there a way to keep the focus on the new record as the user scrolls (on the parent form) to different records? I tried on got focus, on activate, and a few others and can't figure out which event it would be! I'm assuming this is just a matter of putting your code into the correct event right?
Sure ... sorry ... put this in the On Current of the Main form:

Private Sub Form_Current()
Me.YourSubFormControlName.SetFocus.
Me.YourSubFormControlName.Form.SetFocus.
Docmd.RunCommand acCmdRecordsGoToNew

End Sub
Here you go:

put this in the On Current of the Main form:

Private Sub Form_Current()
Me.YourSubFormControlName.SetFocus
Docmd.RunCommand acCmdRecordsGoToNew

End Sub

I just tested this.

mx
cool! - it works great for me as well - there is just one bug I think... when I get to the end of the records (scrolling with command buttons) or, when I try to navigate to a record using the lookup combo box - it gives me the following error:
Runtime Error 2046:
the command or action 'RecordsGoToView' isn't available now

is there something we can add to fix that?
I really really appreciate your help!!
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
second method was the one that worked.  I can't thank you enough!!!

mx, I hope that this is not a violation of EE protocol, but can I ask you to help me with
https://www.experts-exchange.com/questions/23849951/looking-for-function-to-search-for-a-specific-value-in-a-table-field.html

I'm not having much luck with that one and I think it's right up your alley :)  
worked perfectly -- thanks so so so much for your time!!!
uh oh - slight side effect... I'm going to open a new question for ya - but I wanted to give you a heads up here. thanks so much mx