Delete a record based on a value entered by the user and display therest of the records

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • agarwasa2008@gmail.com

    Delete a record based on a value entered by the user and display therest of the records

    Hi,

    I would like to delete a record based on a user entered string. Here
    are the
    details. I have a txtFind textbox. A string is entered by the user.
    Based on
    that string value it displays that one record in the fields in the
    frmDeleteCompon ent form. There is also a lstDelete listBox which
    displays all
    the record. But for some reason it does not delete that particular
    record as
    requested by the user. I have written the sequence I am following and
    I am
    hoping that someon is able to answer my question. I need help.

    Thanks,
    SA
    '============== =============== ===
    My code in frmDeleteCompon ent:
    '============== =============== ===
    Private Sub cmdDelete_Click ()
    On Error GoTo Err_cmdDelete_C lick

    Dim stDocName As String

    Dim Msg, Style, Title, response
    Msg = "Do you wish to Delete This Component?"
    Style = vbYesNo + vbExclamation
    Title = "Delete Component Confiramtion"
    response = MsgBox(Msg, Style, Title)

    If response = vbYes Then
    stDocName = "macroDelet e"
    DoCmd.RunMacro stDocName
    End If

    Exit_cmdDelete_ Click:
    Exit Sub

    Err_cmdDelete_C lick:
    MsgBox Err.Description
    Resume Exit_cmdDelete_ Click

    End Sub

    '************** *************** *********

    Private Sub cmdFind_Click()
    On Error GoTo Err_cmdFind_Cli ck

    DoCmd.GoToContr ol txtPartNumber.N ame
    DoCmd.FindRecor d Me.txtFind.Valu e, acEntire, False, acSearchAll,
    True,
    acCurrent, True

    Exit_cmdFind_Cl ick:
    Exit Sub

    Err_cmdFind_Cli ck:
    MsgBox Err.Description
    Resume Exit_cmdFind_Cl ick

    End Sub


    '============== =============== =======
    Details of macroDelete:

    Action:
    1 SetWarnings
    2 OpenQuery
    3 RunMacro
    4 Close
    5 OpenForm

    Details
    ----------
    1 SetWarning: is set to No
    2 OpenQuery: calls queryDelete ; View: datasheet; dataMode: Edit
    Here's the sql statement:
    DELETE tblParts.*, "PartNumber " AS Expr1
    FROM tblParts
    WHERE ((("PartNumber" )="txtFind")) ;


    3 runMacro: runs another macro called MacroDeleteDisp lay with
    repeatCount
    set to
    Action: GoToControl: lstDelete
    RunCommand: Refresh
    4 Close: Actually closes the frmDeleteCompon ent (I have included the
    code
    from that form above)
    5 OpenForm: frmDeleteCompon ent; View:Form; Window Mode: Dialog
  • Rich P

    #2
    Re: Delete a record based on a value entered by the user and display the rest of the records

    Try something like this in a command button:

    Private Sub Command1_Click( )
    DoCmd.SetWarnin gs False
    If MsgBox("Do you want to delete this record?",vbYesN o, "Delete Record
    Yes/No") = vbYes Then
    DoCmd.RunSql "Delete * From tblPars Where PartNumber = '" & txtFind &
    "'"
    End If
    DoCmd.SetWarnin gs True
    End If

    Note: to make the code more readable - you may want to rename txtFind as
    txtPartNumber. Also, when using parameters in a query as above - you
    have to delimit the parameter with single quotes ' ' if it is a string.
    In Jet sql -- data parameters are delimted with the # symbol. Number do
    not require delimiting. I also temporarily disabled the default
    warnings message and then re-enable it at the end of the procedure.
    Otherwise you will get the default message everytime you delete
    something:

    1 Record is about to be deleted. Continue Yes/No?

    You already have a custom message with the If MsgBox(...) statement.

    Rich

    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Rich P

      #3
      Re: Delete a record based on a value entered by the user and display the rest of the records

      Correction: Date Parameters #3/25/2007# require the # symbol as the
      delimeter in Jet Sql. Most sql languages use the single quotes ' ' for
      delimiting dates the same as strings. That I know, only Jet sql (Access
      Jet sql) uses the # symbol for delimiting dates.



      Rich

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • agarwasa2008@gmail.com

        #4
        Re: Delete a record based on a value entered by the user and displaythe rest of the records

        On May 19, 12:17 pm, Rich P <rpng...@aol.co mwrote:
        Try something like this in a command button:
        >
        Private Sub Command1_Click( )
        DoCmd.SetWarnin gs False
        If MsgBox("Do you want to delete this record?",vbYesN o, "Delete Record
        Yes/No") = vbYes Then
          DoCmd.RunSql "Delete * From tblPars Where PartNumber = '" & txtFind &
        "'"
        End If
        DoCmd.SetWarnin gs True
        End If
        >
        Note: to make the code more readable - you may want to rename txtFind as
        txtPartNumber.  Also, when using parameters in a query as above - you
        have to delimit the parameter with single quotes ' ' if it is a string.
        In Jet sql -- data parameters are delimted with the # symbol.  Number do
        not require delimiting.  I also temporarily disabled the default
        warnings message and then re-enable it at the end of the procedure.
        Otherwise you will get the default message everytime you delete
        something:
        >
        1 Record is about to be deleted.  Continue Yes/No?
        >
        You already have a custom message with the If MsgBox(...) statement.
        >
        Rich
        >
        *** Sent via Developersdexht tp://www.developersd ex.com***

        Well, I had one more question ---
        how would I show all the records minus the deleted record in the
        lstBox. I would like to refresh the lstBox with the right information.
        Currently it is showing all the records.

        Also, I would like to know what is wrong with the way I have set the
        macro originally.

        Thanks,
        Sangeeta.



        Comment

        • Rich P

          #5
          Re: Delete a record based on a value entered by the user and display the rest of the records

          For general purpose operations - the built in macros are fairly ideal
          because they don't require any maintenance. But if you need to
          customize some operation - the macro is not the most ideal way to go
          because they are not easy to customize. Thus, you would use VBA to
          write custom procedures/functions.

          As for "refreshing " your listbox with the current rows in your table -
          just add this to the sample procedure:

          Private Sub Command1_Click( )
          DoCmd.SetWarnin gs False
          If MsgBox("Do you want to delete this record?",vbYesN o, "Delete Record
          Yes/No") = vbYes Then
          DoCmd.RunSql "Delete * From tblPars Where PartNumber = '" & txtFind &
          "'"
          End If
          listbox1.Rowsou rce = "Select fldx From tblParts"
          listbox1.Requer y
          Me.Requery
          DoCmd.SetWarnin gs True

          End If



          Rich

          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          • karsagarwal@gmail.com

            #6
            Re: Delete a record based on a value entered by the user and displaythe rest of the records

            On May 19, 2:15 pm, Rich P <rpng...@aol.co mwrote:
            For general purpose operations - the built in macros are fairly ideal
            because they don't require any maintenance.  But if you need to
            customize some operation - the macro is not the most ideal way to go
            because they are not easy to customize.  Thus, you would use VBA to
            write custom procedures/functions.  
            >
            As for "refreshing " your listbox with the current rows in your table -
            just add this to the sample procedure:
            >
            Private Sub Command1_Click( )
             DoCmd.SetWarnin gs False
             If MsgBox("Do you want to delete this record?",vbYesN o, "Delete Record
             Yes/No") = vbYes Then
               DoCmd.RunSql "Delete * From tblPars Where PartNumber = '" & txtFind &
             "'"
             End If
             listbox1.Rowsou rce = "Select fldx From tblParts"
             listbox1.Requer y
             Me.Requery
             DoCmd.SetWarnin gs True
            >
            End If
            >
            Rich
            >
            *** Sent via Developersdexht tp://www.developersd ex.com***
            Thank you for all your help. It works like magic.

            SA

            Comment

            Working...