How to delete record from the main table if the subtable has no record using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mseo
    New Member
    • Oct 2009
    • 183

    How to delete record from the main table if the subtable has no record using VBA

    hi,
    I have a form for delete fitered records, i need to delete the main table's record if the child has no records
    thank you in advance for any help you may provide me
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    You can remove Main table rows having no child records using a WHERE clause with the NOT EXISTS clause.

    Just checkout the SQL helpfile for using the EXISTS clause.

    Nic;o)

    Comment

    • mseo
      New Member
      • Oct 2009
      • 183

      #3
      Originally posted by nico5038
      You can remove Main table rows having no child records using a WHERE clause with the NOT EXISTS clause.

      Just checkout the SQL helpfile for using the EXISTS clause.

      Nic;o)
      thank you very much for your reply
      ok, I will check the help file
      thank you

      Comment

      • mseo
        New Member
        • Oct 2009
        • 183

        #4
        hi,
        Code:
        SELECT *
        FROM tbl_production
        WHERE not exists (select * from tbl_productiondetail Where tbl_production.batch_No = tbl_productiondetail.batch_no);
        but i need to use the above statement in vba behind cmdbutton
        thank you

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          Well done :-)

          Use the docmd.execute like:

          Code:
          docmd.execute ("<your query string>")
          Nic;o)

          Comment

          • mseo
            New Member
            • Oct 2009
            • 183

            #6
            Originally posted by nico5038
            Well done :-)

            Use the docmd.execute like:

            Code:
            docmd.execute ("<your query string>")
            Nic;o)
            thank you nico5038
            it works great
            thank you again

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32661

              #7
              For the potentially irritating status messages informing how many records have been deleted, use :
              Code:
              Call DoCmd.SetWarnings(False)
              Call DoCmd.Execute(YourSQLString)
              Call DoCmd.SetWarnings(True)

              Comment

              • nico5038
                Recognized Expert Specialist
                • Nov 2006
                • 3080

                #8
                Hi NeoPa,

                The Docmd.execute won't produce these annoying status messages, unless you add the option dbFailOnError like:
                Code:
                DoCmd.Execute("YourSQLString",dbFailOnError )
                This is one of the reasons why I prefer this method.
                The other is that a .SetWarnings(Fa lse) will also suppress a form save pop-up in design, even when out of debugging mode, that's the other reason for me to minimize the use of .SetWarnings.

                Nic;o)

                Comment

                • mseo
                  New Member
                  • Oct 2009
                  • 183

                  #9
                  Originally posted by nico5038
                  Hi NeoPa,

                  The Docmd.execute won't produce these annoying status messages, unless you add the option dbFailOnError like:
                  Code:
                  DoCmd.Execute("YourSQLString",dbFailOnError )
                  This is one of the reasons why I prefer this method.
                  The other is that a .SetWarnings(Fa lse) will also suppress a form save pop-up in design, even when out of debugging mode, that's the other reason for me to minimize the use of .SetWarnings.

                  Nic;o)
                  thank you for the very helpful information that I can gain from here
                  now I can differentiate between
                  Docmd.Execute
                  DoCmd.RunSQL

                  Docmd.Execute: doesn't return any warnings except with dbFailOnError
                  DoCmd.RunSQL: entails using the code that Neopa posted
                  in the case I want to get rid of the built-in Access warnings
                  thank you very much

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32661

                    #10
                    I didn't realise that Nico. Another good tip to learn from :)

                    Comment

                    Working...