Link to home
Start Free TrialLog in
Avatar of ClaudeWalker
ClaudeWalker

asked on

Setfocus on control in parent form from subform

I have a subform where I want to goto a control on the parent form and make the subform invisible.

I have been trying to set the focus to the parent form and then using a number of different methods to goto that control:

Me.Parent.SetFocus
Forms("frmCommonItemEditor").Controls("cboFilter").SetFocus

forms!frmCommonItemEditor.setfocus
docmd.gotocontrol "cboFilter"

forms("frmCommonItemEditor").setfocus
forms!frmCommonItemEditor!cboFilter.setfocus

and none seem to work.

Any ideas,
JOe K.



ElseIf Me.txtChangeInPrice <> 0 And Me.txtNewPrice <> 0 Then
        If MsgBox("Are you done changing the bid price for " & Me.cboItem.Column(1) & "?", _
                  vbYesNo, "Are you done") = vbYes Then
            Cancel = False
            Me.Parent.SetFocus
            Forms("frmCommonItemEditor").Controls("cboFilter").SetFocus
            
            Me.Visible = False

Open in new window

Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Well, for starters you can't set the active control not visible.

Start with this:

ElseIf Me.txtChangeInPrice <> 0 And Me.txtNewPrice <> 0 Then
        If MsgBox("Are you done changing the bid price for " & Me.cboItem.Column(1) & "?", _
                  vbYesNo, "Are you done") = vbYes Then
            Cancel = False
            Me.Parent.cboFilter.SetFocus   ' ***********
           
            ' Me.Visible = False
Avatar of ClaudeWalker
ClaudeWalker

ASKER

I changed it to this:

Forms("frmCommonItemEditor").SetFocus
DoCmd.GoToControl "cboFilter"  'I get a gotocontrol not available
Forms("frmCommonItemEditor").Controls("cboFilter").SetFocus 'I get can't move focus to cboFilter control
Forms("frmCommonItemEditor")("sbfrmCommonPriceChange").Visible = False
This will do it (remember to use the ! for members of collections, such as form controls) (the code also makes the control red, just to make sure you can tell it has the focus:
   Parent![txtPostalCode].SetFocus
   Parent![txtPostalCode].BackColor = vbRed

Open in new window

GoToControl is very old -- pre Access 97, I think.  Use the SetFocus method of a control now.
using Parent![cboFilter].SetFocus

I was still getting the "cannot move focus to control cboFilter" error
Is cboFilter both Visible AND Enabled ?

mx
Yes it is enabled, visible and not locked.
Can you:

1) Compact & Repair (*** to shrink the size),

2) Zip up the MDB (*** to further shrink the size)

3) Attach the file for upload here (using the 'Attach File function below) ... removing any sensitive data of course.

4**** And please give a clear explanation of exactly how to reproduce the problem or what you are trying to do.

mx
"remember to use the ! for members of collections, such as form controls"

Actually, since a Control is also a Property of a Form, you can use the Dot    
That way, you don't lose Intellisense ...

mx
There can be problems with certain control names -- on another recent question, the poster was using Me.Name intending to reference a control called Name (which in iself is a bad idea), but instead it was interpreted as the name of the current form.  Using the ! and control prefixes ensures that there are no problems.
Well ... of course I would never use a Reserved Word ... so, I wouldn't have that issue.  Not really willing to give up Intellisense for the !
I've never ever had an issue with Dots  .... and I've only found a couple of places where you must use Bang.

mx
It takes one keystroke to do either, so I always use the bang for members of collections (in Access form and standard modules).  Of course, on VB forms and VBA UserForms, you have to use the dot for everything.  Microsoft is inconsistent in this respect.
Sorry about the delay.  I left a bit early on Friday and I didn't take my work home with me.  

I really appreciate you taking a look at this.

I have these instructions come up when the form opens.  Its relatively straightforward it just looks complicated because the instructions are detailed.

Thanks,
JOe K.

MsgBox "1) In the combobox labeled filter select Ace Hardware Willy St " & vbCrLf & _
           "2) then Click Change Bid Price next to the line item Trash Bags " & vbCrLf & _
           "3) You'll be prompted are you sure, click yes " & vbCrLf & _
           "4) Go to a new record and enter new values " & vbCrLf & _
           "5) if you know how to goto a new record on the subform that would be a bonus " & vbCrLf & _
           "6) Fill in the date and two price fields " & vbCrLf & _
           "7) Click yes to are you sure " & vbCrLf & _
           "8) This is where the error happens", vbInformation, "Thanks for helping!"
           

Open in new window

EE-CommonItems.zip
Exactly how do we reproduce the issue - please be specific.

thx.mx
OK ... I see the problem.
You can't really set focus to another control during the BU.  Move these two lines of code

            Forms!frmCommonItemEditor![cboFilter].SetFocus
       
            Forms("frmCommonItemEditor")("sbfrmCommonPriceChange").Visible = False

To the After Update event - and remove them from the BU event.

You will be good to go (works for me).

mx
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To go to a new record ... add the two lines of code shown below:

Private Sub btnChangeBid_Click()
    If MsgBox("Are you sure you could like to change the bid price of " & Me.cboItem.Column(1) & "?", vbYesNo, "Change Bid Price") = vbYes Then
        'If DCount("CommonItemsBidHistoryID", "tblCommonItemsPriceHistory", "fkCommonItems = " & Me.txtCommonID) = 0 Then
        'CurrentDb.Execute "INSERT INTO tblCommonItemsPriceHistory (fkCommonItems) " & _
                          "SELECT " & Me.txtCommonID
   
        'End If
        Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Visible = True
        Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Form.filter = "fkCommonItems =" & Me.txtCommonID
        'Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Form.cboItem = Me.txtCommonID
       
        Forms!frmCommonItemEditor!sbfrmCommonPriceChange.SetFocus
        'Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Form.txtDateOfChange.SetFocus
        Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Form.cboItem.DefaultValue = Me.txtCommonID
        Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Requery
       
        Forms!frmCommonItemEditor!sbfrmCommonPriceChange.Form.txtDateOfChange.SetFocus  ' ****** ADD THIS
        DoCmd.RunCommand acCmdRecordsGoToNew  '****** ADD THIS
    End If
End Sub
Genius!

You don't know, by chance how to have it goto the last record when it enters the subform do you?
You already answered it!  thanks a million!

And lets clean up that code just a bit :-)

Private Sub btnChangeBid_Click()
   
    If MsgBox("Are you sure you could like to change the bid price of " & Me.cboItem.Column(1) & "?", vbYesNo, "Change Bid Price") = vbYes Then
        With Me.Parent.sbfrmCommonPriceChange
          .Visible = True
          .Form.filter = "fkCommonItems =" & Me.txtCommonID
          .SetFocus
          .Form.cboItem.DefaultValue = Me.txtCommonID
          .Requery
          .Form.txtDateOfChange.SetFocus  ' ****** ADDS THIS
          DoCmd.RunCommand acCmdRecordsGoToNew  '****** ADDS THIS
        End With
    End If

End Sub