Command buttons, subforms and data entry

G

Guest

I am using an extremely large database and to save space would like to open
my subforms with command buttons. I have all my tables linked properly. Using
the wizard, I insert a subform into my larger form. I am linking on a field
named "ID", which I use consistently throughout all my tables. If I leave the
subform showing the "ID" field is automatically filled in. However, when I
use a command button to open the subform, the "ID" field is blank. I think I
am linking correctly when creating the command button. However, "ID" must be
manually entered into the subform when using a command button to open, as
opposed to being automatically filled in when subform is physically in the
main form. Any suggestions?
 
R

Rick Brandt

ejferraro said:
I am using an extremely large database and to save space would like
to open my subforms with command buttons. I have all my tables linked
properly. Using the wizard, I insert a subform into my larger form. I
am linking on a field named "ID", which I use consistently throughout
all my tables. If I leave the subform showing the "ID" field is
automatically filled in. However, when I use a command button to open
the subform, the "ID" field is blank. I think I am linking correctly
when creating the command button. However, "ID" must be manually
entered into the subform when using a command button to open, as
opposed to being automatically filled in when subform is physically
in the main form. Any suggestions?

The automatic insertion of the ID is caused by the form being embedded, not
by your relationships. In fact if you open it as a separate form it is no
longer a subform at all. It is just a plain old form (that you might have
applied a filter to).

You can either go back to embedded (real) subforms and use a TabControl to
control real estate, or you can use code or other techniques to make your
separate form act more like a subform.

For example, you could set the default value property of the second form
to...

=Forms!NameOfFirstForm!ID

....or you could pass the ID in the OpenArgs argument of OpenForm and then
use code in the second form either to set the DefaultValue property or to to
set the ID in the BeforeInsert event.
 
G

Guest

Hi

You could use this OnClick of your button

Private Sub ButtonName_AfterUpdate()
DoCmd.OpenForm "SubFormName", acNormal, "",
"[ID]=[Forms]![MainForm]![IDControlOnMainForm]", , acNormal
End Sub

But there is a simpler option. Use the OnLoad event - subform visible =
False. Use the button to simply make them visible = True. Then if the
Subform = Visible.true then visible.False. This will mean that if the
subform is visible (after clicking your form button) next time you click the
button the subform will disapear. Like this

Private Sub ButtonName_Click()
If Me.SubFormName.Visible = True Then
Me.SomeOtherField.SetFocus
Me.SubFormName.Visible = False
Else
Me.SubFormName.Visible = Fasle
End If
End Sub



Note - Me.SomeOtherField.SetFocus
You should set the focus to another field that is not hidden.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top