简体   繁体   中英

Can't insert null value into a foreign key field in MS Access

I'm using VB.NET to insert records into an MS Access table. I've run into an issue where I'm inserting a record where one of the values is blank. VB.NET throws an error because it says that a value is required for the field I'm trying to insert.

The Sense_LanguageSource table has six fields including the ID field:

  • LanguageSourceID (Autonumber)
  • SenseFK (Number)
  • LanguageCode (Number)
  • SourceWord (Short Text)
  • LanguageSourceType (Short Text)
  • IsWaseieigo (Yes/No)

The LanguageCode field is also a Foreign Key, which links to the LanguageCodeValue table, which contains a list of LanguageCodes.

This is the code I'm running to perform the insert in VB.NET:

'Finds the ID of the LanguageCode
sql = "SELECT [LanguageCodeID] FROM [LanguageCodeValue] WHERE [LanguageCode] = """ & ls.LanguageCode & """"
cmd = New OleDbCommand(sql, myConnection)
lcodeID = CInt(cmd.ExecuteScalar)

'Inserts the LanguageSource element
sql = "INSERT INTO [Sense_LanguageSource] ([SenseFK], [LanguageCode], [SourceWord], [LanguageSourceType], [IsWaseieigo]) VALUES (?, ?, ?, ?, ?)"

cmd = New OleDbCommand(sql, myConnection)
cmd.Parameters.Add(New OleDbParameter("[SenseFK]", senseID))
cmd.Parameters.Add(New OleDbParameter("[LanguageCode]", lcodeID))
cmd.Parameters.Add(New OleDbParameter("[SourceWord]", ls.SourceWord))
cmd.Parameters.Add(New OleDbParameter("[LanguageSourceType]", ls.LanguageSourceType))
cmd.Parameters.Add(New OleDbParameter("[IsWaseieigo]", ls.IsWaseieigo))

Try
    cmd.ExecuteNonQuery()
    cmd.Dispose()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

The problem is that the value I'm passing to LanguageCode is 0. My code is taking an object which may or may not contain a LanguageCode value, and running a Select query to find its ID in the LanguageCodeValue table. Because the particular object I'm evaluating doesn't have a LanguageCode, the first select query returns 0. When I try to pass 0 to the insert command, I get the error:

"You cannot add or change a record because a related record is required in table 'LanguageCodeValue'."

I'm not sure how to work around this. I want there to be a link between the Sense_LanguageSource table and the LanguageCodeValue table, but I don't want it to be required to insert a value for LanguageCode . Is it possible to insert a null value into a Foreign Key field? Or is there a way to make it not required to insert a value into this field?

Okay, I've worked out how to get around this problem - disable Referential Integrity on the relationship between LanguageCodeValue and Sense_LanguageSource . Probably not the best solution, but it does allow me to input 0 on the Foreign Key field.

Edit: I've found a better way. Create a "Null" record on the LanguageCodeValue table, with blank fields, and refer to that whenever I want to indicate that there is no LanguageCode attached to the Sense_LanguageSource table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM