Solved setfocus to field in subform directly

FahadTiger

Member
Local time
Today, 21:10
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.
 
1. All forms need some validation. At a minimum, you ensure that certain fields are not null or ZLS and dates are not wonky. 2/16/205 may be valid but unless you are recording historical events, it is irrational. So start by adding validation to your main form. In the form's BeforeUpdate event make sure that the name is not null and also not = ZLS. Best way to handle ZLS is to set the allowed property to No for any text field that is required since this prevents them from being appended via queries also. Then you only have to validate for null in your form's BeforeUpdate event.
2. Depending on your version of Access, you may or may not be allowed to dirty a subform where there is no linked mainform record already saved. Several ways to handle this starting by hiding the subform until the record in the main form has been started. OR, add code to the BeforeInsert event of the subform to check the value of the mainform's ID field. If it is null, cancel the update, use Me.undo to undo the typing and move focus back to the main form - Me.Parent!CustName.SetFocus.
3. Always make certain that the subform's master/child links are properly set at the time you add the subform. Access generally does this for you but only when you define relationships using the relationship window. In some versions it might "guess" at a relationship if it finds column names that are identical. But, defining RI is best for your application and the security of your data and you do it BEFORE you enter a single record. It should be an unconscious step. You shouldn't even have to think about it. Anyone who tells you otherwise is no expert.
 
Last edited:
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