I have this code that should update my access database, but I keep getting an error saying:
It is not possible so change or delete the regists because the table
‘MembrosCompasso’ has relating records.
This is my code:
Dim query1 As String = "UPDATE MembrosCompasso SET MembrosCompasso.BI=@compass_bi WHERE MembrosCompasso.BI=Membros.BI" ' Membros.Pai=@pai, Membros.Mae=mae WHERE BI=@BI"
Dim query As String = "UPDATE Membros SET Membros.BI=@num_bi, Membros.Nome=@nome, Membros.Hablitacoes=@hablitacoes, Membros.Contribuinte=@contribuinte, Membros.Telemov=@telemov, Membros.DataNasc=@natnasc, Membros.LocalNasc=@localnasc, Membros.DataBaptismo=@databapt, Membros.LocalBapt=@localbapt, Membros.Sexo=@sexo, Membros.Naturalidade=@natu, Membros.Profissao=@prof, Membros.Estadocivil=@estadciv, Membros.DataCasamento=@datacas, Membros.LocalCasamento=@localcas, Membros.Notas=@notas, Membros.Email=@email WHERE Membros.BI=@BI"
con.Close()
con.Open()
Dim command As New OleDbCommand(query, con)
Dim command1 As New OleDbCommand(query1, con)
' Indicação dos parâmetros que serão actualizados
command1.Parameters.Add("@compass_bi", OleDbType.VarChar).Value = BI.Text
command.Parameters.Add("@num_bi", OleDbType.VarChar).Value = BI.Text
command.Parameters.Add("@nome", OleDbType.VarChar).Value = Nome.Text
command.Parameters.Add("@hablitacoes", OleDbType.VarChar).Value = hablitacoes.Text
command.Parameters.Add("@contribuinte", OleDbType.VarChar).Value = ncront.Text
command.Parameters.Add("@telemov", OleDbType.VarChar).Value = telmov.Text
command.Parameters.Add("@natnasc", OleDbType.VarChar).Value = datnasc.Text
command.Parameters.Add("@localnasc", OleDbType.VarChar).Value = NascLocal.Text
command.Parameters.Add("@databapt", OleDbType.VarChar).Value = DataBapt.Text
command.Parameters.Add("@localbapt", OleDbType.VarChar).Value = LocalBapt.Text
command.Parameters.Add("@sexo", OleDbType.VarChar).Value = sexo.Text
command.Parameters.Add("@natu", OleDbType.VarChar).Value = natu.Text
command.Parameters.Add("@prof", OleDbType.VarChar).Value = profi.Text
command.Parameters.Add("@estadciv", OleDbType.VarChar).Value = estciv.Text
command.Parameters.Add("@datacas", OleDbType.VarChar).Value = datcas.Text
command.Parameters.Add("@localcas", OleDbType.VarChar).Value = localcas.Text
command.Parameters.Add("@notas", OleDbType.VarChar).Value = notas.Text
command.Parameters.Add("@email", OleDbType.VarChar).Value = email.Text
command.Parameters.Add("@BI", OleDbType.VarChar).Value = numero_BI
There is a screenshot of my access db schema here.
It looks like you are trying to update the table
MembrosCompassobefore you make the change in tableMembros.Because you set
MembrosCompasso.BIto be related toMembros.BI, it must exists in that table first.If you set your relationship in Access to be automatically updated, you will not have to perform any manual update to
MembrosCompasso, they will be updated automatically when you update the source field.For instance, say I have a
POtable that has manyPOItem.I could set the one-to-many relationship between them as below:
In that case, if I update
PO.ID, allPOItem.POIDfields will be updated.Same if I delete a given
POrecord, all relatedPOItemrecords will also be deleted.Have a look at the MS Access documentation about table relationships.