Visual Basic Tips


Jetデータベース(DAO)テーブル名の列挙


Jetデータベースのテーブル名を列挙し、コンボボックス、リストボックスにその名称を追加します。

Private Sub Form_Load()
  Call TblNameCtlSet(Combo1, App.Path & "\work.mdb")
  Call TblNameCtlSet(List1, App.Path & "\work.mdb")
End Sub

Sub TblNameCtlSet(ctlControl As Control, strDbPath As String)
  
Dim ws As Workspace
  
Dim db As Database
  
Dim tdf As TableDef

  
If TypeOf ctlControl Is ComboBox Or TypeOf ctlControl Is ListBox Then
  
Else
    
Exit Sub
  
End If

  ctlControl.Clear

  
Set ws = DBEngine.Workspaces(0)
  
Set db = ws.OpenDatabase(strDbPath, False, True)

  ctlControl.Clear
  
'テーブル名をコントロールに追加
  
For Each tdf In db.TableDefs
    
If (tdf.Attributes And dbSystemObject) = 0 Then
      ctlControl.AddItem tdf.Name
    
End If
  
Next

  
If TypeOf ctlControl Is ComboBox Then
    ctlControl.Text = ctlControl.List(0)
  
End If

  db.Close
  ws.Close
End Sub


DownLoad vbtips023.lzh 7KB (VB6.0)