一般情况下,可以不使用中间变量。如写入LISTVIEW
Dim rsSP As New ADODB.Recordset
Dim xItem As ListItem
ListPlan.ListItems.Clear
On Error GoTo SPErr
If rsSP.State <> 0 Then rsSP.Close
rsSP.Open "select ...", oConn, adOpenStatic, adLockOptimistic
Do While Not rsSP.EOF
Set xItem = ListPlan.ListItems.Add() 'ListView名为ListPlan
xItem.Text = rsSP!Sono
xItem.Key = "c" & rsSP!adviceid
xItem.SubItems(1) = rsSP!Stuffing
xItem.SubItems(2) = rsSP!CntrType
xItem.SubItems(3) = rsSP!CName
xItem.SubItems(4) = rsSP!odate
xItem.SubItems(5) = rsSP!UserName
rsSP.MoveNext
Loop
rsSP.Close
Set rsSP = Nothing
Exit Sub
SPErr:
SBar.SimpleText = Err.Description
Err.Clear
Dim rsSP As New ADODB.Recordset
Dim xItem As ListItem
ListPlan.ListItems.Clear
On Error GoTo SPErr
If rsSP.State <> 0 Then rsSP.Close
rsSP.Open "select ...", oConn, adOpenStatic, adLockOptimistic
Do While Not rsSP.EOF
Set xItem = ListPlan.ListItems.Add() 'ListView名为ListPlan
xItem.Text = rsSP!Sono
xItem.Key = "c" & rsSP!adviceid
xItem.SubItems(1) = rsSP!Stuffing
xItem.SubItems(2) = rsSP!CntrType
xItem.SubItems(3) = rsSP!CName
xItem.SubItems(4) = rsSP!odate
xItem.SubItems(5) = rsSP!UserName
rsSP.MoveNext
Loop
rsSP.Close
Set rsSP = Nothing
Exit Sub
SPErr:
SBar.SimpleText = Err.Description
Err.Clear
txtName.text=rs!name & ""
其实速度快慢的关键还是在于你的RECORDSET的SQL语句,是否能尽量筛选出最少的记录,以使用rs.open的时间减少。一般不要使用select *,而且最好加上where条件来选择。
用MsFlexGrid、VsFlexGrid都可以直接绑定到记录集。在双击事件中判断当前行是哪条记录即可。不用一行一行添加到ListView中。
LVM_FIRST = &H1000 #If UNICODE Then
LVM_GETITEMTEXT = (LVM_FIRST + 115)
LVM_SETITEMTEXT = (LVM_FIRST + 116)
#Else
LVM_GETITEMTEXT = (LVM_FIRST + 45)
LVM_SETITEMTEXT = (LVM_FIRST + 46)
#End If
End EnumPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Sub ListView_SetItemText(hwndLV As Long, i As Long, iSubItem As Long, pszText As String)
Dim lvi As LVITEM
lvi.iSubItem = iSubItem
lvi.pszText = pszText
SendMessage hwndLV, LVM_SETITEMTEXT, ByVal i, lvi
End Sub' hwndLV是ListView的句柄,i为ListView的行,iSubItem为子元素序号,pszText为要填入的字符串。
在程序中引用ADO对象,使用代码方式+绑定方式进行数据浏览,及修改。
然后指定ADODC的SQL语句,如果要更新DATAGRID显示的内容,就改变ADODCD的SQL语句,再adodc.refresh就行了