Setting focus to a field outside the subform in order to copy its

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I write code in a subform and I need to collect some text, from a text
field in the main form, I cant refer to that field without an error saying I
need to set focus on the field before attempting to read it.

How can I setfocus a field outside the subform in order to copy its text?

TIA!!
 
i'm guessing that the syntax you're using is

Me.Parent!ControlName.Text

don't refer to the Text property of the control on the main form, since
that's what requires the control to have the focus. instead, refer to the
Value property. since that's the control's "default" property reference,
simply refer to the control, as

Me.Parent!ControlName

or you can explicitly refer to the value property, if you really want to, as

Me.Parent!ControlName.Value

either of the above two works the same.

hth
 
Right on the nail tina!! Thanks a lot. You were right I was using .text
instead of .value!
 
This is along the same vein. I want to refer to several controls on the
parent form of a subform. Instead of using the Me.Parent!ControlName syntax
for each control reference, can I construct a With ... End With section and
if so, could you supply the syntax for both the With statement and the
reference to the control on the parent form.

TIA
 
JoD said:
This is along the same vein. I want to refer to several controls on the
parent form of a subform. Instead of using the Me.Parent!ControlName
syntax
for each control reference, can I construct a With ... End With section
and
if so, could you supply the syntax for both the With statement and the
reference to the control on the parent form.


Here's an example getting values from controls on the parent form into
variables:

Dim varControlAValue As Variant
Dim varControlBValue As Variant

With Me!Parent
varControlAValue = !ControlA
varControlBValue = !ControlB
End With

In the above snippet, "ControlA" and "ControlB" would be the names of
controls on the parent form.
 
If I use the suggested "With Me!Parent ... End With" syntax, I receive the
following error:
"my database" can't find the field "Parent" referred to in your description

If I use "With Me.Parent ... End With" syntax, I receive the following error:
Application-defined or object-defined error
when I reference the control as !controlname

However, I neglected to mention that this "With...End With" section is
within a "With objectref...End With" section, where objectref is a reference
to an Outlook AppointmentItem object.

The code does work if I reference each control as Me.Parent!controlname.
 
JoD said:
If I use the suggested "With Me!Parent ... End With" syntax, I receive the
following error:
"my database" can't find the field "Parent" referred to in your
description

Oops! Sorry, that was supposed to be "Me.Parent" -- using the dot (.)
instead of the bang (!).
If I use "With Me.Parent ... End With" syntax, I receive the following
error:
Application-defined or object-defined error
when I reference the control as !controlname

That's odd. Assuming your control name is correct -- and after having fixed
my error -- it should work. Please post the exact code you're using, and
indicate which line raises the error.
 
Here is my test code:

' create Outlook objects
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objRecipient As Outlook.Recipient
Dim objFolder As Outlook.MAPIFolder
Dim objItems As Outlook.Items
Dim objAppt As Outlook.AppointmentItem
Dim myCalendar, strStart, strShortTime, strLocation As String
Dim lDuration As Long


' now add the new appointment if desired
If MsgBox("Add to Outlook calendar?", vbOKCancel) = vbOK Then
Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
myCalendar = Me!cboCalendar
Set objRecipient = objNameSpace.CreateRecipient(myCalendar)

' check calendar name
objRecipient.Resolve
If objRecipient.Resolved Then
' set values for objects
Set objFolder =
objNameSpace.GetSharedDefaultFolder(objRecipient, olFolderCalendar)
Set objItems = objFolder.Items
Set objAppt = objItems.Add
With objAppt
.Start = Me!txtExamDate & " " & Me!txtScheduledStartTime
.duration = DateDiff("n", Me!txtScheduledStartTime,
Me!txtScheduledEndTime)
'With Me.Parent
.Subject = Me.Parent!txtFirstName & " " &
Me.Parent!txtLastName & " " & Me.Parent!txtCourseName
strLocation = Me.Parent!cboExamAudioFormat & " " &
Me.Parent!cboExamVisualFormat & _
" " & Me.Parent!cboComputer & " " &
Me.Parent!txtScribe
.Location = strLocation
'End With
.ReminderSet = False
' set category based on accommodation
If InStr(strLocation, "Kurz") > 0 Then .Categories = "AES -
Kurzweil"
If InStr(strLocation, "Word Processor") > 0 Then .Categories
= "AES - Computer"
If InStr(strLocation, "DVD") > 0 Then .Categories = "AES -
Computer"
If InStr(strLocation, "PowerPoint") > 0 Then .Categories =
"AES - Computer"
If InStr(strLocation, "Dragon") > 0 Then .Categories = "AES
- AT"
If InStr(strLocation, "JAWS") > 0 Then .Categories = "AES -
AT"
If InStr(strLocation, "Reader") > 0 Then .Categories = "AES
- scribe or read"
If InStr(strLocation, "Taped") > 0 Then .Categories = "AES -
scribe or read"
If InStr(strLocation, "Essay") > 0 Then .Categories = "AES -
scribe or read"
If InStr(strLocation, "All tests") > 0 Then .Categories =
"AES - scribe or read"
.Save
.Close (olSave)
End With

