Results 1 to 8 of 8
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038

    Syntax error: strSQL_Drop1 = "DELETE " & strCriteria & ".* FROM " & strCriteria & ";"

    Hello:

    I currently using the following to drop a table before re-importing the XLS.
    Code:
            Dim strSQL_Drop1 As String
            strSQL_Drop1 = "DELETE tblSource1.* FROM tblSource1;"
            CurrentDb.Execute strSQL_Drop1
    In this example, the table name "tblSource1" is referenced two (2) times in my strSQL_Drop1 (2nd line). Thus, in case the table name changes, I need to update it in 2 places.

    Instead, I'd like to use generic strCriteria statement. At the present time, however, I'm getting a syntax error... I'm sure it's missing a double-quote somewhere (or has one too many ").



    The proposed code looks like this:
    Code:
            Dim strCriteria As String
            strCriteria = tblSource1
            strSQL_Drop1 = "DELETE " & strCriteria & ".* FROM " & strCriteria & ";"        
            CurrentDb.Execute strSQL_Drop1
    Again, I currently encountering a run-time error '3075' ("Syntax error (missing operator) in query expression '.*'.

    What's the correct syntax for making this change?

    Thank you,
    Tom
    Attached Thumbnails Attached Thumbnails SyntaxError.JPG  

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    why not just use a query? It wont get the syntax wrong.
    then its: docmd.openquery "qdDelQry"

    why not:
    strSQL_Drop1 = "DELETE * FROM tblSource"




    you need quotes around the tablename
    strCriteria = "tblSource1"

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Never mind... I solved it. Problem was NOT w/ the SQL statement but the DIM of tblSource1.

    I changed it from
    strCriteria1 = tblSource1
    to
    strCriteria1 = "tblSource1"
    ... and it worked.

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You do know you can remove the first table reference?

    strSQL = "Delete * FROM tb_MyTable"
    db.Execute strSQL
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,724
    The DROP TABLE statement is used to drop an existing table in a database - Data structure and the data.

    The DELETE statement is used to delete existing records in a table.
    DELETE FROM table_name WHERE condition;

  6. #6
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Copy all -- I ended up a different process. See solution below. Expert Welshgasman assisted w/ the solution.

    Code:
    Sub DeleteRecordsFromTables()
    Dim tdf As TableDef
    Dim strSQL As String
    Dim db As DAO.Database
    Set db = CurrentDb
    For Each tdf In db.TableDefs
        If Left(tdf.Name, 3) = "tbl" Then
            strSQL = "DELETE * FROM " & tdf.Name
            Debug.Print strSQL
            db.Execute strSQL
        End If
    Next
    
    End Sub

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Just as a heads up if you ever move this to SQL server you can use the Truncate command to delete the entire contents of the table but leaving its structure intact.

    TRUNCATETABLE tb_flibble

    The beauty of this is that if you are deleting tonnes of rows in SQL using DELETE * ... SQL keeps recording a transaction log of each row being deleted.
    If you have 10000 rows this takes a while (figuratively speaking).

    Truncate does away with this, it literally removes all the records in one fell swoop, really quickly.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Thank you, Minty... that's good feedback. Appreciate your input/recommendations.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need help with "strCriteria" and HAVING clause
    By skydivetom in forum Programming
    Replies: 23
    Last Post: 08-19-2019, 06:27 PM
  2. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  3. Replies: 3
    Last Post: 06-06-2018, 08:26 PM
  4. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  5. "Group By" causes "ODBC--Call Failed" error
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-09-2011, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums