我想在访问数据库中对多个字段进行排序,但作为新手,我从MSDN程序中获取参考,并首先尝试对单个字段进行排序。我收到错误来自以下几行的语法错误。
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM Banks_Trnx_2018-2019" _
& "ORDER BY SNO DESC;")
完整的计划如下。
Option Compare Database
Sub OrderByX()
Dim dbs As Database, rst As Recordset
' Modify this line to include the path to Northwind
' on your computer.
Set dbs = OpenDatabase("E:\FY_2018-2019\Banks\Banks_Trnx_2018-2019.accdb")
' Select the last name and first name values from
' the Employees table, and sort them in descending
' order.
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM Banks_Trnx_2018-2019" _
& "ORDER BY SNO DESC;")
' Populate the Recordset.
rst.MoveLast
' Call EnumFields to print recordset contents.
' EnumFields rst, 12
dbs.Close
End Sub
我很无能为力。请帮忙。如前所述,我想对包含文本和数字字段的三个字段(多个字段)进行排序,目前我可以通过按正确顺序设置列并在排序时一起选择它们来轻松完成。由于这是一个重复的操作,我正在尝试开发相同的VBA程序。任何指向正确的方向都应受到高度赞赏。
答案 0 :(得分:0)
你应该首先将你的sql设置为一个变量 - 让你更容易排除故障 - 你可以看到你在关键字和表之间留下了一些空格
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018-2019]" _
& "ORDER BY SNO DESC;")
应该是(根据@skkakkar建议的表格添加括号)
Set rst = dbs.OpenRecordset("SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018-2019] " _
& "ORDER BY SNO DESC;")
答案 1 :(得分:-1)
你的表名中有 - 。我甚至认为不允许这样做,要么在表名周围添加括号或更改表名。
"SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018-2019]" _
& "ORDER BY SNO DESC;"
或
"SELECT ACT_CD, " _
& "SNO FROM [Banks_Trnx_2018_2019] " _
& "ORDER BY SNO DESC;"
并更改表名。