有网友问到关于排序的问题,随机生成一组数字,然后按升序降序排序。
排序我们一般是用选择排序和冒泡排序等,这里我们说一下冒泡法
1、先把倒数第一个数冒上去,直到上面没有比它大的数才算结束第一轮冒泡。
2、然后把新的最后两个数对比,继续冒上去。如此多次执行(原则上是有几个数据就得冒泡几次),最后达到冒泡排序的效果。
Access冒泡法排序动图
Access随机生成100以内的正整数:
' Randomize
Dim i As Long
Dim L As Long
With Me.LB
For i = 0 To 99
Randomize
L = Int((99 * Rnd) + 1)
.AddItem L
Next i
End With
Access按升序的VBA代码:
Dim LS As Long
LS = Me.LB.ListCount
LS = LS - 1
Dim ss() As String
ReDim ss(LS)
Dim i As Long
With Me.LB
For i = 0 To LS
ss(i) = .Column(0, i)
Next i
End With
Call StrAZ(ss())
Me.LB.RowSource = ""
With Me.LB
For i = 0 To LS
.AddItem ss(i)
Next i
End With
Access按降序的VBA代码:
Dim LS As Long
LS = Me.LB.ListCount
LS = LS - 1
Dim ss() As String
ReDim ss(LS)
Dim i As Long
With Me.LB
For i = 0 To LS
ss(i) = .Column(0, i)
Next i
End With
Call StrZA(ss())
Me.LB.RowSource = ""
With Me.LB
For i = 0 To LS
.AddItem ss(i)
Next i
End With
如想更快的系统地学习Access数据库,请点右上角 关注 部落 点击加入群:access学习群 与更多Access大牛交流 如果您觉得这篇技巧真的有用,请关注我们。更重要手机转发分享一下!
如喜欢此技巧,手机右上角点开,分享到QQ空间,方便自己以后看