Hello, I am currently doing a VB project for a course, I have all the features working bar the datagrid, when i use the program without the datagrid it works perfectly, however if i put a datagrid in I can only add and delete, but I am unable to cancel as the datagrid gives me an error, I have looked everywhere to get a solution and even asked my course tutor but no luck, so any help will be agreatly appreciated.

The code for my Add/Cancel command is;

Private Sub cmdSave_Click()
    'Save the current record

    On Error GoTo HandleSaveErrors
    adoPhones.Recordset.Update
    txtProductCode.Locked = True
    EnableButtons
    cmdSave.Enabled = False
    cmdAdd.Caption = "Add"

cmdSave_Click_Exit:
    Exit Sub
    
HandleSaveErrors:
    Dim strMessage As String

    strMessage = "Record could not be saved." & vbCrLf & vbCrLf _
        & Err.Description
    MsgBox strMessage, vbExclamation, "Database Error"
    SetUpAddRecord
    Resume cmdSave_Click_Exit
        
        
End Sub

Thank you very much DW :)

Recommended Answers

All 3 Replies

Can you please show us what your SetUpAddRecords code says?

Can you please show us what your SetUpAddRecords code says?

I posted the wrong code previously I just noticed, I will post the add/cancel buttons and set up add records

Private Sub cmdAdd_Click()

    'Add New Record

    On Error GoTo HandleAddErrors
    If cmdAdd.Caption = "Add" Then
        adoPhones.Recordset.AddNew
        txtProductCode.Locked = False
        txtProductCode.SetFocus
        DisableButtons
        cmdSave.Enabled = True
        cmdAdd.Caption = "Cancel"
    Else
        adoPhones.Recordset.CancelUpdate
        txtProductCode.Locked = True
        EnableButtons
        cmdSave.Enabled = False
        cmdAdd.Caption = "Add"
    End If

cmdAdd_Click_Exit:
    Exit Sub
    
HandleAddErrors:
    Dim strMessage As String
    strMessage = "Cannot complete operation." & vbCrLf & vbCrLf _
            & Err.Description
    MsgBox strMessage, vbExclamation, "Database Error"
    On Error GoTo 0       'Turn off error trapping

End Sub
Private Sub SetUpAddRecord()

    Dim strProductCode As String
    Dim strModel As String
    Dim strFeatures As String
    Dim strNetwork As String
    Dim strTariff As String
    Dim strSimFree As String

    On Error Resume Next

    strProductCode = txtProductCode.Text
    strModel = txtHandsetModel.Text
    strFeatures = txtFeatures.Text
    strNetwork = cboNetwork.Text
    strTariff = cboTariffType.Text
    strSimFree = txtSimFree.Text

    adoPhones.Recordset.AddNew

    With txtProductCode
        .Text = strProductCode
        .SelStart = 0
        .SelLength = Len(.Text)
        .SetFocus
    End With
    txtHandsetModel.Text = strModel
    txtFeatures.Text = strFeatures
    cboNetwork.Text = strNetwork
    cboTariffType.Text = strTariff
    txtSimFree.Text = strSimFree
End Sub

Created a work around to the problem by having an entry made up of useless data be added to the database and then deleted straight away

Private Sub cmdAdd_Click()

    'Add New Record

    On Error GoTo HandleAddErrors
    If cmdAdd.Caption = "Add" Then
        adoPhones.Recordset.AddNew
        txtProductCode.Locked = False
        txtProductCode.SetFocus
        DisableButtons
        cmdSave.Enabled = True
        cmdAdd.Caption = "Cancel"
    Else
    
        'This is a work around for the datagrid problem
        
        txtProductCode.Text = "P1"
        txtHandsetModel.Text = "1"
        txtFeatures.Text = "1"
        cboNetwork.Text = "Meteor"
        cboTariffType.Text = "Bill Pay"
        txtPrice.Text = "1"
        txtSimFree.Text = "Yes"
        adoPhones.Recordset.CancelUpdate
        
        With adoPhones.Recordset
        .MoveLast
        .Delete
        .MovePrevious
        End With
        
        'End of work around
        
        txtProductCode.Locked = True
        EnableButtons
        cmdSave.Enabled = False
        cmdAdd.Caption = "Add"
    End If

cmdAdd_Click_Exit:
    Exit Sub
    
HandleAddErrors:
    Dim strMessage As String
    strMessage = "Cannot complete operation." & vbCrLf & vbCrLf _
            & Err.Description
    MsgBox strMessage, vbExclamation, "Database Error"
    On Error GoTo 0       'Turn off error trapping
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.