Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create button in Main form to replicate data in Subform

Status
Not open for further replies.

btj

Technical User
Nov 17, 2001
94
US
I have a main form that has one field which looks up data in a subform. The data in the subform will be accessed by users that will either want to enter a new record (in the subform) or replicated the record's date (also in the subform).

How do I do this? Here was the code I am working with...I know it works within the main form, but I can't figure how to reference the subform's fields as those to be replicated.

Code:
Private Sub cmdReplicate_Click()

On Error GoTo Err_cmdReplicate_Click

Dim Build, TargetedRelease As Variant

Build = Me![Build]
TargetedRelease = Me![TargetedRelease]

DoCmd.GoToRecord , , acNewRec

Me![Build] = Build
Me![TargetedRelease] = TargetedRelease

Exit_cmdReplicate_Click:
Exit Sub

Err_cmdReplicate_Click:
MsgBox Err.Description
Resume Exit_cmdReplicate_Click

End Sub


Although there are similar issues, I cannot get my code to do what I want...hopefully you can help.

Please let me know what you think.

Thank you in advance.

- Ben
 
Hi btj,
I hope I understand your question.. if this replicate button is located in the main form, and not in the subform, the syntax for referencing the subform should be something like this:

Forms![MainFormName]![SubFormName].Form![TargetedRelease]

Hope that helps!
 
Katerine,
I believe that is what I was looking for...I just had gotten the syntax right.

Where do I reference the subform? Is it before "DoCmd" or do I need to do this for each line of code?

- Ben
 
Hi btj,
If you want to go to a new record in the subform, I would suggest doing the following:

Put the code (pretty much as you have it above) not in the main form, but in a public sub in the subform. Then reference that sub in the on_click event for the button in the main form as follows:

Call Form_SubFormName.ProcedureName

Hope that helps! Please let me know if what I said made no sense :) , or if it gives you any problems. Katie
Hi! I'm currently studying the COM+ programming model. Hopefully YOU'RE having fun, which would make one of us..
 
Katie -
Thanks for the help!

I placed the code that was in the Replicate button to a public sub within the subform and am having problems.

Code: (in case you need to reference it):

Public Sub cmdReplicate_Click()

On Error GoTo Err_cmdReplicate_Click

Dim Build, TargetedRelease As Variant

Build = Me![Build]
TargetedRelease = Me![TargetedRelease]

DoCmd.GoToRecord , , acNewRec

Me![Build] = Build
Me![TargetedRelease] = TargetedRelease

Exit_cmdReplicate_Click:
Exit Sub

Err_cmdReplicate_Click:
MsgBox Err.Description
Resume Exit_cmdReplicate_Click

End Sub


I, then, went back to the button (on Main form) on_click property and used this code:

Private Sub cmdReplicate_Click()

Call [form_name]_[subform_name].ProcedureName

End Sub


I am getting an "Invalid Character" error within the Code at the underscore (_) that is red.

So, these are my issues:
1. Do I need to change the Sub (Main form) to Public?
2. The Form and Subform names (in brackets) both use an underscore - does that cause difficulty? I thought using brackets allowed file names to have spaces and/or underscores.
3. What do I put for the "ProcedureName"
4. Can you see a mistake in either set of code other than the issues I already mentioned?

Please let me know if you can help.

Thanks,
Ben
 
Hi btj,
I'm sorry my code before was a little unclear. I'll try to do better :)

Let's say your subform is called Unit_Subform. In this case, with the code above, you would call the procedure from the main form like this:

Call Form_Unit_Subform.cmdReplicate_Click

Please let me know if you still encounter any problems. Katie
Hi! I'm currently studying the COM+ programming model. Hopefully YOU'RE having fun, which would make one of us..
 
Katie,
Thanks for the update. Your code was fine, I am just new at this.

Still having problems - hope you can help.

In my subform, I have my code running in:
Public Sub cmdReplicate_Click()

In my main form, I changed my code to what you suggested:
Public Sub cmdReplicate_Click()

Call Form_[NTRK_ST_003_Subform].cmdReplicate_Click

End Sub


I keep getting a syntax error highlighting the "Call Form_[NTRK_ST_003_Subform].cmdReplicate_Click" of code.

Please let me know what you think. Where am I making a mistake?

- Ben
 
Hi Ben,
The brackets shouldn't be necessary.

Here's a suggestion that should help in this and all your future coding endeavors (I live by it):
- Delete that line and retype "Call ".
- Press CTRL+j. A drop-down will appear showing just about every valid object you can call.
- type the first few letters of the object you want (in this case, "form_") and the drop-down will move to the appropriate place.
- Pick the correct form name (use up-down arrows to select). After it's selected, press enter. The form name, with the correct syntax, will appear in your code.
- Type "." (since this is the separator between the object name and the item in the object you want to call). Again, a drop-down will appear with all your valid picks. Select "cmdReplicate_Click" and hit enter.

If you still run into errors, it may need the end-parentheses, but I don't think it will.

Hope that helps! Katie
Hi! I'm currently studying the COM+ programming model. Hopefully YOU'RE having fun, which would make one of us..
 
Katie...
That was it and now the code is working correctly.

Thank you so much for all of your help and patience.

- Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top