2016-12-31 60 views
0

我试图迭代查询作为DAO.Recordset,我的问题是,我的记录集从不打印任何东西。如果我看看我的表格,并且我的查询都具有我之后的数据,但VBA不生成我期望的数据。下面是synatx - 为什么这不写我的数据?使用查询作为DAO.Recordset

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

'rsExportExcel'没有声明 - 你有错字吗?你也错过了'MoveNext',除非你想*无限循环 –

+0

@TimWilliams - 是的,我的错误应该是rs1 –

+0

做你的代码进入'While'循环吗? –

回答

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结果的直接窗口。

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 
相关问题