當資料庫內容逐漸龐大後,使用全部檢視的方式是非常不明智的,通常資料量大的時候,會改用分頁方式檢視,由於Access SQL語法中沒有SQL Server或其他資料庫軟體有分頁的語法可用,因此操作上會變成較為手動,這裡之前我也嘗試撰寫了SQL語句產生器,以便能快速產生語法,然後套用於清單方塊的RowSource中檢視,對於有大量資料需要進行切頁檢視者,會是個不錯的選擇。
主程式如下:
Function CreateViewByPageSQL(strTable As String, _
iTop As Double, _
iPageNumber As Double, _
Optional strColumns As String = "*", _
Optional strWHERE As String = "", _
Optional strOrderBy As String = "", _
Optional strOrderBy2 As String = "", _
Optional bnShowInfo As Boolean = False) As String
'產生切頁方式的SQL語法
'2016 Andy Chiu @ NHIV
'strTable 資料表名稱
'iTop 每頁上限
'iPageNumber 第幾頁
'strColumns 需要什麼欄位
'strWHERE 條件
'strOrderBy 排序方式
'strOrderBy2 後段排序方式
'bnShowInfo 是否由Debug.Print顯示訊息
'strTop 顯示幾筆
'strTop2 後段由幾筆切
'TotRows 資料總量
'資料總量
TotRows = DCount("*", strTable, strWHERE)
'總頁數
iPages = TotRows / iTop
a = Int(iPages)
If a <> iPages Then iPages = a + 1 Else iPages = iPages
'檢查頁數是否超過
If iPageNumber > iPages Then
Debug.Print "頁數超過!"
CreateViewByPageSQL = ""
Exit Function
End If
If strOrderBy <> "" Then
'如果strOrderBy2沒資料,產生反向排序
If strOrderBy2 = "" Then
If UCase(Right(strOrderBy, 5)) = " DESC" Then
strOrderBy2 = Mid(strOrderBy, 1, Len(strOrderBy) - 5)
Else
strOrderBy2 = strOrderBy & " DESC"
End If
End If
strOrderBy1A = "ORDER BY " & strOrderBy
Else
strOrderBy2A = ""
End If
If strOrderBy2 <> "" Then
strOrderBy2A = "ORDER BY " & strOrderBy2
End If
If strWHERE <> "" Then
strWHERE = "WHERE " & strWHERE
End If
strTop = "TOP " & iTop
iTop2 = TotRows - ((iPageNumber - 1) * iTop)
strTop2 = "TOP " & iTop2
strSQL = "SELECT " & strTop & " * FROM (" & vbCrLf & _
" SELECT " & strColumns & " FROM (" & vbCrLf & _
" SELECT " & strTop2 & " * FROM (" & vbCrLf & _
" SELECT * FROM " & strTable & " " & vbCrLf & _
" " & strWHERE & " " & vbCrLf & _
" " & strOrderBy2A & " " & vbCrLf & _
" )" & vbCrLf & _
" ) AS A " & vbCrLf & _
" " & strOrderBy1A & vbCrLf & _
");"
iMod = TotRows Mod iTop
iPageRec = IIf(iTop < iTop2, iTop, iTop2)
If bnShowInfo = True Then
Debug.Print "由" & strTable & "取出" & TotRows & "筆資料,每頁" & iTop & "筆資料共" & iPages & "頁,顯示第" & iPageNumber & "頁,序列" & ((iPageNumber - 1) * iTop) + 1 & "到" & ((iPageNumber - 1) * iTop) + iPageRec & ",共" & iPageRec & "筆資料"
End If
CreateViewByPageSQL = strSQL
End Function
另外,我們建立一個表單,用於檢視與切換資料,畫面如下:
物件名稱分別為:
「List_View」清單方塊,用於檢視資料內容
「cmd_Prev」指令按鈕,用來切到上一頁
「cmd_Next」指令按鈕,用來切到下一頁
「Text_Page」文字方塊,用於顯示現在第幾頁
「Text_Pages」文字方塊,用於顯示總頁數
「Text_iTop」文字方塊,用於設定每頁幾筆資料
該表單的程式碼如下,可整個複製後貼上使用:
Option Compare Database
Private strTable As String
Private iTop As Double
Private iPageNumber As Double
Private strColumns As String
Private strWHERE As String
Private strOrderBy1 As String
Private strOrderBy2 As String
Private Sub cmd_Next_Click()
If Text_Page = Text_Pages Then Exit Sub
Text_Page = Text_Page + 1
Call List_View_RowSource
End Sub
Private Sub cmd_Prev_Click()
If Text_Page = 1 Then Exit Sub
Text_Page = Text_Page - 1
Call List_View_RowSource
End Sub
Private Sub Form_Load()
strColumns = "客戶編號,公司名稱,連絡人,連絡人職稱,城市,地址"
strTable = "客戶"
strWHERE = ""
strOrderBy1 = "城市"
strOrderBy2 = ""
Text_iTop = 17
Text_Page.value = 1
Call Text_iTop_AfterUpdate
End Sub
Sub Text_Pages_Value()
Dim iTop As Double
iTop = Text_iTop
'資料總量
TotRows = DCount("*", strTable, strWHERE)
'總頁數
iPages = TotRows / iTop
a = Int(iPages)
If a <> iPages Then iPages = a + 1 Else iPages = iPages
Text_Pages.value = iPages
If Text_Page > Text_Pages Then Text_Page = Text_Pages
End Sub
Sub List_View_RowSource()
iTop = Text_iTop
iPageNumber = Text_Page.value
strSQL = CreateViewByPageSQL(strTable, iTop, iPageNumber, strColumns, strWHERE, strOrderBy1, strOrderBy2, True)
List_View.RowSource = strSQL
End Sub
Private Sub Text_iTop_AfterUpdate()
Call Text_Pages_Value
Call List_View_RowSource
End Sub
整個程式操作起來並無特別之處,就是上一頁,下一頁這樣的切換著,不過如果實際使用時,可以再增加一些輸入框或選擇框,來達到篩選資料與切換檢視欄位的效果,這樣用起來會更加便捷,不過就看自己的需求,再依照需求去設計。
以上的程式碼分享,希望對大家有幫助。