0

I am trying to match excel files which is placed in a folder with tables which exist in access database (with the same name as my excel files) and trying to import data which is in excel to access.

Sub import()

Dim blnHasFieldNames As Boolean
Dim strWorksheet As String, strTable As String
Dim strPath As String, strPathFile As String

blnHasFieldNames = True


strPath = "D:\PersonalData\working_table\"

strWorksheet = "Sheet1"

' Import the data from each workbook file in the folder
strFile = Dir(strPath & "*.xlsx")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      strTable = Left(strFile, InStrRev(strFile, ".xlsx") - 1)

      DoCmd.TransferSpreadsheet acImport, _
            acSpreadsheetTypeExcel9, strTable, strPathFile, _
            blnHasFieldNames, strWorksheet & "$"

      DoCmd.RunSQL ("DELETE * FROM " & strTable & " WHERE SPEC_ID = 'Specification ID'")



      strFile = Dir()
Loop



End Sub

I am successfully able to get the data into my access database tables, however my requirement is to delete the row from all such tables where SPEC_ID = 'Specification ID' . I am getting an error in line:

DoCmd.RunSQL ("DELETE * FROM " & strTable & " WHERE SPEC_ID = 'Specification ID'")

which states:

Run-time error: '3131' Syntax error in FROM clause.

Kindly guide me what I may have been doing wrong.

Thanks in advance.

8
  • 1
    What is the value of strTable ? Commented Sep 5, 2018 at 7:51
  • 2
    does your database table name contain any spaces? if yes than add your database name in [ strTable ] (Square bracket). From where you get the Specification ID ? I cannot find any integer variable here .. Commented Sep 5, 2018 at 7:53
  • @VincentG : strTable is same as strFile except the .xlsx extension. Commented Sep 5, 2018 at 8:33
  • 1
    There are many reasons why an unbracketed table name might be invalid becides spaces. Just bracket it, like Tarun P said, that should avoid most (but not all) possible invalid table names. Commented Sep 5, 2018 at 8:54
  • 3
    try this DoCmd.RunSQL ("DELETE * FROM [" & strTable & "] WHERE SPEC_ID = 'Specification ID'") Commented Sep 5, 2018 at 8:57

2 Answers 2

2

Using DoCmd.SetWarnings is a recipe for trouble. What if there is an error in the sql and your code exits without setting the warnings back to true? All sorts of weird stuff happens. Instead use the simpler and just as easy

CurrentDb.Execute "DELETE * FROM [" & strTable & "] WHERE SPEC_ID = 'Specification ID'"

You'll still need to check that strTable doesn't have [ or ] in the name (otherwise the bracketing won't be effective) and you'll need to either remove them or escape them (depending on how your actual table is named).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Brad. Would it not give me a pop-up before executing the delete in each table since there are almost 30 tables on which I am executing this on. Sorry but it's late night here and I am just being lazy in asking this question. ;) I have written a Python script post which I am calling this script so I am sure that no other table other than these would contain multiple rows containing 'Specification ID'
Using CurrentDb.Execute does not show a popup when inserting/deleting/updating data. Second advantage: you can also call CurrentDb.RecordsAffected immediately after to check how many records were deleted. It seems like you know there will only be one but you can use this to confirm.
You're calling this from Python?
Wow that's cool if you can check logs with a command. Thanks a lot. I will check if this runs tomorrow early morning and let you know. :) Thanks a lot for taking your time out to reply. I don't use vba much I just search and edit whatever I find. Thanks for letting me know this I have a clearer concept. Yep I am executing this after I have some data from Python. Not exactly integrating it. However you can call a vba script via Python too. :)
Hi @Brad I tested this and its working fine. :) Thanks a lot.
0

do like this

Docmd.SetWarnings False

DoCmd.RunSQL ("DELETE * FROM [" & strTable & "] WHERE SPEC_ID = 'Specification ID'")

Docmd.SetWarnings True

2 Comments

Thanks a lot. God bless you. :)
the answer provided below is more safe to use , just incase any user wants to copy paste this code. Hence changing the accepted answer to the below one. But thanks for your help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.