Adding a new record by popping up a new form

  • Thread starter Thread starter George Papadopoulos
  • Start date Start date
G

George Papadopoulos

I have designed a form that inputs technical repair data into a table. I
also have a second form that inputs the spare parts used and stores them in
a corresponding second table. I would like to have a button on the first
form thay pops-up the second form, which would accept as input the spare
parts. I have linked the two tables (second table has a foreign key) and
would like the repair number to be appear in the second form as well as be
stored in the second table. I have used the code below :


Private Sub Add_spare_Click()
On Error GoTo Err_cmdGoHere_Click
Dim stDocName As String
Dim x As Long

Me.[Kwdikos episkeyhs].SetFocus
x = Me.[Kwdikos episkeyhs].Text

stDocName = "STOIXEIA_ANTALLAKTIKWN"
DoCmd.OpenForm stDocName

Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.[Kwdikos episkeyhs].SetFocus
Me.[Kwdikos episkeyhs].Text = x

Exit_cmdGoHere_Click:
Exit Sub

Err_cmdGoHere_Click:
MsgBox Err.Description
Resume Exit_cmdGoHere_Click


End Sub

When I press the button I get the error message : 'This property is
read-only and can`t be set'. I have traced the error back to the code line :
Me.[Kwdikos episkeyhs].Text = x

Why does this assignment fail?

thanks in advance


George Papadopoulos
 
George said:
I have designed a form that inputs technical repair data into a table. I
also have a second form that inputs the spare parts used and stores them in
a corresponding second table. I would like to have a button on the first
form thay pops-up the second form, which would accept as input the spare
parts. I have linked the two tables (second table has a foreign key) and
would like the repair number to be appear in the second form as well as be
stored in the second table. I have used the code below :


Private Sub Add_spare_Click()
On Error GoTo Err_cmdGoHere_Click
Dim stDocName As String
Dim x As Long

Me.[Kwdikos episkeyhs].SetFocus
x = Me.[Kwdikos episkeyhs].Text

stDocName = "STOIXEIA_ANTALLAKTIKWN"
DoCmd.OpenForm stDocName

Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.[Kwdikos episkeyhs].SetFocus
Me.[Kwdikos episkeyhs].Text = x

Exit_cmdGoHere_Click:
Exit Sub

Err_cmdGoHere_Click:
MsgBox Err.Description
Resume Exit_cmdGoHere_Click


End Sub

When I press the button I get the error message : 'This property is
read-only and can`t be set'. I have traced the error back to the code line :
Me.[Kwdikos episkeyhs].Text = x

Why does this assignment fail?


Unlike VB controls, the value of an Access control is in its
Value property. The Text property is very rarely used since
it only refers to the control's transient text while the
control is being edited and has no meaning until the control
has the focus.

Since the Value property is the default property, you are
not required to specify it, depending on your coding style,
you could just refer to the control. In your code above,
get rid of the .SetFocus lines and use:

x = Me.[Kwdikos episkeyhs].Value
or just:
x = Me.[Kwdikos episkeyhs]

Now that we've got that out of the way, I think the reason
that your statement is failing is that for some reason the
text box is not editable. Regardless of the reason, it
looks like you have some confusion of what the Me object
refers to. It always refers to the class module containing
the executing line of code.

It looks like you're trying to set the value of a control on
the newly opened form. This is at best a poor approach and
at worst won't work because the newly opening form may not
yet be in a state to have values assigned to its controls
(Load event). I can't be sure what your code is supposed to
do, but I think you're trying to open the form and set the
value of a control to be the same as a control in the
running form. If so, let me suggest that you use the
OpenForm method's OpenArgs argument to pass the value to the
opening form and let it set its own control at an
apptopriate time:

Private Sub Add_spare_Click()
On Error GoTo Err_cmdGoHere_Click
Dim stDocName As String

stDocName = "STOIXEIA_ANTALLAKTIKWN"
DoCmd.OpenForm stDocName, _
OpenArgs:= Me.[Kwdikos episkeyhs]
Exit_cmdGoHere_Click:
. . .

And the code in the new form's Load event could be:

Private Form_Load(
' If openargs has a value, add new record
If Not IsNull(Me.OpenArgs) Then
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.[Kwdikos episkeyhs] = Me.OpenArgs
End If
End Sub
 
Back
Top