使用ADODC控件绑定数据源
以下是数据输出按钮代码:
Dim txt1 As String
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim myexcel As New Excel.Application
Dim mybook As New Excel.Workbook
Dim mysheet As New Excel.Worksheet
If Check1.Value = True Then
txt1 = "select 日期 from inothermany "
Adodc1.RecordSource = txt1
Adodc1.Refresh
k = Adodc1.Recordset.Fields.count
l = Adodc1.Recordset.RecordCount + 1
Else
If Check1.Value = True And Check2.Value = True Then
txt1 = "select 日期,收入金额 from inothermany "
Adodc1.RecordSource = txt1
Adodc1.Refresh
k = Adodc1.Recordset.Fields.count
l = Adodc1.Recordset.RecordCount + 1
Else
If Check1.Value = True And Check2.Value = True And Check3.Value = True Then
txt1 = "select 日期,收入金额,来源方式 from inothermany"
Adodc1.RecordSource = txt1
Adodc1.Refresh
k = Adodc1.Recordset.Fields.count
l = Adodc1.Recordset.RecordCount + 1
Else
If Check1.Value = True And Check2.Value = True And Check3.Value = True And Check4.Value = True Then
txt1 = "select 日期,收入金额,来源方式,备注 from inothermany"
Adodc1.RecordSource = txt1
Adodc1.Refresh
k = Adodc1.Recordset.Fields.count
l = Adodc1.Recordset.RecordCount + 1
End If
End If
End If
End If
If l > 1 Then
Set mybook = myexcel.Workbooks.Add '添加一个新的BOOK
Set mysheet = mybook.Worksheets(1) 'worksheets(1),意思是直接输出到工作表1中
For j = 1 To k
mysheet.Cells(1, j) = Adodc1.Recordset.Fields.Item(j - 1).Name
Next j
For i = 2 To l
For j = 1 To Adodc1.Recordset.Fields.count
If Not Adodc1.Recordset.EOF Then
On Error GoTo err02
mysheet.Cells(i, j) = Adodc1.Recordset.Fields.Item(j - 1).Value
If (i * j) Mod 500 = 0 Then
DoEvents
End If
Else
Exit Sub
End If
Next j
Adodc1.Recordset.MoveNext
Next i
myexcel.Visible = True
mybook.SaveAs ("其它收入数据") '保存文件 并输出文件名为many
'Set myexcel = Nothing '交还控制给EXCEL
'Set mybook = Nothing
'Set mysheet = Nothing
'mybook.Close
'myexcel.Quit
Else
MsgBox "查询不到记录!", vbOKOnly, "友情提示"
Exit Sub
End If
err02:
Exit Sub