|
Henry D. Sy 写的ACCESS查询所得数据输入到EXCEL指定位置非常好用!
现碰到一个问题:Balance这个查询如果带有条件格式(例根据窗体上的某个控件查询所得)就会出错
(运行时错误'*****' 无效的SQL语句;期待****)
rs.Open strQueryName, CurrentProject.Connection
改成 rs.Open "select * from " & strQueryName, CurrentProject.Connection,1,1 也一样
该如何解决,望得到高手的帮助,谢谢
=============================
Private Sub Command0_Click()
QueryToExcel "Balance", "balxls", "bal" '实际使用命令格式
'Balance 是 你建的查询名称
'balxls 是 Excel工作表名称
'bal 是 Excel工作表balxls中的工作簿名称
End Sub
'---------------------------------------------------------------------------------------
' Procedure : QueryToExcel
' DateTime : 2008-12-2 00:02
' Author : Henry D. Sy
' Purpose :
'---------------------------------------------------------------------------------------
'
Sub QueryToExcel(ByVal strQueryName As String, ByVal xlsName As String, ByVal _
strShtName As String)
' Send the Query results to Excel
' for further analysis
Dim rs As ADODB.Recordset
Dim objXL As Excel.Application
Dim objWs As Excel.Workbook
Dim fld As ADODB.Field
Dim intCol As Integer
Dim intRow As Integer
Set rs = New ADODB.Recordset
' Get the desired data into a recordset
rs.Open strQueryName, CurrentProject.Connection
' Launch Excel
Set objXL = New Excel.Application
' Open a worksheet
Set objWs = objXL.Workbooks.Open(CurrentProject.Path & "\" & xlsName & _
".xls")
objWs.Worksheets(strShtName).Activate
' Copy the data
' First the field names 第一行是ACCESS中的列表名
For intCol = 0 To rs.Fields.Count - 1 '是获取表中的列数
Set fld = rs.Fields(intCol)
objWs.Worksheets(strShtName).Cells(1, intCol + 1) = fld.Name 'Excel中(1,1)开始输入列名
Next intCol
' Now the actual data 现在开始复制数据
intRow = 2 '你可以修改intRow从第几行开始输入数据,本例中是从第2行开始复制数据
Do Until rs.EOF '你可以修改intCol + 1从第几列开始输入数据,本例中是从第2行第1列开始复制数据
For intCol = 0 To rs.Fields.Count - 1
objWs.Worksheets(strShtName).Cells(intRow, intCol + 1) = _
rs.Fields(intCol).Value
Next intCol
rs.MoveNext
intRow = intRow + 1
Loop
' Make the worksheet visible
objXL.Visible = True '打开Excel表查看数据
rs.Close
Set rs = Nothing
End Sub |
|