[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1014
  • Last Modified:

goto 1st field of subform

I have a form with an add button.  When I click on the button I would like to go to the first field in the header of my subform.  I've tried the following and so far nothing works.
Please help!

This one takes me to the first field in the detail section.  I need the first field in the header
   DoCmd.GoToControl "fsubPlacementQues"

With these two I just get an error "no field named..."
DoCmd.GoToControl "Forms![fsubPlacementQues]![txtPlacementReviewDate]"
DoCmd.GoToControl "Forms![frmPlacementReview]![fsubPlacementQues]!Form![txtPlacementReviewDate]"
0
BobRosas
Asked:
BobRosas
  • 12
  • 7
  • 7
2 Solutions
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
try this

Me.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus
0
 
BobRosasAuthor Commented:
Thanks for your quick response!  Atleast I didn't get an error but it still takes me to the first record in the detail.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
ok ... what is the control name in the Header ?

mx
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
BobRosasAuthor Commented:
The name of the field that I want to move to is txtPlacementReviewDate
Is that what you are asking me?
But I think I found something else.  I posted some of my code below.  I write to 2 files and then in order to display what I wrote I close the form and re-open it.  Your code is probably ok.  I think it's my timing, and I'm just not sure how to fix it.  The code below takes me to the first field in the detail.  But when I tried moving your line of code after I reopen the form I get the message...The expression  you entered referes to an object that is closed or no longer exists.
Any thoughts!
Thanks so much!

Private Sub cmdAdd_Click()
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("dbo_tblPlacementReview", DB_OPEN_DYNASET, dbSeeChanges)
    Set rs1 = db.OpenRecordset("dbo_tblPlacementQues", DB_OPEN_DYNASET, dbSeeChanges)
        'code to add data to tblPlacementReview table
        'code to add to tblPlacementQues
    Me.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus
    rs.Close
    rs1.Close
    DoCmd.Close
    DoCmd.OpenForm "frmPlacementReview"
0
 
GRayLCommented:
Try:

Me!fsubPlacementQues.Form!txtPlacementReviewDate.SetFocus

if indeed txtPlacementReviewDate is the name of the control in the subform header.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
gRay ... that was my first post :-)

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
bob - I missed something

try this - two lines are needed:

Me.fsubPlacementQues.SetFocus                ' focus to subform control first
Me.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus   ' then the actual control

mx
0
 
BobRosasAuthor Commented:
I think we're getting closer.  At least now when I click on the add button I can see the cursor appears to go to the field I want it to and then it goes right back to the first field in the detail.  It's really just a quick flash of the field but I think that's what it's doing.  Sorry to be such a problem.
0
 
GRayLCommented:
Not quite.  Check the implicit references.  Not that you need it, but this from A2003 Help (notice the upgrade!:))

Each Form object has a Controls collection, which contains all controls on the form. You can refer to a control on a form either by implicitly or explicitly referring to the Controls collection. Your code will be faster if you refer to the Controls collection implicitly. The following examples show two of the ways you might refer to a control named NewData on the form called OrderForm:

' Implicit reference.
Forms!OrderForm!NewData
            
' Explicit reference.
Forms!OrderForm.Controls!NewData
            
The next two examples show how you might refer to a control named NewData on a subform ctlSubForm contained in the form called OrderForm:

Forms!OrderForm.ctlSubForm.Form!Controls.NewData
            
Forms!OrderForm.ctlSubForm!NewData

0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
My post:
Me.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus

Your Post:
Me!fsubPlacementQues.Form!txtPlacementReviewDate.SetFocus

What am I missing ?

Kudos on the Upgrade !!
0
 
BobRosasAuthor Commented:
I tried both and I still can't get it to work.  Is there a work around or anything?
Thanks!
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Post the exact code you have now

thx.mx
0
 
BobRosasAuthor Commented:
Here you go.  I posted all the code I have in my add button.  
I think that since I set focus and then re-open the form that it reverts back.  But if I place the code after I reopen I get an error.  
Thanks so much!

