Results 1 to 5 of 5

Thread: SQL Delete question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    SQL Delete question

    The first SQL code below is in an existing db's queries. What I'm wondering is why [Main].[Hospital] is in there 3 times? Wouldn't once be sufficient? The full table with all hospitals (3 of them) gets imported into a new db, then all data except for 1 get deleted. Wouldn't the second code do the same thing?
    VB Code:
    1. DELETE [Main].[Hospital] AS Expr1, [Main].[Hospital] AS Expr2, [Main].[Hospital] AS Expr3
    2. FROM Main
    3. WHERE ((([Main].[Hospital])<>[Which Hospital?]));
    VB Code:
    1. DELETE Hospital as Expr1 FROM Main WHERE Hospital <> [Which Hospital?]

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Not quite. The line WHERE ((([Main].[Hospital])<>[Which
    Hospital?])); will prompt the user to enter the hospital name not
    to delete three times because there are three alias', I believe,
    and the fourth will be deleted since it was not entered. [Which
    Hospital?] is an unknown field so Access (I assume) will prompt
    you for the value of that field to use.

    To optimize this query test...
    Code:
    DELETE Hospital FROM Main WHERE Hospital = [Which Hospital to DELETE?]
    This is reverse logic. Instead of
    asking which tables not to delete, try deleting only one which the
    user prompt will provide. Then import the remaining three tables
    into the new database.

    Try this out on a copy or test database and verify if it produces
    the desired effect before using any code modifications on a live
    production database.

    HTH.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    First, I've _never_ gotten a delete to work with fields listed.... but then Access is weird....

    Code:
    DELETE FROM Main WHERE Hospital = [Hospital to delete?]
    Should be sufficient.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Oops, I included Hospital in the DELETE list by mistake.
    TG is right, should be left empty or Main.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Originally posted by RobDog888
    Not quite. The line WHERE ((([Main].[Hospital])<>[Which
    Hospital?])); will prompt the user to enter the hospital name not
    to delete three times because there are three alias', I believe,
    and the fourth will be deleted since it was not entered. [Which
    Hospital?] is an unknown field so Access (I assume) will prompt
    you for the value of that field to use.

    Yes, Access currently prompts for the parameter, but only does so once. I want to put this in code instead of using a built in query, using an input box to get the hospital to keep. Something like
    VB Code:
    1. strHospital = InputBox("Enter Hospital")
    2. strSQL = "DELETE Hospital FROM Main WHERE Hospital <> '" & strHospital & "'"
    Now that I think about it, maybe it shoud be
    VB Code:
    1. DELETE * FROM Main WHERE Hospital <> strHospital
    since I don't want any of the data from the other two hopitals in the copied db.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width