Solved setfocus to field in subform directly

FahadTiger

Member
Local time
Today, 17:14
Joined
Jun 20, 2021
Messages
120
Hi...
I have a customerNamer field in the main form ..When the user makes a mistake and does not enter the customer name... Of course the ID of the main form does not change And when I enter the itemName in the subform... Then I notice that I did not enter the customer name in the main form ..When I enter the customer name to give me New Record... All records of the subform will be deleted ..Of course I had to make the focus when loading the form on the itemName in the subform...But the problem is that it does not give me a new record number in the main form unless I enter the customer name...Of course this is correct... It does not give a new number except through a text box from the same mainform ..Should I use Dlast and not GotoNewRecord Or is there another solution?
thank you so much..
 
You could have the subform be invisible until the CustomerName is entered/selected on the main form. You would probably want to make the subform visible in the On Current if the CustomerName wasn't blank.
 
Are you sure records in subform are deleted or just no longer displayed? Have you set a relationship in Relationships and enforced referential integrity? Why set focus to subform when opening if not opening to an existing customer?

Post code that is opening form or provide db for analysis.
 
Does not sound like your subform is correctly linked to the main form. The subform should be linked to the main form by customerID. Then you should only be able to add a new item if you have a customer id in the main form. You need to ensure referential integrity is enforced between these tables since an Item cannot exist without a related customer.

Personally I never have a main form where I add new customers and on the same form a subform. To me that is too much going on in one form. I would normally have a pop up to add the new customer. But if you are allowing both the ability to create child records and add new customers on the main form then do something like @DHookom advises.
 
Are you sure records in subform are deleted or just no longer displayed? Have you set a relationship in Relationships and enforced referential integrity? Why set focus to subform when opening if not opening to an existing customer?

Post code that is opening form or provide db for analysis.
There is relationship between main and subform ..
i used this code on load event in main..
Private Sub Form_Load()
Me!PurchaseDetailF.SetFocus ' Set focus to the form first
Me!PurchaseDetailF.Form!ItemName.SetFocus ' Set focus to the ItemName field inside the subform

End Sub
 
You could have the subform be invisible until the CustomerName is entered/selected on the main form. You would probably want to make the subform visible in the On Current if the CustomerName wasn't blank.
yes.. i was do it in one my db... or i will make one form
 
Just as a side comment not intended as a criticism: When setting focus to a field on the sub-form, you try to set focus to the sub-form control after which you set it explicitly to a control within the sub-form. (And you DO use the correct syntax to select that control.)

I don't know that it would make any difference to put focus on the sub-form control, though it DOES have a .SetFocus method attached to it. The rule still applies, even for sub-form controls, that if it isn't enabled, you can't set focus to it anyway. I would think that as long as the subform's form is visible and the sub-form.Form.Enabled property is true, you could just directly set the focus to the desired control. I.e. I don't see the need for the intermediate .SetFocus here.
 

Users who are viewing this thread

Back
Top Bottom