如何从一个记录移动到另一个记录

时间:2012-05-23 11:21:59

标签: vba access-vba

我创建了一个onclick函数,它应该运行一个SQL语句,从语句中选择URL并从URL顺序打开Excel文件。

它可以工作,但如果查询返回3个结果,那么它会打开相同的excel文件3次。

不是打开另一个文件而是打开同一个文件。

这就是我所拥有的:


    Dim strSQL As String
    Dim rs As DAO.Recordset
    Dim db As Database
    Dim appexcel As Object
    Set db = CurrentDb()
    strSQL = "SELECT * from [PROCESSED_CONTRACTS_PRINTALL]"
    Set rs = db.OpenRecordset(strSQL)
    urlval = rs!url
    rs.MoveFirst
    Do Until rs.EOF
    Set appexcel = CreateObject("Excel.Application")
    appexcel.workbooks.Open urlval
    appexcel.Visible = True
    rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    End Sub

请帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:1)

这个怎么样,请注意urlval现在在循环中,而Excel在循环之外。

Dim strSQL As String
Dim rs As DAO.Recordset
Dim db As Database
Dim appexcel As Object
Set db = CurrentDb()
strSQL = "SELECT * from [PROCESSED_CONTRACTS_PRINTALL]"
Set appexcel = CreateObject("Excel.Application")
appexcel.Visible = True
Set rs = db.OpenRecordset(strSQL)
rs.MoveFirst
Do Until rs.EOF
  urlval = rs!url
  appexcel.workbooks.Open urlval
  rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub