• Not saving a record – VBA (2002 SP2?)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Not saving a record – VBA (2002 SP2?)

    Author
    Topic
    #425964

    As usual – a dumb question grin that I’m hoping has an easy answer.

    I have a main form which has an ‘add new qualification’ button which executes my form using
    DoCmd.OpenForm “fmaddqualification”, acNormal, , , acFormAdd

    I have a click event on this form which attempts to do some integrity checks and discard the field changes for the record update. Sadly, it seems to add a record even where I’ve detected it is a duplicate.
    Any suggestions on the correct approach?

    Private Sub Save_Click()
    Dim Qual
    Dim I As Long
    If IsNull(Me.Institutions.Value) Or _
    IsNull(Me.QualificationDescription.Value) Or _
    IsNull(Me.QualificationTypeID.Value) Then
    MsgBox “All fields are required to be entered”
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acDelete, , acMenuVer70
    DoCmd.Close acForm, Me.Name, acSaveNo
    Exit Sub
    End If
    For I = 0 To Forms!fmQualificationMain.Qualifications.ListCount – 1
    Qual = Forms!fmQualificationMain.Qualifications.ItemData(I)
    If Qual = Me.QualificationDescription.Value Then
    MsgBox Me.QualificationDescription & ” already exists”, , “Qualification not added”
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acDelete, , acMenuVer70 ‘ <—– dizzy —- this doesn’t seem to work —- nor anything else I try
    DoCmd.Close acForm, Me.Name, acSaveNo
    Exit Sub
    End If
    Next I
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    DoCmd.Close acForm, Me.Name, acSaveNo

    Exit_Save_Click:
    Exit Sub

    Err_Save_Click:
    MsgBox Err.Description
    Resume Exit_Save_Click

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #982952

      Try replacing the delete instruction with Me.Undo

      BTW, you can use

      RunCommand acCmdDeleteRecord

      instead of the antiquated Access 95 (=7.0) code generated by the Wizard to delete a record.

      • #982954

        Hans
        Thanks very much cheers
        That was one of the by-products of having Access generate code – I remember my early days with Excel code generation all too painfully grin

    Viewing 0 reply threads
    Reply To: Not saving a record – VBA (2002 SP2?)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: