Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Almost Embarrassed to Ask Insert Questino 2

Status
Not open for further replies.

JAES

Technical User
Jul 25, 2004
100
GB
I've been away from Access for years and simple things seem to baffle me.
DoCmd.RunSQL "INSERT INTO tStudents (StudentID) VALUES (StudentID) "

The Student ID is an Integer. As I close the current form I want to insert the StudentID value into the table tStudents Column StudentID

Any help is appreciated
 
Thanks Duane, It still has an error and the line is highlited. The Me.StudentID has the correct value though. The table I'm working with currently is named tActivities. Once I get it working I will use is other places.

Private Sub Command116_Click()
DoCmd.RunSQL "INSERT INTO tActivities (StudentID) VALUES " & Me.StudentID
DoCmd.Close acForm, "frmAddNewStudent"
End Sub

The Table tActivities has the following:
Field Name StudentID data type is Number (long Integer)
10 other fields with Short Text and Numbers

Thank you
 
Run-Time error '3134'
Syntax error on Insert Into Statement.

I'm trying different tables and different fields and the result is the same. But the Me. statement always has the data I'm trying to insert.
 
OK, all of a sudden this works - DoCmd.RunSQL "INSERT INTO tActivities (StudentID) VALUES (StudentID)". My StudentID was 7 digits long and I had them formatted as Integer. One of the few things I did was change them to Text.
 
I don't know what t say. Here is the code. It is the click event for closing the form. I need to populate the other three tables with only the StudentID. This code does that. If something goes terribly wrong later I will know where to look!

Private Sub Command116_Click()
Dim StudentID As Integer
DoCmd.RunSQL "INSERT INTO tActivities (StudentID) VALUES (StudentID)"
DoCmd.RunSQL "INSERT INTO tEmContact (StudentID) Values (StudentID)"
DoCmd.RunSQL "INSERT INTO tInteractions (StudentID) Values (StudentID)"
DoCmd.Close acForm, "frmAddNewStudent"
End Sub
 
JAES,
You declare [tt]StudentID[/tt] but never assign any value to it.

You may also consider this:

Code:
Private Sub Command116_Click()
Dim StudentID As Integer
Dim strSQL As String

StudentID = [red]???[/red]

strSQL = "INSERT INTO tActivities (StudentID) VALUES (" & StudentID & ")"
DoCmd.RunSQL strSQL

strSQL = "INSERT INTO tEmContact (StudentID) Values (" & StudentID & ")"
DoCmd.RunSQL strSQL

strSQL = "INSERT INTO tInteractions (StudentID) Values (" & StudentID & ")"
DoCmd.RunSQL strSQL

DoCmd.Close acForm, "frmAddNewStudent"
End Sub

This way you can examine your INSERT statement before is executed.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Thanks Duane and Andy. I had changed the value of StudentID to String but didn't change the declare statement. The latest code works great by changing the declare to string. It's amazing how much you lose not doing this for 7 years. Hopefully this will wake up some memory cells! Thank you again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top