How to set the focus on the current record in a datasheet

desgordon

New member
Local time
Today, 01:00
Joined
Aug 20, 2009
Messages
6
I am creating an application with a parent form and a subform that contains a datasheet. When a button on the main form is clicked I want to initialize certain fields on a new record in the datasheet then set the focus to the next field. I use the following code:

Private Sub NEW_BUILDING_OPTIONS_Click()

HH_Subform.Controls.Item(6).DefaultValue = ""
HH_Subform.Controls.Item(8).DefaultValue = 25
HH_Subform.Controls.Item(10).DefaultValue = 75

HH_Subform.Controls.Item(12).SetFocus

End Sub

The first three lines does what I want it to do i.e. initialize some fields of a new record to some values I want. The other line however sets the focus on the correct field, but on the first record of the recordset and not on the new record as I want.

Can somebody help with this?
 
The first three lines does what I want it to do i.e. initialize some fields of a new record to some values I want.
Actually that is not a correct description of what your code is doing. Unfortunately your code is not doing what you want. There is not a new record created with your code. You are only setting the default value for the controls for when a new record is created.

Now where is your code to you actually create the new record. Until you create the new record, you can not set the focus to a control in the new record.
 
HiTechCoach,

You are probably very right. I am struggling on how to create the new record and have changed the code somewhat (with help from other sources):

Private Sub NEW_BUILDING_OPTIONS_Click()

HH_Subform![BUILDING_NUMBER].SetFocus
HH_Subform![BUILDING_NUMBER].Value = "1001"
HH_Subform![DWELLING_UNIT_NUMBER].Value = "2002"
HH_Subform![HOUSEHOLD_NUMBER].Value = "2002"
HH_Subform![FULL_ADDRESS].SetFocus

End Sub

I am having some difficulty using the
DoCmd.GoToRecord

method to create a new record before the initialization begins.
Can you offer some help there bearing in mind that my command
button is on the main form and not the subform.
 
I have solved this problem with help from another forum. For anybody that might be interested the code on the command button is :

[HH Subform].SetFocus
DoCmd.GoToRecord , , acNewRec
[HH Subform]![BUILDING_NUMBER] = 1001
[HH Subform]![DWELLING_UNIT_NUMBER] = 2002
[HH Subform]![HOUSEHOLD_NUMBER] = 2002
[HH Subform]![FULL_ADDRESS].SetFocus


Thanks to all who helped.
 

Users who are viewing this thread

Back
Top Bottom