Help please

G

Guest

I have a "Deliverables" form. Each deliverable has a command button to open
a "Tasks" form (that shows all the tasks for the particular deliverable -
Each task record has the associated deliverable ID number ). The code for
this command is:

Private Sub Command15_Click()
On Error GoTo Err_Command15_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmTasks"

stLinkCriteria = "[Deliverable ID]=" & Me![Deliverable ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command15_Click:
Exit Sub

Err_Command15_Click:
MsgBox Err.Description
Resume Exit_Command15_Click

End Sub

The problem is when I create a new deliverable and click on the button, the
deliverable id number is 0 not the associated deliverable ID . How can I
have the number transfered over on new task records.

Thanks
 
G

Guest

You need to do two things; firstly ensure the new deliverable record is
saved, or referential integrity will be violated; secondly set the
DefaultValue property of the ID control on the Tasks form to the current
deliverable's ID, so amend the code like so:

Private Sub Command15_Click()
On Error GoTo Err_Command15_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmTasks"

stLinkCriteria = "[Deliverable ID]=" & Me![Deliverable ID]

RunCommand acCmdSaveRecord
DoCmd.OpenForm stDocName, , , stLinkCriteria
Forms(stDocName).[Deliverable ID].DefaultValue = """" & Me. [Deliverable
ID] & """"

Exit_Command15_Click:
Exit Sub

Err_Command15_Click:
MsgBox Err.Description
Resume Exit_Command15_Click

End Sub

If RunCommand acCmdSaveRecord raises a 'Command Unavailable' error for some
reason, put Me.Dirty = False instead. Both save the current record.

Note that the DefaultValue property is a string expression regardless of the
data type of the [Deliverable ID] field so is wrapped in quotes characters.
Most of the time this isn’t absolutely necessary, but can be crucial in
situations where you might not think so, and produce the wrong result if the
quotes are omitted. The two sets of 4 quotes characters above are each
interpreted as a literal quotes character when setting the property's value.

Setting the DefaultValue rather than the Value property has two advantages;
firstly it does not initiate an edit in the Task form's new record so a user
can back out simply by closing the form if necessary; secondly it will be
used as the value for however many new records you create in the tasks form
before closing it to return to the deliverables form.

BTW when adding a control to a form its better to give it a meaningful name
e.g. cmdOpenTasksForm rather than accepting a default name like Command15.
This makes the application more easily maintained. Don't change the name of
a control after code has been created in one of its event procedures,
however. The link between the code and the control will be broken and the
event procedure will need to be recreated by cutting and pasting the code
into a new procedure.

Ken Sheridan
Stafford, England

Jet said:
I have a "Deliverables" form. Each deliverable has a command button to open
a "Tasks" form (that shows all the tasks for the particular deliverable -
Each task record has the associated deliverable ID number ). The code for
this command is:

Private Sub Command15_Click()
On Error GoTo Err_Command15_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmTasks"

stLinkCriteria = "[Deliverable ID]=" & Me![Deliverable ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command15_Click:
Exit Sub

Err_Command15_Click:
MsgBox Err.Description
Resume Exit_Command15_Click

End Sub

The problem is when I create a new deliverable and click on the button, the
deliverable id number is 0 not the associated deliverable ID . How can I
have the number transfered over on new task records.

Thanks
 

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

Similar Threads


Top