Solved How do I set the focus to a component on the main from an Error Event of a subform (1 Viewer)

bmaccess

Member
Local time
Today, 05:26
Joined
Mar 4, 2016
Messages
78
Hi there. I would like to set the focus on a component cmdBookNoTitle in my main form from an Error Event handler of a subform.
The name of my subform is frmStudentBooksSubform.
The parent form is frmLearnerBaseDetails
The component on the main form is cmdBookNoTitle.
Thanks

Error Event code:
If DataErr = 3022 Then
MsgBox "Duplicate unique number.", vbInformation, "Message"
Response = 0
Me.StudentBookIssue_ID = ""
SendKeys "{esc}", True
SendKeys "{esc}", True
Exit Sub
End If
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:26
Joined
Feb 19, 2002
Messages
43,275
To set focus to the parent form, use:

Me.Parent!cmdBookNoTitle.SetFocus

But, this doesn't seem logical. Why would the book title be entered on the main form but the error thrown by the subform?
 

bmaccess

Member
Local time
Today, 05:26
Joined
Mar 4, 2016
Messages
78
To set focus to the parent form, use:

Me.Parent!cmdBookNoTitle.SetFocus

But, this doesn't seem logical. Why would the book title be entered on the main form but the error thrown by the subform?
The data from the combobox is stored as a new record on the subform. If there is already that name a duplicate error message appear and I must add a new different book. So I want the setfocus to go back to the combobox to add a different book.
Okay I added code:

If DataErr = 3022 Then
MsgBox "Duplicate unique number.", vbInformation, "Message"
Me.Parent!cmdBookNoTitle.SetFocus
Response = 0
Me.StudentBookIssue_ID = ""

'MsgBox "Please press escape", vbInformation, "Message"
SendKeys "{esc}", True
SendKeys "{esc}", True
Exit Sub
End If

1699634060965.png


1699634238567.png
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:26
Joined
Feb 19, 2002
Messages
43,275
You gave me the wrong control name. Now try the correct one.
 
Last edited:

bmaccess

Member
Local time
Today, 05:26
Joined
Mar 4, 2016
Messages
78
You gave me the wrong control name. Now try the correct one.

You gave me the wrong control name. Now try the correct one.
You gave me the wrong control name. Now try the correct one.
Okay the combobox name is cmbBookNoTitle.
1699637756420.png


If DataErr = 3022 Then
MsgBox "Duplicate unique number.", vbInformation, "Message"
Me.Parent!cmbBookNoTitle.SetFocus
Response = 0
Me.StudentBookIssue_ID = ""

'MsgBox "Please press escape", vbInformation, "Message"
SendKeys "{esc}", True
SendKeys "{esc}", True
Exit Sub
End If

1699637896618.png


Is there any reason why it cannot be moved there?
I appreciate all your help . Thanks
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:26
Joined
Feb 19, 2002
Messages
43,275
You didn't include the entire procedure so I'm assuming that the code is in the Error event.

But "" is a ZeroLengthString and should NEVER be used for numbers or dates. Change to:

Me.StudentBookIssue_ID = Null

Also get rid of the send keys. If you want to clear the all of the subform record, USE:
Me.Undo and do it BEFORE you move focus to the main form or you'll keep getting the error. Using Me.Undo means you don't need to clear individual controls and it doesn't matter what data type they are.

This is probably the issue. You are trying to set focus to the main form without clearing the error in the subform.
 

bmaccess

Member
Local time
Today, 05:26
Joined
Mar 4, 2016
Messages
78
You didn't include the entire procedure so I'm assuming that the code is in the Error event.

But "" is a ZeroLengthString and should NEVER be used for numbers or dates. Change to:

Me.StudentBookIssue_ID = Null

Also get rid of the send keys. If you want to clear the all of the subform record, USE:
Me.Undo and do it BEFORE you move focus to the main form or you'll keep getting the error. Using Me.Undo means you don't need to clear individual controls and it doesn't matter what data type they are.

This is probably the issue. You are trying to set focus to the main form without clearing the error in the subform.
Hi there. Thanks you very much I will try the suggestions.
 

bmaccess

Member
Local time
Today, 05:26
Joined
Mar 4, 2016
Messages
78
Hi Pat. Thank you very much. After the changes you suggested the following code WORKS PERFECTLY.

Me.Parent!cmbBookNoTitle.SetFocus

Thanks once again. I appreciate your help. You are Brilliant.
 

Users who are viewing this thread

Top Bottom