Reference Without Focus Problem

Tyler08

Registered User.
Local time
Today, 17:40
Joined
Feb 27, 2008
Messages
44
Afternoon all.

My database keeps records of shifts worked. The form which is used to input the data has drop down boxes to select Contract, Site, Name, etc. The user enters this data from the time sheets. Each shift record can be the same for each day of the week. I'm trying to code a way of copying data from an existing record and pasting into a new record on the click of a button so only the date has to be changed, to save inputting time, using this code:

Private Sub CmdCopyRecord_Click()
On Error GoTo Err_CmdCopyRecord_Click

Dim StrShiftDate As String
Dim StrSiteID As String
Dim StrStaffID As String
Dim StrStaffTypeID As String

StrShiftDate = ShiftDate.Text
StrSiteID = SiteID.Value
StrStaffID = StaffID.Value
StrStaffTypeID = StaffTypeID.Value

DoCmd.GoToRecord , , acNewRec

ShiftDate.Text = StrShiftDate
SiteID.Value = StrSiteID
StaffID.Value = StrStaffID
StaffTypeID.Value = StrStaffTypeID

Exit_CmdCopyRecord_Click:
Exit Sub

Err_CmdCopyRecord_Click:
MsgBox Err.Description
Resume Exit_CmdCopyRecord_Click

End Sub

When the button is clicked I get the error message "You can't reference a property or method for a control unless the control has the focus".

If I rem out the ShiftDate.Txt lines the code works lovely. This control is a text box, the ones that do work are combo boxes.

Can anyone please give me a pointer on how to make this work for the date field text box too.

Cheers

Thanks

Regards

Tyler
 
Use the .Value property as you have for the others.
 
And Tyler the way the .Text works versus the .Value, is that .Text is used primarily if you want the value BEFORE the control has been updated. This is typically used in the On Change event when you are looking at what characters are being typed in. The control has to have the focus for that because the value has not changed yet and in order to get the current text that is in the textbox, the .Text property has to be used.

For all others, using the .Value (and .Value is the default so you don't even have to use it - you can say Me.txtMyTextBox1 = Me.txtMyTextBox2 and that is the same as saying Me.txtMyTextBox1.Value = Me.txtMyTextBox2.Value) is the way to go.
 
Thanks for your help gentlemen.

I did the text box ShiftDate.text first then the other 3 combo boxes only offered .value so that's what I used.

I'll change the ShiftDate.text to ShiftDate.value and mark my learning curve slightly higher!

Thanks again

Regards

Tyler
 

Users who are viewing this thread

Back
Top Bottom