|
Sub sjk()
Dim Conn As New ADODB.Connection
Dim rs As New Recordset
'Dim xx As Date
Application.ScreenUpdating = False
MsgBox "请先输入提取数据库数据的起止时间,规则是从某月" & (Chr(13)) & "1号起至某月最后一天止,如:2012-1-1至2012-5-31," & (Chr(13)) & "可以一次性提取多个月,不过速度会受到影响!", 48, "提示"
xx = InputBox("请输入查询日期(格式为:yyyy-mm-dd):")
yy = InputBox("请输入截止日期(格式为:yyyy-mm-dd):")
ConnStr = "Driver={SQL Server};DataBase=tk_center;Server=b;UID=sa;PWD="
Conn.Open ConnStr '连接数据库<br />
Sql = "select card_type,total_money,line_no,allot_time from dbo.[zy_finance_dcard_log] where allot_time >='" & Format(xx, "yyyy-mm-dd") & "' and allot_time <= '" & Format(yy, "yyyy-mm-dd") & "'"
rs.Open Sql, Conn, 3, 3
Set ws = ActiveWorkbook.Worksheets("数据库") '把sht指向当前工作簿的sheet1工作表
For iCols = 0 To rs.Fields.Count - 1
ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
ws.Range(ws.Cells(1, 1), ws.Cells(1, rs.Fields.Count)).Font.Bold = True
ws.Range("c2").CopyFromRecordset rs
rs.Close '关闭数据集
Set rs = Nothing '清空记录集释放内存
Conn.Close '关闭数据连接
Set Conn = Nothing '清空数据连接释放内存
Application.ScreenUpdating = True '恢复屏幕更新
End Sub
|
|