Problem inserting record into table

S

Shaldaman

Hi,
I have 2 forms - 'New Instructor' and 'New Course' which are linked.
The first form has the following fields - InstructorID(autonumber
primary key), Instructor First Name, Instructor Lase Name and
Department, all of which are linked to an 'Instructor' table. The 'New
Course' form has 3 fields - InstructorID, CourseID, Course Name. If a
user on the 'New Instructor' form clicks a button 'Add New
Course'(named Command14), it should do 2 things:
1) Add the values for InstructorID, Instructor First Name, Instructor
Lase Name and Department into the 'Instructor' table, and
2) Open the 'New Course' form, displaying the corresponding
InstructorID but the other 2 fields should remain blank. When the user
enters information for CourseID & Course Name, and click a button 'Add
Course' on the form, the CourseID & Course Name are to be inserted into
another table 'Course'. The 'Instructor' table has a one-to-many
relationship with the 'Course' table.

To achieve this, I have an OnClick function associated with the 'Add
New Course' button on the 'New Instructor' form:

Private Sub Command14_Click()
On Error GoTo Err_Command14_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Course"

DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec
Exit_Command14_Click:
Exit Sub

Err_Command14_Click:
MsgBox Err.Description
Resume Exit_Command14_Click

End Sub

And for the 'Course' form, I have the following OnCurrent event:

Private Sub Form_Current()
Me!InstructorID = Forms![Instructor]!InstructorID
Me!CourseID = ""
Me!CourseName = ""
Me!CourseDesc = ""

End Sub

My problem is, when I click 'Add Course' on the 'Course' form, I get
the "You can't go to the specified record'. I'm assuming it has
somethng to with the fact that the records that are getting inserted in
the Instructor table didn't get updated or something because when i
replace the InstructorID on the Course form with one that already
exists , enter all the course infromation and then click 'Add Course',
it inserts the record with no problem. I'm really stuck here...I've
tried all kinds of things but to no avail..please help...
 
S

SteveS

Shaldaman said:
Hi,
I have 2 forms - 'New Instructor' and 'New Course' which are linked.
The first form has the following fields - InstructorID(autonumber
primary key), Instructor First Name, Instructor Lase Name and
Department, all of which are linked to an 'Instructor' table. The 'New
Course' form has 3 fields - InstructorID, CourseID, Course Name. If a
user on the 'New Instructor' form clicks a button 'Add New
Course'(named Command14), it should do 2 things:
1) Add the values for InstructorID, Instructor First Name, Instructor
Lase Name and Department into the 'Instructor' table, and
2) Open the 'New Course' form, displaying the corresponding
InstructorID but the other 2 fields should remain blank. When the user
enters information for CourseID & Course Name, and click a button 'Add
Course' on the form, the CourseID & Course Name are to be inserted into
another table 'Course'. The 'Instructor' table has a one-to-many
relationship with the 'Course' table.

To achieve this, I have an OnClick function associated with the 'Add
New Course' button on the 'New Instructor' form:

Private Sub Command14_Click()
On Error GoTo Err_Command14_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Course"

DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.GoToRecord , , acNewRec
Exit_Command14_Click:
Exit Sub

Err_Command14_Click:
MsgBox Err.Description
Resume Exit_Command14_Click

End Sub

And for the 'Course' form, I have the following OnCurrent event:

Private Sub Form_Current()
Me!InstructorID = Forms![Instructor]!InstructorID
Me!CourseID = ""
Me!CourseName = ""
Me!CourseDesc = ""

End Sub

My problem is, when I click 'Add Course' on the 'Course' form, I get
the "You can't go to the specified record'. I'm assuming it has
somethng to with the fact that the records that are getting inserted in
the Instructor table didn't get updated or something because when i
replace the InstructorID on the Course form with one that already
exists , enter all the course infromation and then click 'Add Course',
it inserts the record with no problem. I'm really stuck here...I've
tried all kinds of things but to no avail..please help...

To save the new record in form "New Instructor", add the following:

'****** SNIP ******
Dim stDocName As String
Dim stLinkCriteria As String

'-------- ADDED ----------
' this saves the new record
If Me.Dirty Then
Me.Dirty = False
End If
'-------- ADDED ----------

stDocName = "Course"
'****** SNIP ******

HTH
 

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