Public Sub 查询全部字段前面若干条记录() Dim mydata As String, mytable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer ActiveSheet.Cells.Clear mydata = ThisWorkbook.Path & "\职工管理.mdb" '指定数据库 mytable = "职工基本信息" '指定数据表 Set cnn = New ADODB.Connection With cnn .Provider = "microsoft.jet.oledb.4.0" .Open mydata End With SQL = "select top 5 * from " & mytable _ & " where 年龄>40 order by 年龄 DESC" Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic '也可以使用下面的语句 ' Set rs = cnn.Execute(SQL) For i = 1 To rs.Fields.Count Cells(1, i) = rs.Fields(i - 1).Name Next i With Range(Cells(1, 1), Cells(1, rs.Fields.Count)) .Font.Bold = True .HorizontalAlignment = xlCenter End With Range("A2").CopyFromRecordset rs ActiveSheet.Cells.Font.Size = 10 ActiveSheet.Columns.AutoFit rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub Public Sub 查询部份字段前面若干条记录() Dim mydata As String, mytable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer ActiveSheet.Cells.Clear mydata = ThisWorkbook.Path & "\职工管理.mdb" mytable = "职工基本信息" Set cnn = New ADODB.Connection With cnn .Provider = "microsoft.jet.oledb.4.0" .Open mydata End With SQL = "select top 5 姓名,性别,所属部门,职务,职称,年龄,进本单位时间" _ & " from " & mytable _ & " where 年龄>40 order by 年龄 DESC" Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic '也可以使用下面的语句 ' Set rs = cnn.Execute(SQL) For i = 1 To rs.Fields.Count Cells(1, i) = rs.Fields(i - 1).Name Next i With Range(Cells(1, 1), Cells(1, rs.Fields.Count)) .Font.Bold = True .HorizontalAlignment = xlCenter End With Range("A2").CopyFromRecordset rs ActiveSheet.Cells.Font.Size = 10 ActiveSheet.Columns.AutoFit rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub Public Sub 查询不重复字段的内容() Dim mydata As String, mytable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer '清除工作表的全部数据 ActiveSheet.Cells.Clear '输入标题 With Range("A1:C1") .Value = Array("部门", "职称类别", "职务类别") .Font.Bold = True .HorizontalAlignment = xlCenter End With '指定数据库和数据表 mydata = ThisWorkbook.Path & "\职工管理.mdb" '指定数据库 mytable = "职工基本信息" '指定数据表 '建立与数据库的连接 Set cnn = New ADODB.Connection With cnn .Provider = "microsoft.jet.oledb.4.0" .Open mydata End With '查询数据表中不重复的部门名称,并复制数据 SQL = "select distinct 所属部门 from " & mytable Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic Range("A2").CopyFromRecordset rs '查询数据表中不重复的职称类别名称,并复制数据 SQL = "select distinct 职称 from " & mytable Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic Range("B2").CopyFromRecordset rs '查询数据表中不重复的职务名称,并复制数据 SQL = "select distinct 职务 from " & mytable Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic Range("C2").CopyFromRecordset rs '设置工作表格式 ActiveSheet.Cells.Font.Size = 10 ActiveSheet.Columns.AutoFit '关闭记录集及数据库连接,并释放变量 rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub Public Sub 利用Like运算符进行模糊查询() Dim mydata As String, mytable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer '清除工作表的全部数据 ActiveSheet.Cells.Clear '指定数据库和数据表 mydata = ThisWorkbook.Path & "\职工管理.mdb" '指定数据库 mytable = "职工基本信息" '指定数据表 '建立与数据库的连接 Set cnn = New ADODB.Connection With cnn .Provider = "microsoft.jet.oledb.4.0" .Open mydata End With '查询数据表 SQL = "select * from " & mytable & " where 姓名 like '李%'" Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic '也可以使用下面的语句 ' Set rs = cnn.Execute(SQL) '复制字段名 For i = 1 To rs.Fields.Count Cells(1, i) = rs.Fields(i - 1).Name Next i '设置字段名字体为加粗并居中对齐 With Range(Cells(1, 1), Cells(1, rs.Fields.Count)) .Font.Bold = True .HorizontalAlignment = xlCenter End With '复制全部数据 Range("A2").CopyFromRecordset rs '设置工作表格式 ActiveSheet.Cells.Font.Size = 10 ActiveSheet.Columns.AutoFit '关闭记录集及数据库连接,并释放变量 rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub Public Sub 查询某一区间的记录() Dim mydata As String, mytable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer '清除工作表的全部数据 ActiveSheet.Cells.Clear '指定数据库和数据表 mydata = ThisWorkbook.Path & "\职工管理.mdb" '指定数据库 mytable = "职工基本信息" '指定数据表 '建立与数据库的连接 Set cnn = New ADODB.Connection With cnn .Provider = "microsoft.jet.oledb.4.0" .Open mydata End With '查询数据表 SQL = "select * from " & mytable _ & " where 年龄 between 30 and 40 order by 年龄 DESC" Set rs = New ADODB.Recordset rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic '也可以使用下面的语句 ' Set rs = cnn.Execute(SQL) '复制字段名 For i = 1 To rs.Fields.Count Cells(1, i) = rs.Fields(i - 1).Name Next i '设置字段名字体为加粗并居中对齐 With Range(Cells(1, 1), Cells(1, rs.Fields.Count)) .Font.Bold = True .HorizontalAlignment = xlCenter End With '复制全部数据 Range("A2").CopyFromRecordset rs '设置工作表格式 ActiveSheet.Cells.Font.Size = 10 ActiveSheet.Columns.AutoFit '关闭记录集及数据库连接,并释放变量 rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub Public Sub 查询存在于某集合里面的记录() Dim mydata As String, mytable As String, SQL As String Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer ActiveSheet.Cells.Clear mydata = ThisWorkbook.Path & "\职工管理.mdb" mytable = "职工基本信息" Set cnn = New ADODB.Connection With cnn .Provider = "microsoft.jet.oledb.4.0" .Open mydata End With SQL = "select * from " & mytable _ & " where 年龄> 40 and 职称 in('工程师','经济师') order by 年龄 DESC" Set rs = cnn.Execute(SQL) For i = 1 To rs.Fields.Count Cells(1, i) = rs.Fields(i - 1).Name Next i With Range(Cells(1, 1), Cells(1, rs.Fields.Count)) .Font.Bold = True .HorizontalAlignment = xlCenter End With Range("A2").CopyFromRecordset rs ActiveSheet.Cells.Font.Size = 10 ActiveSheet.Columns.AutoFit rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub
|