Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2012
    Location
    Blackstone, MA, USA
    Posts
    5

    Trouble deleting data in VBA based on criteria

    I've been stuck on one small item for awhile and it's driviing me nuts. In short, I want to run a delete query in VBA to delete records containing a billing month that's greater that a precalculated current month. The source data from another system has the dates in text format, shown as (YYYY/MM).

    Here's the statement:

    Code:
    mySQL = "DELETE * FROM [tblBillingData]" _
    & "WHERE (((tblBillingData.[Billing Month]) > #" & PreCalcCurrentMonth & "#, ));"
    DoCmd****nSQL mySQL

    When it runs, it deletes everything from the table, not just the items where the billing month is greater than the current month.

    All variables are declared properly and I can see the PreCalcCurrentMonth when I step through the codeAny suggestions?

    Should I be evaluating each recordset individually?

    Thanks,

    Ed

  2. #2
    Join Date
    Mar 2009
    Posts
    5,273
    The format of the data does not matter here. What's the data type of the column [Billing Month] and what's the Type of the variable PreCalcCurrentMonth?
    Have a nice day!

  3. #3
    Join Date
    Mar 2012
    Location
    Blackstone, MA, USA
    Posts
    5
    Hi, both [Billing Month] and PreCalcCurrentMonth data types are String

    If I do this is a regular MS Access query and use PreCalcCurrentMonth as a parameter which I key in at run time, it works.

  4. #4
    Join Date
    Mar 2009
    Posts
    5,273
    Then the criteria should be:
    Code:
    "WHERE (((tblBillingData.[Billing Month]) > '" & PreCalcCurrentMonth & "'" ));"
    The pound (or number) character (#) has a numeric code of 35, i.e. lower than any numeric or alphabetic character. In a string comparison, a string beginning with # will always be smaller than any alphanumeric string.
    Have a nice day!

  5. #5
    Join Date
    Mar 2012
    Location
    Blackstone, MA, USA
    Posts
    5
    Great, thanks! It worked successfully once, but I've got more testing to do. Btw, you had an extra " at the end after the single '. Just in case someone else should need this, Thanks again!

  6. #6
    Join Date
    Mar 2009
    Posts
    5,273
    You're welcome!
    Have a nice day!

Tags for this Thread

Posting Permissions

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