Private Sub cmdAdd_Click()
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("dbo_tblPlacementReview", DB_OPEN_DYNASET, dbSeeChanges)
    Set rs1 = db.OpenRecordset("dbo_tblPlacementQues", DB_OPEN_DYNASET, dbSeeChanges)
        'add date to tblPlacementReview table
        With rs
            .AddNew
            !PlacementID = Forms![frmPlacement]![fsubPlacement].Form![txtPlacementID]
            !PlacementReviewDate = Date
            .Update
            'to get last PlacementReviewId available because it's used below
            .MoveLast
            [txtPlacementReviewNewID] = !PlacementReviewID
        End With

        'add all questions for that date to tblPlacementQues
        With rs1
            .AddNew
            !PlacementReviewID = [txtPlacementReviewNewID]
            !PlacementQuesLkUpID = 1
            .Update
            .AddNew
            !PlacementReviewID = [txtPlacementReviewNewID]
            !PlacementQuesLkUpID = 2
            .Update
            .AddNew
            !PlacementReviewID = [txtPlacementReviewNewID]
            !PlacementQuesLkUpID = 3
            .Update
        End With
    Me.fsubPlacementQues.SetFocus                ' focus to subform control first
    'Me.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus   ' then the actual control
    Me!fsubPlacementQues.Form!txtPlacementReviewDate.SetFocus
    rs.Close
    rs1.Close
    DoCmd.Close
    DoCmd.OpenForm "frmPlacementReview"
End Sub

0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"But if I place the code after I reopen I get an error.  "

What is the error ?  And what line of code ?

mx

0
 
BobRosasAuthor Commented:
If use the following code I get this error as shown below
The expression  you entered referes to an object that is closed or no longer exists.

    rs.Close
    rs1.Close
    DoCmd.Close
    DoCmd.OpenForm "frmPlacementReview"  'opening this opens the subform so  don't get the err
    Me.fsubPlacementQues.SetFocus                ' ERROR HERE
    Me.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus
0
 
GRayLCommented:
Mx: Re My Post/YourPost

use of the exclamation mark (implicit) versus period(explicit), versus the 'use a period everywhere' version which the parser can accept in a lot of cases.  I still stand by the old saw - If I made it use ! - If Microsoft made it use .
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Ok ... what is the name of the form with the Header ?   frmPlacementReview  ??

Lets say it's called Form1 ... then


    Form1.fsubPlacementQues.SetFocus                
    Form1.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus

or

    frmPlacementReview.fsubPlacementQues.SetFocus                
    frmPlacementReview.fsubPlacementQues.Form.txtPlacementReviewDate.SetFocus
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
I use periods everywhere all the time ... for years - because Intellisense works.

This:

' Implicit reference.
Forms!OrderForm!NewData
           
' Explicit reference.
Forms!OrderForm.Controls!NewData

The difference here is the use of Controls, not the !

Could also be

Forms.OrderForm.Controls.NewData

Note the Help snippet does not refer to periods or bangs ...

This *would* fail:

Forms!OrderForm!Controls!NewData     ! after OrderForm - before Controls

mx

0
 
GRayLCommented:
I would have written it:

             
    Form1!fsubPlacementQues.Form!txtPlacementReviewDate.SetFocus

or
               
    frmPlacementReview!fsubPlacementQues.Form!txtPlacementReviewDate.SetFocus
0
 
GRayLCommented:
When ever I use periods referencing a field of a recordset I get an error:

rs!myFld = "abc"      this works
rs.myFld = "abc"      this didn't (at least in A2K)

I just noticed the last two lines of my Help excerpt is is error.  Going to test.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
rs!myFld = "abc"      this works
rs.myFld = "abc"      this didn't (at least in A2K)

Not the same thing.  This is one place where you need the !   to refer to a Field in a record set.  

rs![YourField] ... I have to use !'s here ... sorry I didn't mention that.

mx
0
 
BobRosasAuthor Commented:
I doubled points and awarded both of you.  I hope that's ok.  You both stuck by me and went above and beyond for what should have been an easy one.  Thanks so much.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
np ... glad it works !..!!.....!...!.!

mx
0
 
GRayLCommented:
Forms!OrderForm.ctlSubForm.Form!Controls.NewData
-------------------------------------------^--this should be a period            
Forms!OrderForm.ctlSubForm!NewData
---------------------^--this should be a bang

When I try to define a field of a recorset using a period as in updating a record, the period throws an error.  I have to use the bang.  
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
gRay ... see my post just above - before Open Discussion ...
0
 
GRayLCommented:
These posts are sure not arriving in my Inbox in what you would call a timely fashion.  Thanks for the dialog.  In my post at http:*a20845561 I was pointing out the error in the A2003 Help file - to think they've had 5 years to get it right!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now