VBScript - FROM子句中的语法错误

时间:2014-06-01 21:07:57

标签: sql vbscript

所以我只想通过说我对VBS的经验很少来做序。但无论如何,我正在尝试编写这个脚本,我希望从存储多个项目的数据库中计算项目的年份数(但在某些情况下保持空白)。

我收到的错误是: 错误800A0C3B - From子句中的语法错误。 第26行 Char 1 来源:DAO.Database 我无法弄清楚如何解决它。

Option Explicit 'Line 1
'Declare Variables
Dim rs1
Dim rs2
Dim Year '5
Dim YearNo
Dim oAccess
Dim oShell
Dim dbLocation
Dim currentdb '10
Dim sql
Dim Program_Year
Dim Construction_Year
Dim oFSO
Set oFSO = CreateObject("scripting.fileSystemObject") '15
Set oShell = Wscript.CreateObject("Wscript.Shell")

dbLocation = "[drive]\folder\folder\file.accdb"

Set oAccess = createobject("Access.Application") '20

oAccess.opencurrentdatabase(dbLocation)

'sql = "SELECT * FROM [Expenditures] IN '" & dbLocation & "' "
'25
oAccess.currentdb.execute("SELECT * FROM [Expenditures] IN '" & dbLocation & "'""") '26

Set rs1 = CurrentDb.OpenRecordset(oAccess)

'Move to first record '30
rs1.MoveFirst
'Copy recordset
Set rs2 = rs1.Clone
rs2.Bookmark = rs1.Bookmark
rs2.MoveNext '35

Do While Not rs2.EOF

            rs2.Edit

'1. Check if Construction Year is before Program Year '40
If Me.Construction_Year < Me.Program_Year Then

'a) If year comes before program year, leave yearno blank
 If Me.Year < Me.Program_Year Then
     Me.YearNo = "" '45
 'b) If year is program year or is greater than program year, fill in yearno
 ElseIf Me.Year > Me.Program_Year or Me.Year = Me.Program_Year Then
     If rs1.Year < rs2.Year And Trim(rs1.GWP) = Trim(rs2.GWP) Then
         rs2.YearNo = rs2.Year - rs1.Year + rs1.YearNo
     End If '50
 End If

'2. Check if Program Year is Before Construction Year
ElseIf Me.Program_Year < Me.Construction_Year Then
'55
 'a) If year comes before construction year, leave yearno blank
 If Me.Year < Me.Construction_Year Then
     Me.YearNo =""
 'b) If year is construction year or is greater than construction year, fill in yearno
 ElseIf Me.Year > Me.Construction_Year or Me.Year = Me.Construction_Year Then '60
     If rs1.Year < rs2.Year And Trim(rs1.GWP) = Trim(rs2.GWP) Then
         rs2.YearNo = rs2.Year - rs1.Year + rs1.YearNo
     End If
 End If
'3. Check if Program Year and Construction Year are the same '65
ElseIf Me.ProgramYear = Me.Construction_Year Then
 'a) If year comes before program year, leave yearno blank
 If Me.Year < Me.Program_Year Then
     Me.YearNo = ""
 'b) Otherwise fill in yearno '70
 ElseIf rs1.Year < rs2.Year And Trim(rs1.GWP) = Trim(rs2.GWP) Then
         rs2.YearNo = rs2.Year - rs1.Year + rs1.YearNo
     End If
 End If
'80
'End If

'Update and move to next

            rs2.Update

rs1.MoveNext '85

            rs2.MoveNext

        Loop


        'Close recordsets and set to nothing

rs2.Close '90

        rs1.Close

        Set rs1 = Nothing

        Set rs2 = Nothing

'95
oShell.Popup "Update Complete!",2, "",0

oAccess.quit
Set oAccess = Nothing

WScript.Quit

任何帮助 - 我的代码中存在此问题和任何明显的问题 - 都将非常感激。

编辑:忘记添加数据库基于Access。

2 个答案:

答案 0 :(得分:1)

SQL?看起来像访问。这是来自一个非常古老的VBScript的代码。可能有所帮助:

' CursorTypeEnum Values
Const adOpenForwardOnly = 0
Const adOpenKeyset = 1
Const adOpenDynamic = 2
Const adOpenStatic = 3

' LockTypeEnum Values
Const adLockReadOnly = 1
Const adLockPessimistic = 2
Const adLockOptimistic = 3
Const adLockBatchOptimistic = 4

' CommandTypeEnum Values
Const adCmdUnknown = &H0008
Const adCmdText = &H0001
Const adCmdTable = &H0002
Const adCmdStoredProc = &H0004

Dim Connection, rs, item, svcOpsRA(1), datafile, n, fs
Set fs = CreateObject("Scripting.FileSystemObject")

Dim CurrentPath, CurrentFolder
set CurrentPath = fs.GetFolder(".")
CurrentFolder = CurrentPath.path
set CurrentPath = nothing

Set Connection = CreateObject("ADODB.Connection")
Connection.ConnectionTimeout = 30
Connection.CommandTimeout = 80
datafile = CurrentFolder & "\deploy.mdb"
Connection.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & datafile & ";DefaultDir=;UID=;PWD=;"
set rs = CreateObject("ADODB.RecordSet")

rs.Open "SELECT Service, State FROM svcOps WHERE Status", Connection, adOpenForwardOnly, adLockOptimistic
while not rs.EOF
    n = 0
    for each item in rs.Fields
        svcOpsRA(n) = item.value ' ... populating an array with the values
        n = n + 1
    next
    svcOps svcOpsRA(0), svcOpsRA(1) ' ... basically call a sub/function and pass 2 fields as parameters
    rs.MoveNext
wend
rs.Close

Connection.Close
set rs = nothing
set Connection = nothing
set fs = nothing

svcOps子看起来像:

sub svcOps(service,state) ' ... takes 2 parameters
    ' ... do something
end sub

另外,如果您打算更新记录集,请使用adOpenDynamic代替adOpenForwardOnly;)

这里是关于记录锁和枚举类型的文档:MSDN - Recordset Object Basics

答案 1 :(得分:1)

"SELECT * FROM [Expenditures] IN '" & dbLocation & "'"""

这是无效的语法。如果你想使用IN那么它可能是:

"SELECT * FROM [Expenditures] WHERE somefield IN('val1','val2','val3')"

如果值是变量,那么您仍然需要使用串联&amp;。

插入它们
"SELECT * FROM [Expenditures] WHERE somefield IN('" & val1 & "','" & val_etc & "')"

或者,

"SELECT * FROM [Expenditures] WHERE somefield = 'val1'"

如果val1是一个数字,那么您就不需要撇号。