iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 13
0
自我挑戰組

Access VBA的眉眉角角系列 第 13

Access VBA 的眉眉角角Day13: 列出資料表與欄位清單

  • 分享至 

  • xImage
  •  

當初步規劃Access資料庫時,可能會先由手動由資料表處新增資料表,然後再依照需求建立相關VBA程式,中途可能會再調整程式與資料表,但最後為了要發布程式,可能會將CREATE TABLE的語句搬到VBA程式中,以便在資料表不存在時,由VBA即可建立,但如果要手動撰寫CREATE TABLE實在有點麻煩,這時候使用以下程式將會簡單的多,這程式依然是由網路上找到的,但自己依照需求又加上了指定資料表的功能,使用上會方便些。

主程式,sp_help的名字應該是由SQL Server的功能名稱而來

Public Function sp_help(Optional strTable As String = "") As Boolean
'列出所有資料表與欄位資訊
'https://bytes.com/topic/access/answers/467046-query-get-table-schema

sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String
Const constPad01 = 25
Const constPad02 = 15

Set db = CurrentDb()

For Each tbl In db.TableDefs
    If strTable = "" Or strTable = tbl.Name Then
        Debug.Print ""
        Debug.Print tbl.Name
        For Each fld In tbl.Fields
            sStmt = " " & fld.Name & String(constPad01 - Len(fld.Name), " ")
            Select Case fld.Type
            Case dbLong:
                sStmt = sStmt & "long" & " (" & fld.Size & ")"
            Case dbText:
              sStmt = sStmt & "text" & " (" & fld.Size & ")"
            Case dbMemo:
                sStmt = sStmt & "memo" & " (" & fld.Size & ")"
            Case dbDate:
                sStmt = sStmt & "date" & " (" & fld.Size & ")"
            Case dbTime:
                sStmt = sStmt & "time" & " (" & fld.Size & ")"
            Case dbTimeStamp:
                sStmt = sStmt & "timestamp" & " (" & fld.Size & ")"
            Case dbLongBinary:
                sStmt = sStmt & "long binary" & " (" & fld.Size & ")"
            Case dbBigInt:
                sStmt = sStmt & "BigInt" & " (" & fld.Size & ")"
            Case dbBinary:
                sStmt = sStmt & "Binary" & " (" & fld.Size & ")"
            Case dbVarBinary:
                sStmt = sStmt & "VarBinary" & " (" & fld.Size & ")"
            Case dbBoolean:
                sStmt = sStmt & "Boolean" & " (" & fld.Size & ")"
            Case dbByte:
                sStmt = sStmt & "Byte" & " (" & fld.Size & ")"
            Case dbChar:
                sStmt = sStmt & "Char" & " (" & fld.Size & ")"
            Case dbDouble:
                sStmt = sStmt & "Double" & " (" & fld.Size & ")"
            Case dbFloat:
                sStmt = sStmt & "Float" & " (" & fld.Size & ")"
            Case dbInteger:
                sStmt = sStmt & "Integer" & " (" & fld.Size & ")"
            Case dbCurrency:
                sStmt = sStmt & "Currency" & " (" & fld.Size & ")"
            Case dbSingle:
                sStmt = sStmt & "Single" & " (" & fld.Size & ")"
            Case Else:
                sStmt = sStmt & fld.Type & " (" & fld.Size & ")"
            End Select
            
            Debug.Print sStmt
        Next
    End If
Next

Set db = Nothing

End Function

以下進行測試

Sub sp_help測試()
    Dim strTable As String
    
    '不指定,全部列出
    Call sp_help
    
    '指定資料表
    strTable = "供應商"
    Call sp_help(strTable)

End Sub

輸出結果:
http://ithelp.ithome.com.tw/upload/images/20161213/20007221gmQ3mJKh0B.png

各位可以依照自己需求再自己修改程式碼,以便達到自己的需求。
今天的分享希望對各位有幫助。


上一篇
Access VBA 的眉眉角角Day12: 檔案清單的建立
下一篇
Access VBA 的眉眉角角Day14: 關於多頁切換的SQL語法與應用
系列文
Access VBA的眉眉角角30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言