Hey all,
I'm trying to fix what I believe to be a design flaw in the database I'm working on. I'm creating relationships that didn't exist so I can avoid orphan records.
So I find a suitable PK/FK to create a relationship and I attempt to add it with RI enabled. Sorry you have orphan records. I created a unmatched query using the wizard, removed the offending records and created the relationship. Sweet!
So I go on to testing DB with my new relationship by adding a record through a form to the parent table. Err 3201 can't add or change, a related record is required in the child table. Hmm, over to the back end, open up the parent table in design view, select FK field default value 0. I cleared the default value to stick a null in there instead of 0 (which I believe is correct) as the FK's presence is optional.
Ok, Back to testing! Add a new parent record to the table via form entry button click. Error, Access can't update all the records in the update query. Didn't update 1 field due to key violation. Do you want to ignore and move on with your life? Well that doesn't seem like a viable long term option. Into the VBA for attempting to decipher the event procedure behind the button click.
So among many other things the button click does it calls this function. When I get the "Can't update error" described above and choose not to continue I get a VBA error SQL cancelled and the code in Green below is highlighted.
With my limited VBA knowledge I assume the code in Red is the offending code, which to me appears to set null fields to 0. That does have a purpose on the form and I want it to happen for all fields except my PK/FK that I'm trying to enforce RI. To attempt to skip the code setting nulls to 0 for my PK/FK field I created the select case in Blue. To identify the appropriate fldName I ran the code with the debug.print first and then made the select case.
My current issue when I try to add a parent record is a runtime 94. Invalid use of null. The highlighted line is my PK/FK in the select case statement. Case [AircraftN]
I didn't write the database, the form or the code and I'm pretty amateur using Access for complex solutions. I believe I just need to skip the red code for my PK/FK field but the select case was my only idea for doing so. Any thoughts/ideas for another direction would certainly be appreciated!
Chris
I'm trying to fix what I believe to be a design flaw in the database I'm working on. I'm creating relationships that didn't exist so I can avoid orphan records.
So I find a suitable PK/FK to create a relationship and I attempt to add it with RI enabled. Sorry you have orphan records. I created a unmatched query using the wizard, removed the offending records and created the relationship. Sweet!
So I go on to testing DB with my new relationship by adding a record through a form to the parent table. Err 3201 can't add or change, a related record is required in the child table. Hmm, over to the back end, open up the parent table in design view, select FK field default value 0. I cleared the default value to stick a null in there instead of 0 (which I believe is correct) as the FK's presence is optional.
Ok, Back to testing! Add a new parent record to the table via form entry button click. Error, Access can't update all the records in the update query. Didn't update 1 field due to key violation. Do you want to ignore and move on with your life? Well that doesn't seem like a viable long term option. Into the VBA for attempting to decipher the event procedure behind the button click.
So among many other things the button click does it calls this function. When I get the "Can't update error" described above and choose not to continue I get a VBA error SQL cancelled and the code in Green below is highlighted.
With my limited VBA knowledge I assume the code in Red is the offending code, which to me appears to set null fields to 0. That does have a purpose on the form and I want it to happen for all fields except my PK/FK that I'm trying to enforce RI. To attempt to skip the code setting nulls to 0 for my PK/FK field I created the select case in Blue. To identify the appropriate fldName I ran the code with the debug.print first and then made the select case.
My current issue when I try to add a parent record is a runtime 94. Invalid use of null. The highlighted line is my PK/FK in the select case statement. Case [AircraftN]
Code:
Function PostFlightSub()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSql As String
Dim tdf As TableDef
Dim cntr As Long
Dim FldName As String
DoCmd.RunCommand acCmdSaveRecord
If Nz(Me!SelectLesson) = 0 Or Nz(Me.LessonFlightNum) = 0 Then
MsgBox (" You MUST select a lesson and flight first! ")
PostFlightSub = False
Exit Function
End If
If Nz(Me.RecordEntryDate) = 0 Then
Me.RecordEntryDate = Now()
End If
DelTableIfExists ("STDFlightsDummy")
strSql = "SELECT STDFlightsTemp.* INTO STDFlightsDummy FROM STDFlightsTemp;"
DoCmd.RunSQL (strSql)
Me.RecordSource = "STDFlightsDummy"
Call PostElements
Set db = CurrentDb()
'plug zeros in any null fields
Set rst = db.OpenRecordset("StdFlightsTemp")
rst.MoveFirst
Set tdf = db.TableDefs("STDFlightsTemp")
FldCount = tdf.Fields.count
[COLOR=Red]For cntr = 1 To FldCount - 1
FldName = "[" & (tdf.Fields(cntr).Name) & "]"[/COLOR]
[COLOR=Red] [COLOR=RoyalBlue]Debug.Print FldName[/COLOR]
[COLOR=Blue]'Select Case FldName
' Case [AircraftN]
' If IsNull(rst(FldName)) Then
' End If
' Case Else[/COLOR][/COLOR]
[COLOR=Blue] [COLOR=Red] If IsNull(rst(FldName)) Then
rst.Edit
rst(FldName) = 0
rst.Update
End If[/COLOR][/COLOR]
[COLOR=Blue]'End Select[/COLOR]
[COLOR=Red]Next cntr[/COLOR]
Set tdf = Nothing
rst.Close
Set rst = Nothing
strSql = "UPDATE STDFlightsTemp SET STDFlightsTemp.FltComments = Null " & _
"WHERE (((STDFlightsTemp.FltComments)='0'));"
DoCmd.RunSQL (strSql)
strSql = "UPDATE STDFlightsTemp SET STDFlightsTemp.LessonCmpDate = Null " & _
"WHERE (((STDFlightsTemp.LessonCmpDate)=0));"
DoCmd.RunSQL (strSql)
'post to STDFlights
strSql = "UPDATE STDFlightsTemp INNER JOIN STDFlights ON STDFlightsTemp.FlightID = STDFlights.FlightID SET STDFlights.FlightDate = [stdflightsTemp]![flightdate], STDFlights.InvoiceNum = [stdflightsTemp]![invoicenum], STDFlights.AircraftN = [stdflightsTemp]![aircraftn], STDFlights.InstructorID = [stdflightsTemp]![instructorID], STDFlights.GroupGround = [stdflightsTemp]![groupground], STDFlights.IndividualGround = [stdflightsTemp]![individualground], STDFlights.CPT = [stdflightsTemp]![cpt], STDFlights.SEDual = [stdflightsTemp]![sedual], " & _
"STDFlights.SESolo = [stdflightsTemp]![sesolo], STDFlights.SEPIC = [stdflightsTemp]![sepic], STDFlights.SEComplex = [stdflightsTemp]![secomplex], STDFlights.MEDual = [stdflightsTemp]![medual], STDFlights.MEPIC = [stdflightsTemp]![MEPIC], STDFlights.MEPDPIC = [stdflightsTemp]![mepdpic], STDFlights.XCDual = [stdflightsTemp]![xcdual], STDFlights.XCPICSolo = [stdflightsTemp]![xcpicsolo], STDFlights.NightDual = [stdflightsTemp]![nightdual], STDFlights.NightDualXC = [stdflightsTemp]![nightdualxc], STDFlights.NightPICSolo = [stdflightsTemp]![nightpicsolo], STDFlights.InstHood = [stdflightsTemp]![insthood], STDFlights.InstActual = [stdflightsTemp]![instactual], STDFlights.FTD = [stdflightsTemp]![ftd], STDFlights.PCATD = [stdflightsTemp]![pcatd], STDFlights.SpinUpset = [stdflightsTemp]![spinupset], STDFlights.ILS = [stdflightsTemp]![ils], STDFlights.LOC = [stdflightsTemp]![loc], STDFlights.VOR = [stdflightsTemp]![vor], " & _
"STDFlights.[RNAV/GPS] = [stdflightsTemp]![rnav/gps], STDFlights.NDB = [stdflightsTemp]![NDB], STDFlights.[LnD] = [stdflightsTemp]![LnD], " & _
"STDFlights.[LnN] = [stdflightsTemp]![LnN], STDFlights.FltComments = [stdflightsTemp]![fltComments], STDFlights.LessonCmpDate = [stdflightsTemp]![LessonCmpDate], STDFlights.RecordEntryDate = [stdflightsTemp]![RecordEntryDate];"
[COLOR=Lime]DoCmd.RunSQL (strSql)[/COLOR]
'if completed date is greater than zero plug lesson completed date into all flights for the lesson
strSql = "UPDATE STDFlights INNER JOIN STDFlightsTemp ON (STDFlights.LessonID = STDFlightsTemp.LessonID) AND (STDFlights.STDID = STDFlightsTemp.STDID) SET STDFlights.LessonCmpDate = [stdflightstemp]![lessoncmpdate] " & _
"WHERE (((STDFlightsTemp.LessonCmpDate)>0));"
DoCmd.RunSQL (strSql)
Set db = Nothing
LessonUpdate = False
Me.RecordSource = "STDFlightsTemp"
DelTableIfExists ("StdFlightsDummy")
PostFlightSub = True
End Function
Chris