Help! record buttons do not work on child frm when frm accessed through parent frm (1 Viewer)

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Hi! I am incrediably new to forms and Access 2003 (thank you for the advice on formatting!).

I've run into a problem with the command buttons on one of my forms. I have a main form that has a command button that opens up a second form. On the second form my Record Navigation 'go to previous record' and Record Navigation 'go to next record' buttons do not work, I receive the error: You can't go to the specified record.

However, when I open this second form up by itself these buttons function as they should.

@AccessMSSQL mentioned that when I open the second form through the command button, this may be because I have something set to only display one record so I can't move to the next or previous.

If this is the case, would someone be able to help me find a way to get the Record Navigation buttons to work as they should if I open the second form through the command button on the main form?

The code I have for the previous Record Navigation button on the second form is:

Code:
Private Sub Previous_Disease_Condition_Button_Click() 
On Error GoTo Err_Previous_Disease_Condition_Button_Click 
DoCmd.GoToRecord , , acPrevious 
Exit_Previous_Disease_Condition_Button_C: 
Exit Sub 
Err_Previous_Disease_Condition_Button_Click: 
MsgBox Err.Description 
Resume Exit_Previous_Disease_Condition_Button_C 
End Sub
Any help would be greatly appreciated- thank you so much!
 
Last edited:

spikepl

Eledittingent Beliped
Local time
Today, 08:03
Joined
Nov 3, 2010
Messages
6,142
Edit your post. Tidy up - it is rather unreadable black mess right now. Insert some line breaks.

Choose Advanced

For code, select the code and click "#" - it wraps it in code tags, so it is more readable. Edit it until it looks more normal l :D
 

spikepl

Eledittingent Beliped
Local time
Today, 08:03
Joined
Nov 3, 2010
Messages
6,142
@AccessMSSQL mentioned that when I open the second form through the command button, this may be because I have something set to only display one record so I can't move to the next or previous.

So how is that form opened? Show the code that does that.
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Sure- I have a command button on the parent form that opens up the child form.

The main form is called Nutrition Diseases/Conditions
The command button is called Pathophysiology
The second form is called Secondary Nutrition Information (Pathophysiology)

Code:
Private Sub Pathophysiology_command_Click()
On Error GoTo Err_Pathophysiology_command_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Secondary Nutrition Information (Pathophysiology)"
    
    stLinkCriteria = "[ID]=" & Me![ID]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Pathophysiology_command_Click:
    Exit Sub

Err_Pathophysiology_command_Click:
    MsgBox Err.Description
    Resume Exit_Pathophysiology_command_Click
    
End Sub

Thanks!
 

spikepl

Eledittingent Beliped
Local time
Today, 08:03
Joined
Nov 3, 2010
Messages
6,142
Well then, @AccessMSSQL was right. What do you thing this does:
stLinkCriteria = "[ID]=" & Me![ID]

when used in

DoCmd.OpenForm stDocName, , , stLinkCriteria
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
I was attempting to link up the ID field from my parent frm to my child frm so that when my parent frm was on ID#3 and I clicked the command button Pathophysiology my child form would automatically be on ID#3 as well.

This worked for me and I am happy with this, but I would also like to be able to search back and forth between records/ID's while I am on the child form without having to close down the child form and searching for the information on the parent form (with a new ID number) and reclicking the Pathophysiology command button to open the child form with the new ID/information.

:confused: I don't know if this is possible as I want to definietly keep the ID on the parent form to the ID on the child form linked.
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
There is an option that you can use that will make this work the way you want.

Code:
Private Sub Pathophysiology_command_Click()

On Error GoTo Err_Pathophysiology_command_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Secondary Nutrition Information (Pathophysiology)"
    
    'Add this line. It uses Access' built in TempVars
    TempVars.Add "ID", Me![ID]

    'Remove this line. Open the form without filtering it
    'stLinkCriteria = "[ID]=" & Me![ID]
    
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Pathophysiology_command_Click:
    Exit Sub

Err_Pathophysiology_command_Click:
    MsgBox Err.Description
    Resume Exit_Pathophysiology_command_Click
    
End Sub

On your subform/childform add the following code to the "Open" event.

Code:
'Create the variable
Dim str_ID as String
 
'Set focus to whatever field on the child form that contains the id
Me.ID.SetFocus
 
'Set your variable to the TempVar
str_ID = TempVars("ID")
 
'Search for the record
DoCmd.FindRecord str_ID, acAnywhere, False, acSearchAll

This will open up the form and find the record with that ID, but it doesn't filter it so you can use the record navigators.
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Thank you for the help- I tried using the code but I received some errors. Prehaps I did it wrong?

On the parent form when I click the command button to access the child form I get the error: object required using this code

Code:
Private Sub Pathophysiology_command_Click()
On Error GoTo Err_Pathophysiology_command_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Secondary Nutrition Information (Pathophysiology)"
    
    TempVars.Add "ID", Me![ID]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Pathophysiology_command_Click:
    Exit Sub

Err_Pathophysiology_command_Click:
    MsgBox Err.Description
    Resume Exit_Pathophysiology_command_Click
    
End Sub

Although I cannot get to the child form from the parent form, if I open the child form on its own, on the child form I get the error: compile error: sub or function not defined when I try to use the record buttons.

Code:
Private Sub Previous_Disease_Condition_Button_Click()
On Error GoTo Err_Previous_Disease_Condition_Button_Click

Dim str_ID As String

Me.ID.SetFocus

str_ID = TempVars("ID")

DoCmd.FindRecord str_ID, acAnywhere, False, acSearchAll

    DoCmd.GoToRecord , , acPrevious
    
Exit_Previous_Disease_Condition_Button_C:
    Exit Sub

Err_Previous_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Previous_Disease_Condition_Button_C
    
End Sub

Any ideas?
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
Try changing the line:

DoCmd.OpenForm stDocName, , , stLinkCriteria

to:

DoCmd.OpenForm stDocName

Also, change:

TempVars.Add "ID", Me![ID]

to

TempVars.Add "ID", Me.ID.Value.

Let me know if that fixes it.

On the child, copy the following code in (*This code needs to be on the open event*)
Code:
Dim str_ID As String
 
Me.ID.SetFocus
 
If Nz(TempVars("ID").Value, "") <> "" Then
  str_ID = TempVars("ID")
  DoCmd.FindRecord str_ID, acAnywhere, False, acSearchAll
End If
 
End Sub
 
Last edited:

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Thank you for the suggestion, I have tried it and on the parent form I still get the error: object required:


Code:
Private Sub Pathophysiology_command_Click()
On Error GoTo Err_Pathophysiology_command_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Secondary Nutrition Information (Pathophysiology)"
    
    TempVars.Add "ID", Me![ID]
    DoCmd.OpenForm stDocName

Exit_Pathophysiology_command_Click:
    Exit Sub

Err_Pathophysiology_command_Click:
    MsgBox Err.Description
    Resume Exit_Pathophysiology_command_Click
    
End Sub

I am also still receiving the [FONT=&quot]compile error: sub or function not define (the bold/underlined TempVars is highlighted when I recieved this error in VBA), on the child form when I open it by itself since the object error is preventing me from opening it from the main form

[/FONT]
Code:
Private Sub Previous_Disease_Condition_Button_Click()
On Error GoTo Err_Previous_Disease_Condition_Button_Click

Dim str_ID As String

Me.ID.SetFocus

If Nz([U][B]TempVars[/B][/U]("ID").Value, "") <> "" Then
  str_ID = TempVars("ID")
  DoCmd.FindRecord str_ID, acAnywhere, False, acSearchAll
End If
 
End Sub

    DoCmd.GoToRecord , , acPrevious
    
Exit_Previous_Disease_Condition_Button_C:
    Exit Sub

Err_Previous_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Previous_Disease_Condition_Button_C
    
End Sub
[FONT=&quot]
[/FONT]
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
That is my fault. I failed to see that you were using Access 2003. TempVars were not added until 2007.

Instead of setting the TempVars, you can just refer the the field value like:

Forms![Main Form Name]!ID.Value

In your searchrecord statement on the child form
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Hey :) I've added that code in:

Code:
Private Sub Previous_Disease_Condition_Button_Click()
On Error GoTo Err_Previous_Disease_Condition_Button_Click

Forms![Nutrition Diseases/Conditions]!ID.Value

    DoCmd.GoToRecord , , acPrevious
    
Exit_Previous_Disease_Condition_Button_C:
    Exit Sub

Err_Previous_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Previous_Disease_Condition_Button_C
    
End Sub

and I am getting the error: object doesn't support this property/method.

Thanks so much for your help with this!
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
It seems as if the code is being put on a button instead of the open event. On you child form the code should look like the below:

