+ Reply to Thread
Results 1 to 14 of 14

VB delete Access rows from table on condition

  1. #1
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    VB delete Access rows from table on condition

    I want to be able to delete rows from a an access query where a condition (date) is met within the table?

  2. #2
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    stuck

    im still stuck with this one guys any advise?

  3. #3
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    anyone

    anyone know if this can even be done?

  4. #4
    Registered User
    Join Date
    10-06-2006
    Posts
    82
    I don't understand what you are trying to do. Are you trying to do this within Access or Excel?

    Access forumn link
    http://www.access-programmers.co.uk/forums/

  5. #5
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    ah ha

    using excel vb code i would like to delete records from an access table based on a criteria

  6. #6
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Delete access db record from excel

    To do what you asked do so:
    1) open excel and then Vb editor (alt + F11)
    2) from the menu, choose tools, references and, from the list, choose Microsoft DAO 3.6 object librery
    if you don't have 3.6 you can use also 3.51 or another

    3) paste this code:
    Sub DeleteRecords()
    Dim db As Database
    Dim dbName As String
    Dim query As String

    dbName = "c:\<path>\<db name>.mdb"

    'connect to access database
    Set db = dbengine.Workspaces(0).OpenDatabase(dbName)
    query = "delete from <rable name> where <field name> = 'criteria'"

    if your field is numeric don't use quote in criteria:
    query = "delete from <rable name> where <field name> = criteria"

    'deleting
    db.Execute (query)
    db.Close
    Set db = Nothing

    End Sub

    Regards.
    Antonio

  7. #7
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    please help

    try this but i get the following debug error

    run time error 3131
    syntax error in FROM clause

    my database is named db1, the test table is called table and it contains three
    columns table, names, number

    from

    Sub DeleteRecords()
    Dim db As Database
    Dim dbName As String
    Dim query As String

    dbName = "C:\Documents and Settings\Home\My Documents\db1.mdb"

    'connect to access database
    Set db = dbengine.Workspaces(0).OpenDatabase(dbName)
    query = "delete from table where names = 'nick'"

    'if your field is numeric don't use quote in criteria:
    'query = "delete from <rable name> where <field name> = criteria"

    'deleting
    db.Execute (query)
    db.Close
    Set db = Nothing

    End Sub

  8. #8
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    deleting ms access table rows from excel

    Excuse me but I wrongly wrote:
    query = "delete from <rable name> where <field name> = 'criteria'"

    when the right was:
    query = "delete from <table name> where <field name> = 'criteria'"

    Here you have to change <table name> with your table name.

    For instance, if you need to delete rows from the table named 'Clients' where field named 'Name' is equal to 'Philip'

    you need to write:
    query = "delete from Clients where Name = 'Philip'"

    if the table name or the field name have blanks in the name use []:
    query = "delete from [Top Client] where [Main Name] = 'Philip'"

    Regards,
    Antonio

  9. #9
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    working

    i got ths working but how can i get this working if i want to rffer to a date in a variable

    works with text
    query = "delete from [Top Client] where [Main Name] = 'Philip'"

    want it to work like this
    mydate = 25/10/2005
    query = "delete from [Top Client] where [Main Name] = mydate"

  10. #10
    Forum Contributor
    Join Date
    10-08-2006
    Location
    Walnut, CA
    MS-Off Ver
    2003,2010, Office 365
    Posts
    114
    Hi,

    Use the following:
    query = "delete from [Top Client] where [Main Name] = #25/10/2005#"

  11. #11
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    cant

    thanks for the reply but it has to be from a variable as i pulling the date from a cell you see

  12. #12
    Forum Contributor
    Join Date
    10-08-2006
    Location
    Walnut, CA
    MS-Off Ver
    2003,2010, Office 365
    Posts
    114
    Hi,
    Sorry, should've read your question closely before posting.

    You can use
    query = "delete from [Top Client] where [Main Name] = #" & mydate &"#"
    or
    query = "delete from [Top Client] where [Main Name] = #" & Sheets(1).[a1] & "#"
    asssuming [a1] holds the value you want to use

  13. #13
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    For my experience, it's better to use format with ms access dates

    For instance:
    query = "delete from [Top Client] where format([birth day],""yyyymmdd"") = " & format(Sheets(1).range("a1"),"yyyymmdd")

    Regards,
    Antonio

  14. #14
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    all records in record set

    i got this working but it only removes the first records how can i get this to remove all records in the record set where the condition is equal to the date in question?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1