点蓝字关注,回复“VBA”获取专业资料
《VBA代码解决方案》系列书作者
头条百家平台 VBA资深创作者
_______________________________
大家好,今天给继续讲解VBA数据库解决方案的第16讲:动态显示每一条Recordset对象记录的方法。在上一讲中我们讲了MoveFirst,MoveLast,MoveNext,MovePrevious的定位记录的位置,今日我们继续讲解这些方法的实际应用。
实例:仍是上讲的数据库中的数据表,我们要首先显示部门为“一厂”的第一条记录,然后是最后一条记录,还要显示一个依照员工号显示详细信息的需求。这就是有查询的了。该怎么写代码呢?
一 显示部门为“一厂”的第一条记录。代码:
Sub mynzRSJLJF()
Dim cnADO, rsADO As Object
Dim strPath, strSQL AsString
Dim i As Integer
Set cnADO =CreateObject("ADODB.Connection")
Set rsADO =CreateObject("ADODB.RecordSet")
strPath =ThisWorkbook.Path & "\mydata2.accdb"
cnADO.Open"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath
strSQL = "SELECT *FROM 员工信息 WHERE 部门='一厂'"
rsADO.Open strSQL, cnADO,1, 3
For i = 0 TorsADO.Fields.Count - 1
Sheets("Sheet1").Cells(1, i + 1) = rsADO.Fields(i).Name
Next i
rsADO.MoveFirst
Sheets("Sheet1").Rows("2:2").Select
Selection.ClearContents
For j = 0 TorsADO.Fields.Count - 1
Sheets("Sheet1").Cells(2, j + 1) = rsADO.Fields(j)
Next j
rsADO.Close
cnADO.Close
Set rsADO = Nothing
Set cnADO = Nothing
End Sub
代码解析:
1 Set cnADO =CreateObject("ADODB.Connection")
Set rsADO =CreateObject("ADODB.RecordSet")
strPath = ThisWorkbook.Path &"\mydata2.accdb"
cnADO.Open"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath
strSQL = "SELECT *FROM 员工信息 WHERE 部门='一厂'"
rsADO.Open strSQL, cnADO, 1, 3
上述代码创建了ADO对象和RecordSet对象,并打开了ADO连接,通过SQL语句,设置了打开的记录集为部门为一厂的记录集。
2 For i= 0 To rsADO.Fields.Count - 1
Sheets("Sheet1").Cells(1, i + 1) = rsADO.Fields(i).Name
Next i
上述代码的意义是计入表头
3 rsADO.MoveFirst
Sheets("Sheet1").Rows("2:2").Select
Selection.ClearContents
将记录集指针移动到第一条记录,在工作表中情况待填数据的区域
4 For j = 0 TorsADO.Fields.Count - 1
Sheets("Sheet1").Cells(2, j + 1) = rsADO.Fields(j)
Next j
精确的记入记录
5 rsADO.Close
cnADO.Close
Set rsADO = Nothing
Set cnADO = Nothing
关闭连接,释放内存。下面看代码截图:
运行结果:
二 显示为最后一条记录:代码:
Sub mynzRSJLJE()
DimcnADO, rsADO As Object
Dim strPath, strSQL As String
Dim i As Integer
Set cnADO = CreateObject("ADODB.Connection")
Set rsADO = CreateObject("ADODB.RecordSet")
strPath = ThisWorkbook.Path & "\mydata2.accdb"
cnADO.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="& strPath
strSQL = "SELECT * FROM 员工信息 WHERE 部门='一厂'"
rsADO.Open strSQL, cnADO, 1, 3
For i = 0 To rsADO.Fields.Count - 1
Sheets("Sheet1").Cells(1, i + 1) = rsADO.Fields(i).Name
Next i
rsADO.MoveLast
Sheets("Sheet1").Rows("2:2").Select
Selection.ClearContents
For j = 0 To rsADO.Fields.Count - 1
Sheets("Sheet1").Cells(2, j + 1) = rsADO.Fields(j)
Next j
rsADO.Close
cnADO.Close
Set rsADO = Nothing
Set cnADO = Nothing
End Sub
三:下面的代码显示指定员工号码的记录:
Sub mynzRSJLJFIND() '
Dim cnADO, rsADO As Object
Dim strPath, strSQL As String
Dim i As Integer
Set cnADO = CreateObject("ADODB.Connection")
Set rsADO = CreateObject("ADODB.RecordSet")
strPath = ThisWorkbook.Path & "\mydata2.accdb"
cnADO.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="& strPath
strSQL = "SELECT * FROM 员工信息 WHERE 部门='一厂'"
rsADO.Open strSQL, cnADO, 1, 3
For i = 0 To rsADO.Fields.Count - 1
Sheets("Sheet1").Cells(1, i + 1) = rsADO.Fields(i).Name
Next i
For i = 1 To rsADO.RecordCount
If Sheets("Sheet1").Cells(2, 1) "" AndrsADO.Fields(0) = Sheets("Sheet1").Cells(2, 1) Then
For j = 1 To rsADO.Fields.Count - 1
Sheets("Sheet1").Cells(2, j + 1) = rsADO.Fields(j)
Next j
End If
rsADO.MoveNext
Next i
rsADO.Close
cnADO.Close
Set rsADO = Nothing
Set cnADO = Nothing
End Sub
代码截图:
上述三个代码基本是相同的,只是个别的语句有不同,通过代码的精确控制可以实现每个记录的控制和显示,读者在学习的时候要多加利用。
今日内容回向:
1 如何显示记录集的第一条记录?
2 如何显示记录的最后一条记录?
3 如何显示指定的记录?
_____________________________
觉得有启发,点个“在看”,转给朋友们
欢迎你“留言”,和作者直接交流
更多关联阅读
公众号与作者联系方式
|
|