Code:
'This is the "On Open" Event Code. This runs when the form is opened 
Private Sub Form_Open(Cancel As Integer)

  'Create the string that will hold your variable
  Dim str_ID As String
 
  'Check to see if the main form is open
  If CurrentProject.AllForms("Nutrition Diseases/Conditions").IsLoaded Then

    'If it is, set the ID field to the ID on the main form
    str_ID = Forms![Nutrition Diseases/Conditions]!ID.Value
 
    'Set the focus to the ID of the Child Form
    Me.ID.SetFocus
 
    'Search for the ID in the record
    DoCmd.FindRecord str_ID, acAnywhere, False, acSearchAll

  End If
 
End Sub

The parent form should look like this:

Code:
'This is the code that executes when you click the button
Private Sub Pathophysiology_command_Click()

'If an error occurs, go to the appropriate error handler
On Error GoTo Err_Pathophysiology_command_Click

    'Create variable to hold document name
    Dim str_DocName As String

    'Set the variable equal to the child form name
    str_DocName = "Secondary Nutrition Information (Pathophysiology)"
    
    'Open the child form
    DoCmd.OpenForm stDocName

Exit_Pathophysiology_command_Click:
    'Exit the sub routine
    Exit Sub

Err_Pathophysiology_command_Click:
    'Display the error message
    MsgBox Err.Description
 
    'Resume at the proper point
    Resume Exit_Pathophysiology_command_Click
    
End Sub

Let me know how that works.
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
I was, I was completely applying the code to the record button on the child form. Thank you for taking the time to catch that!

When I run the parent form I recieve the error: the action or method requires a Form Name arguement. This is how I put the code:

Code:
'This is the code that executes when you click the button
Private Sub Pathophysiology_command_Click()

'If an error occurs, go to the appropriate error handler
On Error GoTo Err_Pathophysiology_command_Click

    'Create variable to hold document name
    Dim str_DocName As String

    'Set the variable equal to the child form name
    str_DocName = "Secondary Nutrition Information (Pathophysiology)"
    
    'Open the child form
    DoCmd.OpenForm stDocName

Exit_Pathophysiology_command_Click:
    'Exit the sub routine
    Exit Sub

Err_Pathophysiology_command_Click:
    'Display the error message
    MsgBox Err.Description
 
    'Resume at the proper point
    Resume Exit_Pathophysiology_command_Click
    
End Sub

(I hope I'm not wrong to assume that if the wording turns green that means VBA is not reading it as code...)

Although I wasn't able to open the child form through the command button, opening the form up on its own it worked great with the teeny correction of applying the code to the right section.
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
You are correct to assume that access does not read the green lines. These are comments. Try this in the Parent form and let me know how it works:

Code:
'This is the code that executes when you click the button
Private Sub Pathophysiology_command_Click()

'If an error occurs, go to the appropriate error handler
On Error GoTo Err_Pathophysiology_command_Click

    'Create variable to hold document name
    Dim str_DocName As String

    'Set the variable equal to the child form name
    str_DocName = "Secondary Nutrition Information (Pathophysiology)"
    
    'Open the child form
    DoCmd.OpenForm str_DocName

Exit_Pathophysiology_command_Click:
    'Exit the sub routine
    Exit Sub

Err_Pathophysiology_command_Click:
    'Display the error message
    MsgBox Err.Description

    'Resume at the proper point
    Resume Exit_Pathophysiology_command_Click
    
End Sub
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Thank you! Honestly- I am so grateful right now. You spent the entire day on this and found the solution for me. THANK YOU.

The only thing left for me to do is find how to wrap the record buttons so that they don't produce the error message you can't go to the specified record, but instead just go around in a circle. After that, I will be DONE.
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
Not a problem.

Are you using buttons you created or the native buttons on the bottom of the form?
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Aww, your like gold- thanks for helping me with the wrapping issue too! :D

I'd like to use the record buttons I have created- the previous/next ones on the parent form

Code:
Private Sub Previous_Disease_Condition_Button_Click()
On Error GoTo Err_Previous_Disease_Condition_Button_Click


    DoCmd.GoToRecord , , acPrevious

Exit_Previous_Disease_Condition_Button_C:
    Exit Sub

Err_Previous_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Previous_Disease_Condition_Button_C
    
End Sub

Code:
Private Sub Next_Disease_Condition_Button_Click()
On Error GoTo Err_Next_Disease_Condition_Button_Click


    DoCmd.GoToRecord , , acNext

Exit_Next_Disease_Condition_Button_Click:
    Exit Sub

Err_Next_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Next_Disease_Condition_Button_Click
    
End Sub

As well as the previous/next ones on the child form.

Code:
Private Sub Previous_Disease_Condition_Button_Click()
On Error GoTo Err_Previous_Disease_Condition_Button_Click

    DoCmd.GoToRecord , , acPrevious
    
Exit_Previous_Disease_Condition_Button_C:
    Exit Sub

Err_Previous_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Previous_Disease_Condition_Button_C
    
End Sub

Code:
Private Sub Next_Disease_Condition_Button_Click()
On Error GoTo Err_Next_Disease_Condition_Button_Click


    DoCmd.GoToRecord , , acNext

Exit_Next_Disease_Condition_Button_Click:
    Exit Sub

Err_Next_Disease_Condition_Button_Click:
    MsgBox Err.Description
    Resume Exit_Next_Disease_Condition_Button_Click
    
End Sub
 

NDuarte

Registered User.
Local time
Yesterday, 23:03
Joined
Jun 5, 2012
Messages
12
Parent Form:

Code:
'Executes when previous button is pressed
Private Sub Previous_Disease_Condition_Button_Click()
 
'When an error is encountered, go to the proper handler
On Error GoTo Err_Previous_Disease_Condition_Button_Click
 
  'Go to previous record
  DoCmd.GoToRecord , , acPrevious
 
Exit_Previous_Disease_Condition_Button_C:
  'Exit the sub routine
  Exit Sub
 
Err_Previous_Disease_Condition_Button_Click:
  'Check to see if the error number is 2105 (can't go to record)
  If Err.Number = 2105 Then
    'Go to last record
    DoCmd.GoToRecord , , acLast
  Else
    'Display the error
    MsgBox Err.Description
  End If
 
  Resume Exit_Previous_Disease_Condition_Button_C
 
