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

问题描述 投票:1回答:1

我创建了一个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

有什么帮助吗?谢谢。

vba access-vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.