Kryst51
Singin' in the Hou. Rain
- Local time
- Today, 10:47
- Joined
- Jun 29, 2009
- Messages
- 1,896
OK... In a previous thread I came out with some code that works and looks like this:
Currently this adds a new record to my table, "tblTagRelationships" by adding the current date and time to a new record. This date/time is really irrelevant to anything I am doing and is useless data given the help in a previous thread to do what I need in a better way.
What I found, though, is that now the bottom part of the code where I am adding "NCRID" to the newly created record doesn't work. And I thought, too, that it might be cleaner to use this NCRID to create the new record in "tblTagRelationships" instead of creating a useless date/time stamp.... The problem is that I get a run time error (The Red text is the items that the Blue text uses, and the blue text is what throws the error when I hit debug) :
I assume this means that the SQL isn't reading my "NCRID" value which is set to the current ID, and I don't know why, am I referring to it incorrectly? Here is the updated code:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Sets ADO connection
Set MasterDbConn = CurrentProject.Connection
'Declarations
Dim rsNewRTId As ADODB.Recordset
'Dim rsNewRTIDSQL As String
Dim NewRTID As Integer
Set rsNewRTId = New ADODB.Recordset
'Add Record to tag relationships
Dim NewTagRelQRY As String
Dim AssignNCR As String
Dim NCRID As Integer
Dim fkTRID As Integer
[COLOR=red]NCRID = Me.Parent.txtNCRHeaderID[/COLOR]
'SQL to create a new record in the tblTagRelationships
[COLOR=cyan]NewTagRelQRY = "Insert INTO tblTagRelationships(DateTimeStamp)" & _[/COLOR]
[COLOR=cyan]" VALUES (Now());"[/COLOR]
[COLOR=red]AssignNCR = "Update tblTagRelationships" & _[/COLOR]
[COLOR=red]" Set fkNCRHeaderID =" & NCRID & _[/COLOR]
[COLOR=red]" Where pkTagRelationshipID =" & fkTRID[/COLOR]
'If the record is a new record then a Tag Relationship
'is created and assigned. But if it is not a new
'record then this has already been done, and there
'will be problems if it is given a new one.
If Me.NewRecord = True Then
'This will add a new record in the tag relationships
'and put that number into the tag foreign key
With CurrentProject.Connection
[COLOR=cyan].Execute NewTagRelQRY[/COLOR]
NewRTID = .Execute("SELECT @@Identity")(0)
Me.fkTagRelationshipID = NewRTID
fkTRID = Me.fkTagRelationshipID
End With
End If
'This will assign the current NCR to the tagrelationship
[COLOR=red]If Me.NewRecord = True Then[/COLOR]
[COLOR=red] CurrentProject.Connection.Execute AssignNCR[/COLOR]
[COLOR=red]End If[/COLOR]
End Sub
Currently this adds a new record to my table, "tblTagRelationships" by adding the current date and time to a new record. This date/time is really irrelevant to anything I am doing and is useless data given the help in a previous thread to do what I need in a better way.
What I found, though, is that now the bottom part of the code where I am adding "NCRID" to the newly created record doesn't work. And I thought, too, that it might be cleaner to use this NCRID to create the new record in "tblTagRelationships" instead of creating a useless date/time stamp.... The problem is that I get a run time error (The Red text is the items that the Blue text uses, and the blue text is what throws the error when I hit debug) :
Run-time error '2147217904(80040e10) No value given for one or more parameters
I assume this means that the SQL isn't reading my "NCRID" value which is set to the current ID, and I don't know why, am I referring to it incorrectly? Here is the updated code:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Sets ADO connection
Set MasterDbConn = CurrentProject.Connection
'Declarations
Dim rsNewRTId As ADODB.Recordset
'Dim rsNewRTIDSQL As String
Dim NewRTID As Integer
Set rsNewRTId = New ADODB.Recordset
'Add Record to tag relationships
Dim NewTagRelQRY As String
Dim AssignNCR As String
Dim NCRID As Integer
Dim fkTRID As Integer
[COLOR=red][B]NCRID = Me.Parent.txtNCRHeaderID[/B][/COLOR]
'SQL to create a new record in the tblTagRelationships
[COLOR=red][B]NewTagRelQRY = "Insert INTO tblTagRelationships(fkNCRHeaderID)" & _[/B][/COLOR]
[B][COLOR=red]" VALUES (NCRID);"[/COLOR][/B]
AssignNCR = "Update tblTagRelationships" & _
" Set fkNCRHeaderID =" & NCRID & _
" Where pkTagRelationshipID =" & fkTRID
'If the record is a new record then a Tag Relationship
'is created and assigned. But if it is not a new
'record then this has already been done, and there
'will be problems if it is given a new one.
If Me.NewRecord = True Then
'This will add a new record in the tag relationships
'and put that number into the tag foreign key
With CurrentProject.Connection
[COLOR=cyan][B][U].Execute NewTagRelQRY[/U][/B][/COLOR]
NewRTID = .Execute("SELECT @@Identity")(0)
Me.fkTagRelationshipID = NewRTID
fkTRID = Me.fkTagRelationshipID
End With
End If
'This will assign the current NCR to the tagrelationship
If Me.NewRecord = True Then
CurrentProject.Connection.Execute AssignNCR
End If
End Sub
Last edited: