Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2004, 09:36 PM
Authorized User
 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default DoCmd.GoToRecord Error in Access VBA

Actually, I had not pasted the complete code. I have a main form and subform in Access. In the main form I have some fields from the master table and in the subform I have the fields from detail table. Now when I select the Diet (which is in the main form)from the combo box in the master form and click on the OK button, the subform should fill with all the records corresponding to that diet in the subform.
But when I run the application, only the last record is being displayed in the subform. That is if there are fifteen records correspnding to Diet A then all the fifteen records are not displayed. Instead, only the last record is displayed in 15 rows.
When I run the program step by step by using F8 key, then I get the error
Run-time error 2499: You cant use the GotoRecord action or method on an object in Design view.
in the stmts DoCmd.GoToRecord , , acFirst, DoCmd.GoToRecord , , acNewRec

If I comment the DoCmd stmts I dont get the error but the last record is displayed in all rows of subform.
I am pasting the complete code I have used in the OK click button.
Dim dbs As dao.Database, rst As dao.Recordset, strSQL As String
    Dim fld As Field
    Dim Crittext As String
        Dim Crittext1 As String

    Set dbs = CurrentDb

strSQL = "SELECT DISTINCTROW DietMain.CodeNo,DietMain.Protein,DietMain.Hospital No,DietMain.Suger,DietMain.Advice,DietMain.Diet,PN o,DietSubform.item,DietSubform.SN FROM DietSubform INNER JOIN DietMain ON DietSubform.CodeNo = DietMain.CodeNo"
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
Dim ccc ', Crittext
ccc = "Low Cholesterol Diet" '[Forms]![Dietmain]![DietTitle] ' = "me"
Crittext = "Diet=" & Chr(34) & ccc & Chr(34)
rst.FindFirst Crittext
Me![ge] = rst![Advice]
Me![HospitalNo] = rst![HospitalNo]
Me![CodeNo] = rst![CodeNo]
          Do
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToRecord , , acFirst
[Forms]![DietMain]![DietSubform].[Form]![Item] = rst![Item]
[Forms]![DietMain]![DietSubform].[Form]![SN] = rst![SN]
rst.MoveNext
DoCmd.GoToRecord , , acNext
Loop Until rst.EOF
rst.Close 'close recordset.

End Sub
Please tell the code correction and the new code to be given as I am new to VBA.

 
Old October 29th, 2004, 09:57 PM
Authorized User
 
Join Date: Oct 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code you posted makes no sense. It looks like you're trying to manually "paste" the data from your recordset into the subform by reading through a recordset. What is the record source of the subform? If you set the Link Master and Link Child properties of the subform control properly, Access should display the related records automatically as soon as you set or navigate to the related record in the outer form. No code required at all!


John Viescas, author
"Microsoft Office Access 2003 Inside Out"
"Running Microsoft Access 2000"
"SQL Queries for Mere Mortals"
 
Old October 30th, 2004, 06:51 AM
Authorized User
 
Join Date: Oct 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

set the Link Master and Link Child properties of the subform it works. That is what I had done first. But I want it to fill the fields only when the button is clicked. What do I do?

 
Old October 30th, 2004, 07:27 AM
Authorized User
 
Join Date: Oct 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why not let Access display the related records as soon as the user picks the Diet (or whatever is the related field) on the outer form? I suppose you could leave the Link Child and Link Master properties blank and set them when the user clicks the button.

But I sense something else is going on here. What is the business problem you're trying to solve? Describe the problem from the perspective of your user, not your code or design.


John Viescas, author
"Microsoft Office Access 2003 Inside Out"
"Running Microsoft Access 2000"
"SQL Queries for Mere Mortals"
 
Old February 16th, 2006, 01:58 AM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had a very similar problem with DoCmd.GoToRecord and Subforms. I found the solution after several hours of playing with the code. To get DoCmd.GoToRecord to work on a Subform, set the focus on the Subform object as a whole (you don't want to set focus on a control in the Subform) with this line of code:
Forms!FormName!SubformName.SetFocus
Then use the DoCmd.GoToRecord command directly after.
Worked for me!

Quote:
quote:Originally posted by myth12345
 Actually, I had not pasted the complete code. I have a main form and subform in Access. In the main form I have some fields from the master table and in the subform I have the fields from detail table. Now when I select the Diet (which is in the main form)from the combo box in the master form and click on the OK button, the subform should fill with all the records corresponding to that diet in the subform.
But when I run the application, only the last record is being displayed in the subform. That is if there are fifteen records correspnding to Diet A then all the fifteen records are not displayed. Instead, only the last record is displayed in 15 rows.
When I run the program step by step by using F8 key, then I get the error
Run-time error 2499: You cant use the GotoRecord action or method on an object in Design view.
in the stmts DoCmd.GoToRecord , , acFirst, DoCmd.GoToRecord , , acNewRec

If I comment the DoCmd stmts I dont get the error but the last record is displayed in all rows of subform.
I am pasting the complete code I have used in the OK click button.
Dim dbs As dao.Database, rst As dao.Recordset, strSQL As String
    Dim fld As Field
    Dim Crittext As String
        Dim Crittext1 As String

    Set dbs = CurrentDb

strSQL = "SELECT DISTINCTROW DietMain.CodeNo,DietMain.Protein,DietMain.Hospital No,DietMain.Suger,DietMain.Advice,DietMain.Diet,PN o,DietSubform.item,DietSubform.SN FROM DietSubform INNER JOIN DietMain ON DietSubform.CodeNo = DietMain.CodeNo"
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
Dim ccc ', Crittext
ccc = "Low Cholesterol Diet" '[Forms]![Dietmain]![DietTitle] ' = "me"
Crittext = "Diet=" & Chr(34) & ccc & Chr(34)
rst.FindFirst Crittext
Me![ge] = rst![Advice]
Me![HospitalNo] = rst![HospitalNo]
Me![CodeNo] = rst![CodeNo]
         Do
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToRecord , , acFirst
[Forms]![DietMain]![DietSubform].[Form]![Item] = rst![Item]
[Forms]![DietMain]![DietSubform].[Form]![SN] = rst![SN]
rst.MoveNext
DoCmd.GoToRecord , , acNext
Loop Until rst.EOF
rst.Close 'close recordset.

End Sub
Please tell the code correction and the new code to be given as I am new to VBA.






Similar Threads
Thread Thread Starter Forum Replies Last Post
DoCmd.GoToRecord , , acNewRec doesnt create new re david_bridges Access VBA 1 August 21st, 2006 06:03 AM
GoToRecord Error using VBA tied to Command Button pkaptein1 Access 5 May 28th, 2005 09:49 AM
DoCmd.GoToRecord Error in Access VBA myth12345 Access VBA 1 October 30th, 2004 07:29 AM
DoCmd.GoToRecord Error in Access VBA myth12345 Access VBA 1 October 28th, 2004 10:27 AM
DoCmd.GoToRecord Error myth12345 VB How-To 0 October 28th, 2004 07:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.