'Release the AppointmentItem object variable
Set objAppt = Nothing
Set objItems = Nothing
Set objFolder = Nothing
Else
MsgBox ("No appointment created, could not resolve calendar name")
End If
'Release the object variables
Set objNameSpace = Nothing
Set objRecipient = Nothing
Set objOutlook = Nothing
End If

Exit_Form_AfterUpdate:
Exit Sub

Err_Form_AfterUpdate:
MsgBox Err.Description
Resume Exit_Form_AfterUpdate

End Sub

The error occurs at the following line
.Subject = Me.Parent!txtFirstName & " " &
Me.Parent!txtLastName & " " & Me.Parent!txtCourseName
if I activate the "With Me.Parent...End With" section and replace
"Me.Parent" with "!". It works if I run it with the "With" section commented.

Thanks for taking the time to look at this.
 
JoD said:
Here is my test code: [...]
With objAppt
.Start = Me!txtExamDate & " " & Me!txtScheduledStartTime
.duration = DateDiff("n", Me!txtScheduledStartTime,
Me!txtScheduledEndTime)
'With Me.Parent
.Subject = Me.Parent!txtFirstName & " " &
Me.Parent!txtLastName & " " & Me.Parent!txtCourseName
strLocation = Me.Parent!cboExamAudioFormat & " " &
Me.Parent!cboExamVisualFormat & _
" " & Me.Parent!cboComputer & " " &
Me.Parent!txtScribe
.Location = strLocation
'End With [...]
End With

The error occurs at the following line
.Subject = Me.Parent!txtFirstName & " " &
Me.Parent!txtLastName & " " & Me.Parent!txtCourseName
if I activate the "With Me.Parent...End With" section and replace
"Me.Parent" with "!". It works if I run it with the "With" section
commented.

Do you mean you had something like this as your code:

With Me.Parent
.Subject = !txtFirstName & " " & !txtLastName & _
" " & !txtCourseName
strLocation = !cboExamAudioFormat & _
" " & !cboExamVisualFormat & _
" " & !cboComputer & " " & !txtScribe
.Location = strLocation
End With

? Because if so, there's bound to be confusion as to what object .Subject
and .Location belong to. VBA will think they belong to Me.Parent, and they
don't. You would need to write this:

With Me.Parent
objAppt.Subject = !txtFirstName & " " & !txtLastName & _
" " & !txtCourseName
strLocation = !cboExamAudioFormat & _
" " & !cboExamVisualFormat & _
" " & !cboComputer & " " & !txtScribe
objAppt.Location = strLocation
End With
 
Thanks for looking at this - you're right. It would be cleaner to explicitly
specify the reference to the appointment item object and use the "With...End
With" section for the parent form.

I will take your suggestion.

Dirk Goldgar said:
JoD said:
Here is my test code: [...]
With objAppt
.Start = Me!txtExamDate & " " & Me!txtScheduledStartTime
.duration = DateDiff("n", Me!txtScheduledStartTime,
Me!txtScheduledEndTime)
'With Me.Parent
.Subject = Me.Parent!txtFirstName & " " &
Me.Parent!txtLastName & " " & Me.Parent!txtCourseName
strLocation = Me.Parent!cboExamAudioFormat & " " &
Me.Parent!cboExamVisualFormat & _
" " & Me.Parent!cboComputer & " " &
Me.Parent!txtScribe
.Location = strLocation
'End With [...]
End With

The error occurs at the following line
.Subject = Me.Parent!txtFirstName & " " &
Me.Parent!txtLastName & " " & Me.Parent!txtCourseName
if I activate the "With Me.Parent...End With" section and replace
"Me.Parent" with "!". It works if I run it with the "With" section
commented.

Do you mean you had something like this as your code:

With Me.Parent
.Subject = !txtFirstName & " " & !txtLastName & _
" " & !txtCourseName
strLocation = !cboExamAudioFormat & _
" " & !cboExamVisualFormat & _
" " & !cboComputer & " " & !txtScribe
.Location = strLocation
End With

? Because if so, there's bound to be confusion as to what object .Subject
and .Location belong to. VBA will think they belong to Me.Parent, and they
don't. You would need to write this:

With Me.Parent
objAppt.Subject = !txtFirstName & " " & !txtLastName & _
" " & !txtCourseName
strLocation = !cboExamAudioFormat & _
" " & !cboExamVisualFormat & _
" " & !cboComputer & " " & !txtScribe
objAppt.Location = strLocation
End With


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top