rs记录集之调用

来源:百度知道 编辑:UC知道 时间:2024/06/21 02:34:04
Private Sub cmdquery_Click()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.ConnectionString = "provider=sqloledb.1;persist security info=false;user id=sa;password=;initial catalog=JW;data source=microsof-0B5555"
conn.Open
rs.CursorLocation = adUseClient
rs.Open "select * from registrecords where registor='" & Trim(txtemployeeid.Text) & "' and registtime>'" & dtpfrom.Value & "' and '" & DateAdd(Day, -1, registtime) & "'<'" & dtpto.Value & "'", conn, 1, 1
Set DataGrid1.DataSource = rs
Set conn = Nothing
Set rs = Nothing
End Sub
本人想在另一过程中将查询结果转为excel电子表格,rs和conn应如何定义?或datagrid1.datasource可以在另一过程被调用吗?

Public Function ExporToExcel(strOpen As String)
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'*********************************************************
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Integer
Dim Icolcount As Integer

Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQuery As Excel.QueryTable

With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = Conn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strOpen
.Open
End With
With Rs_Data
If .RecordCount < 1 Then