我在MSAccess 2010中创建了一个表单,在运行时出现以下错误: select语句包含拼写错误或缺失的保留字或参数名称,或标点符号不正确。
我的数据库名称是Accessexp
这是我的代码:
Private Sub SearchButton_Click()
Dim dbs As Database
Dim rs As Recordset
Dim qdf As QueryDef
Dim productName As String
Dim lastNQuarters As String
Dim strSql As String
Dim columns As String
Dim compstr As String
Dim curstr As String
Dim timestr As String
Dim tablestr As String
Dim sumstr As String
Dim varItem As Variant
Dim compcount As Integer
Dim colcount As Integer
Dim errorcount As Integer
Dim Monthvalue As String
Set dbs = CurrentDb()
errorcount = 0
colcount = 0
lastNQuarters = Me!quarterbox.Value
tablestr = ""
timestr = Me!Monthbox.Value
MsgBox (timestr)
tablestr = "Q" & lastNQuarters
For Each varItem In Me!columnlist.ItemsSelected
If Me!columnlist.ItemData(varItem) <> "---" Then
columns = columns & "," & Chr(34) & Me!columnlist.ItemData(varItem) & Chr(34)
colcount = colcount + 1
End If
Next varItem
compcount = 0
For Each varItem In Me!practicelist.ItemsSelected
compstr = compstr & "," & Chr(34) & Me!practicelist.ItemData(varItem) & Chr(34)
compcount = compcount + 1
Next varItem
If compcount = 0 Then MsgBox ("Warning: No Practice Selected"): errorcount = errorcount + 1
If colcount = 0 Then MsgBox ("Warning: No Practice Selected"): errorcount = errorcount + 1
If errorcount = 0 Then
lastNQuarters = Me!quarterbox.Value
strSql = "Select " & columns & ", " & tablestr & " from Accessexp where Accessexp.[Quarter] = " & tablestr & " and Accessexp.[Month] = " & timestr & ";"
MsgBox (strSql)
Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)
With dbs
Set qdf = .CreateQueryDef("QueryOutput", strSql)
DoCmd.OpenQuery "QueryOutput"
.QueryDefs.Delete "QueryOutput"
End With
dbs.Close
qdf.Close
End If
End Sub
我的查询是这样的:
Select ,"Revenue Blended","Revenue Direct" Q4
from Accessexp
where Accessexp.[Quarter] = Q4 and Accessexp.[Month] = April;
答案 0 :(得分:1)
还有其他方法可以用逗号分隔值而不是开始使用
使用前导逗号构建字符串后,只需使用
columns = Mid(columns, 2)
修剪第一个角色。
其他说明:
您生成的查询字符串应如下所示:
Select [Revenue Blended],[Revenue Direct]
from Accessexp
where Accessexp.[Quarter] = 'Q4' and Accessexp.[Month] = 'April';
答案 1 :(得分:0)
尝试此更新的代码。
Private Sub SearchButton_Click()
Dim dbs As Database, rs As Recordset
Dim qdf As QueryDef
Dim productName As String, lastNQuarters As String
Dim strSql As String, columns As String, compstr As String
Dim curstr As String, timestr As String, tablestr As String
Dim sumstr As String, varItem As Variant
Dim compcount As Integer, colcount As Integer, errorcount As Integer
Dim Monthvalue As String
Set dbs = CurrentDb()
errorcount = 0
colcount = 0
lastNQuarters = Me!quarterbox.Value
tablestr = ""
timestr = Me!Monthbox.Value
MsgBox timestr
tablestr = "Q" & lastNQuarters
For Each varItem In Me!columnlist.ItemsSelected
If Me!columnlist.ItemData(varItem) <> "---" Then
columns = columns & "[" & Me!columnlist.ItemData(varItem) & "],"
colcount = colcount + 1
End If
Next
compcount = 0
For Each varItem In Me!practicelist.ItemsSelected
compstr = compstr & "[" & Me!practicelist.ItemData(varItem) & "],"
compcount = compcount + 1
Next
If compcount = 0 Then
MsgBox ("Warning: No Practice Selected")
errorcount = errorcount + 1
Else
compstr = Left(compstr, Len(compstr)-1)
End If
If colcount = 0 Then
MsgBox ("Warning: No Practice Selected")
errorcount = errorcount + 1
Else
columns = Left(columns, Len(columns)-1)
End If
If errorcount = 0 Then
lastNQuarters = Me!quarterbox.Value
strSql = "SELECT " & columns & " FROM Accessexp WHERE Accessexp.[Quarter] = '" & tablestr & "'" & _
" AND Accessexp.[Month] = '" & timestr & "';"
MsgBox (strSql)
Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)
With dbs
Set qdf = .CreateQueryDef("QueryOutput", strSql)
DoCmd.OpenQuery "QueryOutput"
'.QueryDefs.Delete "QueryOutput"
End With
dbs.Close
qdf.Close
End If
End Sub