'*定义一个连接
Dim Conn As ADODB.Connection
'*定义一个记录集
Dim mrc As ADODB.Recordset
'*分别实例化
Set Conn = New ADODB.Connection
set mrc =New ADODB.Recordset
'*定义一个连接字符串
dim ConnectString as string
ConnectString="provider=microsoft.jet.oledb.4.0data source=" &App.Path &"\data\数据库名.mdbjet oledb:database password=数据库密码"
'*打开连接
Conn1.Open ConnectString
'*定义游标位置
Conn1.CursorLocation = adUseClient
'*查询记录集(从student表中找出名子为"张三"的记录)
mrc.open "select * from student where name='张三'",Conn, adOpenKeyset, adLockOptimistic
'*现在你已经得到了你想要查询的记录集了,那就是mrc
'*你可以把此记录集与DataGrid榜定,用datagrid显示你查询的记录
set me.datagrid.datasource=mrc
可以快速导出使用excel 就有该功能
Public Function ExportToExcel(ByVal strOpen As String, Title As String, dizhi As String, con As ADODB.Connection)'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL'* 用法:ExporToExcel(strOpen查询字符串,titile
'*excel标题,dizhi 保存路径,con 数据库连接地址)
'*********************************************************
lok: On Error GoTo er
Screen.MousePointer = 11
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Long
Dim Icolcount As Long
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 = con
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strOpen
DoEvents
' Debug.Print strOpen
.Open
End With
Debug.Print strOpen
' Set Rs_Data = Open_rst_from_str(strOpen)
With Rs_Data
If .RecordCount < 1 Then
MsgBox ("没有记录!")
Screen.MousePointer = 0
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With
Set XlApp = CreateObject("Excel.Application")
Set xlbook = Nothing
Set xlSheet = Nothing
Set xlbook = XlApp.Workbooks().Add
Set xlSheet = xlbook.Worksheets("sheet1")
'添加查询语句,导入EXCEL数据
Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))
With xlQuery
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
End With
xlQuery.FieldNames = True '显示字段名
xlQuery.Refresh
Dim i As Integer, Zd As String
With xlSheet
For i = 1 To 6
Zd = .Range(.Cells(1, 1), .Cells(1, Icolcount)).item(1, i)
' .Range(.Cells(1, 1), .Cells(1, Icolcount)).Item(1, i) = Lm_YwToZw(Zd)
Next
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.name = "黑体"
'设标题为黑体字
' .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
' .Range(.Cells(Irowcount + 2, Icolcount)).Text = Zje
'设表格边框样式
End With
XlApp.Visible = True
XlApp.Application.Visible = True
' xlBook.SaveAs dizhi
Set XlApp = Nothing '"交还控制给Excel
Set xlbook = Nothing
Set xlSheet = Nothing
Screen.MousePointer = 0
Exit Function
er:
' Dispose_Err
MsgBox err.Description & " 从新导报表,请等待!"
GoTo lok:
End Function
使用这个模块就可以,你可以看看引用的函数即可
多条件混合模糊搜索"select * from 表名 where 字段名 Like'%" &text1.text &"%'and 字段名 like'%" &combo1.text &"%' and 字段名 like'%" &text2.text &"%'"
例子
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim cnstr As String
Private Sub Form_Load() '窗口打开时,连接数据库
conn.CursorLocation = adUseClient
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0Data Source= db1.mdbJet OLEDB:Database Password=" '修改成你的数据为地址/密码
conn.ConnectionString = cnstr
conn.Open cnstr
End Sub
Private Sub Command1_Click()
if rs.state=adstateopen then rs.close'记录集打开时则关闭记录集
strsql ="select * from 表名 where 字段名 Like'%" &text1.text &"%'and 字段名 like'%" &combo1.text &"%' and 字段名 like'%" &text2.text &"%'"
rs.Open strsql, conn, 3, 3
set DataGrid1.DataSource =rs
'这时适当调整一下datagird控件的格式(略)
End Sub
Private sub form_unload()
conn.close
end sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)