我试图将查询迭代为DAO.Recordset
,我的问题是我的记录集从不打印任何内容。如果我查看我的表,我的查询都有我所追求的数据,但VBA没有产生我期望的数据。下面是synatx - 为什么这不会写我的数据?
Option Compare Database
Sub Test()
Dim query1 As String, rs1 As DAO.Recordset
Dim qryDef As QueryDef, strSQL As String
query1 = "qryPullData"
strSQL = "SELECT fl1 As [Field With Spaces One],fl2 As [Field With Spaces Two], " & _
"fl3 As [Field WIth Spaces Three], fl4 As [Field With Spaces Four] " & _
"FROM smallsubset ORDER BY fl1 ASC;"
Set qryDef = CurrentDb.CreateQueryDef(query1, strSQL)
Set rs1 = CurrentDb.OpenRecordset(query1)
If Not rs1.EOF Then
While Not rs1.EOF
Debug.Print rs1("Field With Spaces One")
Debug.Print rs1("Field With Spaces Two")
Debug.Print rs1("Field With Spaces Three")
Debug.Print rs1("Field With Spaces Four")
Debug.Print rs1("[Field With Spaces One]")
Debug.Print rs1("[Field With Spaces Two]")
Debug.Print rs1("[Field With Spaces Three]")
Debug.Print rs1("[Field With Spaces Four]")
Wend
rs1.Close
End If
End Sub
答案 0 :(得分:0)
以下是使用上述评论中的一些建议的代码:
Sub Test()
Dim query1 As String, rs1 As DAO.Recordset
Dim qryDef As QueryDef, strSQL As String
If CheckQuery("qryPullData") = "Yes" Then
DoCmd.DeleteObject acQuery, "qryPullData"
End If
query1 = "qryPullData"
strSQL = "SELECT fl1 As [Field With Spaces One],fl2 As [Field With Spaces Two], " & _
"fl3 As [Field WIth Spaces Three], fl4 As [Field With Spaces Four] " & _
"FROM smallsubset ORDER BY fl1 ASC;"
Set qryDef = CurrentDb.CreateQueryDef(query1, strSQL)
Set rs1 = CurrentDb.OpenRecordset(query1)
rs1.MoveFirst
While Not rs1.EOF
Debug.Print rs1("Field With Spaces One")
Debug.Print rs1("Field With Spaces Two")
Debug.Print rs1("Field With Spaces Three")
Debug.Print rs1("Field With Spaces Four")
rs1.MoveNext
Wend
rs1.Close
End Sub
这是从这里偷来的CheckQuery子:http://www.access-programmers.co.uk/forums/showthread.php?t=206298
Function CheckQuery(queryName As String)
Dim qryLoop As QueryDef
Dim dbs As Database
Dim exists As String
exists = "No"
For Each qryLoop In CurrentDb.QueryDefs
If qryLoop.Name = queryName Then
exists = "Yes"
Exit For
End If
Next
CheckQuery = exists
End Function
确保您正在查看Debug.Print结果的即时窗口。
答案 1 :(得分:0)
来自@tlemaster的代码稍微整洁一点。这样可以更好地格式化输出,而不是一个接一个地运行所有字段和记录,从CheckQuery
函数中删除不必要的变量并正确释放所有对象变量。
Public Sub Test()
Dim rs1 As DAO.Recordset
Dim qryDef As QueryDef
Dim query1 As String
Dim strSQL As String
Dim lngRecordNum As Long '(how many records are you expecting?)
query1 = "qryPullData"
If QueryExists(query1) Then
DoCmd.DeleteObject acQuery, query1
End If
strSQL = "SELECT fl1 As [Field With Spaces One], fl2 As [Field With Spaces Two], " & _
"fl3 As [Field WIth Spaces Three], fl4 As [Field With Spaces Four] " & _
"FROM smallsubset ORDER BY fl1 ASC;"
Set qryDef = CurrentDb.CreateQueryDef(query1, strSQL)
Set rs1 = CurrentDb.OpenRecordset(query1)
lngRecordNum = 1
Do While Not rs1.EOF
Debug.Print "Record " & lngRecordNum & ":"
Debug.Print " " & rs1("Field With Spaces One")
Debug.Print " " & rs1("Field With Spaces Two")
Debug.Print " " & rs1("Field With Spaces Three")
Debug.Print " " & rs1("Field With Spaces Four")
rs1.MoveNext
Loop
Set rs1 = Nothing
Set qryDef = Nothing
End Sub
Public Function QueryExists(queryName As String) As Boolean
Dim qryLoop As QueryDef
For Each qryLoop In CurrentDb.QueryDefs
If qryLoop.Name = queryName Then
QueryExists = True
Exit For
End If
Next
Set qryLoop = Nothing
End Function