Microsoft VBScript compilation error: Expected end of statement

前端 未结 3 1711
耶瑟儿~
耶瑟儿~ 2021-01-25 07:42

I am trying to insert some records into MS Access Table with the help of below VB Script. But when am trying to execute it, it\'s throwing Compilation error: Expected end of sta

3条回答
  •  半阙折子戏
    2021-01-25 08:35

    So lets break down the real reason why this code doesn't work.

    You copied and pasted Visual Basic for Applications(VBA) into a .VBS(Visual Basic Script) file and expected it to work, I assume.

    The problem with this is that VBA and VBScript are slightly different languages. Review the info section for both tags on stackoverflow when you get the opportunity.

    For now lets just patch your code and maintain your DAO object so you don't have to reconstruct your Database usage with ADODB.

    ExecuteInsert
    Sub ExecuteInsert()
        Dim dbs, DbFullName, acc
        Set acc = createobject("Access.Application")
    
        DbFullName = "D:\G\Diamond\FINAL MS-Access\MS-Access project.accdb"
        Set dbs = acc.DBEngine.OpenDatabase(DbFullName, False, False)
        dbs.Execute "INSERT INTO [2014_Status] ( Prompt, Project_Name, STATUS,Release_Name )SELECT     RoadMap.SPRF_CC, RoadMap.SPRF_Name, RoadMap.Project_Phase,RoadMap.Release_Name FROM RoadMap WHERE (((Exists (select 1 FROM [2014_Status] where RoadMap.SPRF_CC = [2014_Status].[Prompt]))=False));"
        dbs.Close
        msgbox "done"
    End Sub
    

    Changes made.

    1. Blocked your dim'd variables and removed As *** statements for vbscript compatibility
    2. Set an access object so you could maintain the remainder of your code.
    3. Added the acc.DBEngine. before OpenDatabase with additional parameters.
    4. Renamed your Sub from Form_Click to ExecuteInsert, then placed ExecuteInsert at the top of the code so that the vbscript activates the sub. If you just place a sub in a vbscript file, it will not necessarily run, you have to activate it directly.

    This code is tested and functions. Best of luck to you.

提交回复
热议问题