现在我有一个我在Access中运行的查询:
UPDATE [Part B Mater Table] SET [Part B Mater Table].MCCat = 1
WHERE ((([Part B Mater Table].fyear)=1990)) AND CUSIP IN
(SELECT TOP 33 PERCENT [Part B Mater Table].CUSIP
FROM [Part B Mater Table]
WHERE ((([Part B Mater Table].fyear)=1990))
ORDER BY [Part B Mater Table].MC DESC);
我需要能够循环从1990年到2012年的不同年份,并在最后将DESC更改为ASC,因此每年有一个ASC和一个DESC运行。
最好使用宏来执行此操作或使用VBA吗?
如果有帮助我有一个php脚本打印出我需要的所有Access SQL代码,那么我可以轻松修改它以显示VBA代码,如果可以在VBA中写出查询。
感谢任何帮助,谢谢。
答案 0 :(得分:2)
VBA:
Option Compare Database
Sub sqlCommand(iYear As Integer, sOrder)
Dim sSQL As String
sSQL = "UPDATE [Part B Mater Table] SET [Part B Mater Table].MCCat = 1 " & _
"WHERE ((([Part B Mater Table].fyear)=" & iYear & ")) AND CUSIP IN " & _
"(SELECT TOP 33 PERCENT [Part B Mater Table].CUSIP & " & _
"FROM [Part B Mater Table] " & _
"WHERE ((([Part B Mater Table].fyear) = " & iYear & ")) " & _
"ORDER BY [Part B Mater Table].MC " & sOrder & ")"
DoCmd.RunSQL sSQL
End Sub
Sub update()
Dim aOrder(2), sOrder As Variant, iCounter As Integer
Dim iYears As Integer
aOrder(0) = "ASC"
aOrder(1) = "DESC"
For Each sOrder In aOrder
For iCounter = 1990 To 2012
sqlCommand iCounter, sOrder
Next
Next sOrder
End Sub