Sub CX() '最单的VBA创建查询例子 Dim strSQL As String Dim qdf As QueryDef '删除TEMP查询 For Each Query1 In CurrentDb.QueryDefs If Query1.Name = "TEMP" Then CurrentDb.QueryDefs.Delete Query1.Name Exit For End If Next Query1 strSQL = "Select * from ZZ1" 'tblaa为表 Set qdf = CurrentDb.CreateQueryDef("TEMP", strSQL) '创建查询TEMP DoCmd.OpenQuery qdf.Name '执行查询 End Sub
Dim db As DAO.Database Dim qry As DAO.QueryDef Set db = CurrentDb Set qry = db.QueryDefs("TMP") SQL = "Select myvagn([08期中],[08期末],[09期中]) AS X总分X,学号, 性别, 姓名, 班级,[08期中],[08期末],[09期中]" SQL = SQL + " FROM " & Me.Combo0 & "摸底 orDER BY myvagn([08期中],[08期末],[09期中]) DESC;" qry.SQL = SQL DoCmd.OpenQuery "TMP", acNormal, acEdit '运行查询 End Sub