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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT! Datagrid various update & delete problems

Status
Not open for further replies.

kmas3

IS-IT--Management
Jun 1, 2003
2
AU
I have a Master Detail form linked to access tables, which I used the form wizard to make. I havent been using visual basic for very long so I am unsure where I am going wrong. I am having lots of problems with the form.
The form has text fields for the master, and a datagrid to display the detail, and add, delete,refresh & edit buttons.
When I try to add a record the datagrid will only let me add the master details and one row into the datagrid. I tried using various keys to go to the next line but it will not work. If I then update the record I have to refresh it before I can add anymore data to the datagrid, and once its been refreshed I can add as many details as I like.

Then when I go to delete a record it will not work I get the error 'record cannot be deleted or changed because table table cust_ord_line includes related records'.
If I delete the datagrid contents using the delete button on the keyboard, and then try to refresh or delete the master data I get the message 'row handle referred to a deleted row or a row marked for deletion'. Can anyone suggest a way I can fix my code to update or delete the whole record correctly. The code for the update and delete buttons is below. This is extremely urgent and I could not find any other postings about this in the forum. Thanks in advance.

Private Sub cmdUpdate_Click()
On Error GoTo UpdateErr
' if required fields not empty update record
If txtFields(0) = "" Or txtFields(1) = "" Or txtFields(3) = "" Or txtFields(4) = "" Then
MsgBox "You have not filled in one of the required fields, please try again!", , "Error"
Else
adoPrimaryRS.UpdateBatch adAffectAll
If mbAddNewFlag Then
adoPrimaryRS.MoveLast 'move to the new record
End If
mbEditFlag = False
mbAddNewFlag = False
SetButtons True
mbDataChanged = False
End If
'refresh recordset
Exit Sub
UpdateErr:
cmdCancel.Enabled = False 'disable cancel until adjusted
MsgBox "Some information you entered is incorrect, please adjust it or consult manual and try again!", , "Error"
End Sub

Private Sub cmdDelete_Click()
On Error GoTo DeleteErr
If MsgBox("Are you sure you want to delete this record?", vbQuestion + vbYesNo, " Continue?") = vbYes Then
'delete current record
With adoPrimaryRS
If adoPrimaryRS.EOF = False And adoPrimaryRS.BOF = False Then
.Delete
Set grdDataGrid.DataSource = Nothing
adoPrimaryRS.Requery
Set grdDataGrid.DataSource = adoPrimaryRS("ChildCMD").UnderlyingValue
.MoveNext
Exit Sub
Else
MsgBox "There are no records to delete!", , "Attention!"
End If
End With
End If
Exit Sub
DeleteErr:
MsgBox "To delete a customer order you must first go to edit and delete order contents, please consult manual", , "Error"
 
You should use a subform instead of just a datagrid.
Also, check on the way you created your indexes since not allways can you have a one to many relationship and be able to update.
For an example see the "Order Entry" example that comes with Access.
Good Luck.
 
Thanks Tigreblanco,

I forgto to check the relationships in Access, that was causing the deletion problem as it wasnt set to casecade delete. I also checked the datagrid porperties and found the tab settigns were wrong.
Its all fixed now though thanks anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top