End Sub

Code:
'Executes when the Next button is pressed
Private Sub Next_Disease_Condition_Button_Click()
 
'When an error is encountered, go to the proper handler
On Error GoTo Err_Next_Disease_Condition_Button_Click
 
  'Go to next record
  DoCmd.GoToRecord , , acNext
 
Exit_Next_Disease_Condition_Button_Click:
  'Exit the sub routine
  Exit Sub
 
Err_Next_Disease_Condition_Button_Click:
  'Check to see if the error number is 2105 (can't go to record)
  If Err.Number = 2105 Then
    'Go to first record
    DoCmd.GoToRecord , , acFirst
  Else
    'Display the error
    MsgBox Err.Description
  End If
 
  Resume Exit_Next_Disease_Condition_Button_Click
 
End Sub

Child form:

Code:
'Executes when the previous button is pressed
Private Sub Previous_Disease_Condition_Button_Click()
On Error GoTo Err_Previous_Disease_Condition_Button_Click
 
  'Go to previous record
  DoCmd.GoToRecord , , acPrevious
 
Exit_Previous_Disease_Condition_Button_C:
  'Exit the sub routine
  Exit Sub
 
Err_Previous_Disease_Condition_Button_Click:
  'Check to see if the error number is 2105 (can't go to record)
  If Err.Number = 2105 Then
    'Go to last record
    DoCmd.GoToRecord , , acLast
  Else
    'Display the error
    MsgBox Err.Description
  End If
 
  Resume Exit_Previous_Disease_Condition_Button_C
 
End Sub

Code:
'Executes when the Next button is pressed
Private Sub Next_Disease_Condition_Button_Click()
 
'On error, go to the correct handler
On Error GoTo Err_Next_Disease_Condition_Button_Click
 
'Go to next record
DoCmd.GoToRecord , , acNext
 
Exit_Next_Disease_Condition_Button_Click:
'Exit the sub routine
Exit Sub
 
Err_Next_Disease_Condition_Button_Click:
'Check to see if the error number is 2105 (can't go to record)
If Err.Number = 2105 Then
'Go to first record
DoCmd.GoToRecord , , acFirst
Else
'Display the error
MsgBox Err.Description
End If
 
Resume Exit_Next_Disease_Condition_Button_Click
 
End Sub
Let me know if this solves the issue.

Thanks!
 

access account

Registered User.
Local time
Today, 00:03
Joined
Jun 4, 2012
Messages
14
Works like a charm :) Thank you so much! I really, really appreciate your help!!
 

Users who are viewing this thread

Top Bottom