Button to insert boilerplate text into field in a parent form

T

Toucan

Dear all.

I have a form with memo fields and it contains a subform with buttons to
insert boilerplate text into the fields in the parent form.

If the button were in the same form as the memo field, this code would work:
Private Sub Command1_Click()
Dim ctl As Control
Set ctl = Me.Field1
DoCmd.GoToControl ctl.Name
Field1.Text = Field1.Text & " " & "test text"
End Sub

But I can't get it to work from the subform. This is what I have so far:
Private Sub Command0_Click()
Dim ctl As Control
Set ctl = Forms!Form1
DoCmd.GoToControl ctl.Name
Me.Field1.Text = Field1.Text & " " & "test text"
End Sub

Any thoughts?
Toucan
 
A

Arvin Meyer [MVP]

Try this:

Private Sub Command0_Click()
Dim ctl As Control
Set ctl = Me.Parent.Field1
DoCmd.GoToControl ctl.Name
Me.Field1.Text = Field1.Text & " " & "test text"
End Sub

And you may want to dim a variable as a string, and set it equal to a text
control on the subform before moving to Field1 on the main form.
 
T

Toucan

It works if the last line reads:
Parent.Field1.Text = Field1.Text & " " & "test text"

Thank you so much for your help!!!!

Niels
 
T

Toucan

One more question:

Is there a way of going to the last active field in the parent form and not
to a specific field?

Thanks
Niels
 
T

Toucan

Cool.

This is what I have so far:

Private Sub CommandWNL_Click()
Screen.PreviousControl = Screen.PreviousControl & "WNL" & " "
Screen.PreviousControl.SetFocus
End Sub

In a subform with multiple other similar items, for different boilerplate
texts.

The problem I'm having is, when I click a button for the first time, or a
different button, it gives me an error message.
Object doesn't support this property or method (Error 438)

What should I do?

Thanks!!!
Niels
 
T

Toucan

I forgot to do what you told me before:

Dim WNL As String
strWNL = "WNL"

That does it!

Thanks!!!
 
T

Toucan

Still having this problem, even after making the text a string....
when I click a button for the first time, or a
different button, it gives me an error message.
Object doesn't support this property or method (Error 438)

Any thoughts?

Niels
 
A

Arvin Meyer [MVP]

I have no idea what you are trying to do here. You can either use:

Screen.PreviousControl

or:

Me.ControlName.SetFocus

You cannot use Screen.PreviousControl.SetFocus. Do not use
Screen.PreviousControl, if the control is on the same form, only if on a
different form.

For a subform, if the event is on the subform use:

Me.Parent.ControlName.SetFocus

Replace ControlName with the name of your control.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
 
T

Toucan

Thanks!!!

Arvin Meyer said:
I have no idea what you are trying to do here. You can either use:

Screen.PreviousControl

or:

Me.ControlName.SetFocus

You cannot use Screen.PreviousControl.SetFocus. Do not use
Screen.PreviousControl, if the control is on the same form, only if on a
different form.

For a subform, if the event is on the subform use:

Me.Parent.ControlName.SetFocus

Replace ControlName with the name of your control.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
 

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