SetFocus after MsgBox vbOKOnly - Then Exit Sub

J

JK

Access 2003

In a subform (Workorder Vendor) I want to allow the user to double click
ProductID to add a new Vendor Part. The vendor parts in the combo box of the
subform are dependant on a combo box on the main form (Workorders.) So,
before a user can add a new Vendor Part by double clicking the productID on
the subform, they must select a Vendor on the main form.

For the most part, the following code works. I'm just having trouble with
the setfocus part of it - for some reason it doesn't work.

The error says I can't move the focus to that field - any idea why?

Private Sub ProductID_DblClick(Cancel As Integer)
If IsNull(Forms![Workorders].Form![txtSupplierID] Or
[Forms]![Workorders].Form![txtSupplierID] = "") Then
MsgBox "You must select a vendor before adding a Vendor Part.",
vbOKOnly
Forms![Workorders].Form![txtSupplierID].SetFocus
End If
Exit Sub
If vbYes = MsgBox("Do you want to add a new Vendor Part?", vbYesNo +
vbQuestion + vbDefaultButton2, gstrAppTitle) Then
' Open the add a Vendor Part form
DoCmd.OpenForm "Vendor Parts Entry Form", DataMode:=acFormAdd,
WindowMode:=acDialog
' Requery the combo on return in case a row actually added
Me.ProductID.Requery
' Expose the new list of Vendor Parts
Me.ProductID.Dropdown
End If
End Sub
 
J

JerryData

first, this is a nice thing to know, testing if a form is loaded:
Function test()
Dim dform As AccessObject
If CurrentProject.AllForms("frmBrand").IsLoaded = False Then Debug.Print "Hi!"
End Function

Second you have to cascade to the object, like:
DoCmd.SelectObject acForm, "frmPurchasing"
Forms!frmPurchasing.SetFocus
Forms!frmPurchasing!subfrmPO.SetFocus
Forms!frmPurchasing!subfrmPO!whosit.SetFocus

good luck!
 
K

Klatuu

None of this is necessary:
Second you have to cascade to the object, like:
DoCmd.SelectObject acForm, "frmPurchasing"
Forms!frmPurchasing.SetFocus
Forms!frmPurchasing!subfrmPO.SetFocus
Forms!frmPurchasing!subfrmPO!whosit.SetFocus

All you nee is

Forms!frmPurchasing!subfrmPO.Form!whosit.SetFocus

--
Dave Hargis, Microsoft Access MVP


JerryData said:
first, this is a nice thing to know, testing if a form is loaded:
Function test()
Dim dform As AccessObject
If CurrentProject.AllForms("frmBrand").IsLoaded = False Then Debug.Print "Hi!"
End Function

Second you have to cascade to the object, like:
DoCmd.SelectObject acForm, "frmPurchasing"
Forms!frmPurchasing.SetFocus
Forms!frmPurchasing!subfrmPO.SetFocus
Forms!frmPurchasing!subfrmPO!whosit.SetFocus

good luck!
--
-Jerry
JR Data Inc.


JK said:
Access 2003

In a subform (Workorder Vendor) I want to allow the user to double click
ProductID to add a new Vendor Part. The vendor parts in the combo box of the
subform are dependant on a combo box on the main form (Workorders.) So,
before a user can add a new Vendor Part by double clicking the productID on
the subform, they must select a Vendor on the main form.

For the most part, the following code works. I'm just having trouble with
the setfocus part of it - for some reason it doesn't work.

The error says I can't move the focus to that field - any idea why?

Private Sub ProductID_DblClick(Cancel As Integer)
If IsNull(Forms![Workorders].Form![txtSupplierID] Or
[Forms]![Workorders].Form![txtSupplierID] = "") Then
MsgBox "You must select a vendor before adding a Vendor Part.",
vbOKOnly
Forms![Workorders].Form![txtSupplierID].SetFocus
End If
Exit Sub
If vbYes = MsgBox("Do you want to add a new Vendor Part?", vbYesNo +
vbQuestion + vbDefaultButton2, gstrAppTitle) Then
' Open the add a Vendor Part form
DoCmd.OpenForm "Vendor Parts Entry Form", DataMode:=acFormAdd,
WindowMode:=acDialog
' Requery the combo on return in case a row actually added
Me.ProductID.Requery
' Expose the new list of Vendor Parts
Me.ProductID.Dropdown
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top