这段代码有什么问题?无法在VB中导出Access数据表到Excel,都是2007版
PrivateSubcmdout_Click()DimrstAsNewADODB.Recordsetrst.Open"select*fromcombo1.Text",cn...
Private Sub cmdout_Click()
Dim rst As New ADODB.Recordset
rst.Open "select * from combo1.Text", cn1, adOpenDynamic, adLockOptimistic
'Left(Trim(Text1.Text), Len(Trim(Text1.Text)) - 1)
Dim xlsApp As Excel.Application '定义Excel程序
Dim xlsBook As Excel.Workbook '定义工作薄
Dim xlsSheet As Excel.Worksheet '定义工作表
Dim i, j As Long
Set xlsApp = CreateObject("Excel.Application")
'创建Excel应用程序
Set xlsBook = xlsApp.Workbooks.Add '创建工作薄
Set xlsSheet = xlsBook.Worksheets(1) '创建工作表
On Error Resume Next
j = 1
Do Until rst.EOF
For i = 1 To rst.Fields.Count
xlsSheet.Cells(j, i) = rst.Fields(i - 1)
'写入记录集(不包括表头)
Next i
rst.MoveNext
j = j + 1
Loop
xlsApp.Visible = True '显示电子表格
xlsBook.SaveAs App.Path & "\导出数据.xlsx"
Set xlsApp = Nothing '交换控制权给Excel
rst.Close
cn1.Close
Set rst = Nothing
Set cn1 = Nothing
Unload Me
Unload fm
End Sub
报错是这句
rst.Open "select * from combo1.Text", cn1, adOpenDynamic, adLockOptimistic 展开
Dim rst As New ADODB.Recordset
rst.Open "select * from combo1.Text", cn1, adOpenDynamic, adLockOptimistic
'Left(Trim(Text1.Text), Len(Trim(Text1.Text)) - 1)
Dim xlsApp As Excel.Application '定义Excel程序
Dim xlsBook As Excel.Workbook '定义工作薄
Dim xlsSheet As Excel.Worksheet '定义工作表
Dim i, j As Long
Set xlsApp = CreateObject("Excel.Application")
'创建Excel应用程序
Set xlsBook = xlsApp.Workbooks.Add '创建工作薄
Set xlsSheet = xlsBook.Worksheets(1) '创建工作表
On Error Resume Next
j = 1
Do Until rst.EOF
For i = 1 To rst.Fields.Count
xlsSheet.Cells(j, i) = rst.Fields(i - 1)
'写入记录集(不包括表头)
Next i
rst.MoveNext
j = j + 1
Loop
xlsApp.Visible = True '显示电子表格
xlsBook.SaveAs App.Path & "\导出数据.xlsx"
Set xlsApp = Nothing '交换控制权给Excel
rst.Close
cn1.Close
Set rst = Nothing
Set cn1 = Nothing
Unload Me
Unload fm
End Sub
报错是这句
rst.Open "select * from combo1.Text", cn1, adOpenDynamic, adLockOptimistic 展开
展开全部
首先rst.Open "select * from combo1.Text", cn1, adOpenDynamic, adLockOptimistic 应改成:rst.Open "select * from " & combo1.Text, cn1, adOpenDynamic, adLockOptimistic ,并且combo1.Text需要是表名
其次 cn1定义语句贴出来看一下
其次 cn1定义语句贴出来看一下
追答
电脑上装了excel没有,qq270581704
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询