Global It Leader!!



 
 

VB ADO를 이용한 MDB 제어하기

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 10,433회 작성일 12-08-31 18:54

본문

ADOX를 이용한 MDB Control
' "구성요소 -> 참조"에서 "Microsoft ADO Ext. 2.7 for DDL and Security"를 선택해야 합니다.
1. 깡통 DB 파일 만들기
다음 코드는 create Method를 이용해서 MS Jet database를 어떻게 만드는지를 보여준다.

Attribute VB_Name = "Create"
Option Explicit

' BeginCreateDatabseVB
Sub CreateDatabase()
    On Error GoTo CreateDatabaseError
    
    Dim cat As New ADOX.Catalog

    ' Jet.OLEDB.4.0  : MDB for Access2000
    ' Jet.OLEDB.3.51 : MDB for Access97
    cat.Create "Provider='Microsoft.Jet.OLEDB.4.0';Data Source='c:\new.mdb'"

    'Clean up
    Set cat = Nothing
    Exit Sub
    
CreateDatabaseError:
    Set cat = Nothing

    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Sub
' EndCreateDatabaseVB
2. 테이블 생성
' BeginCreateTableVB
Sub Main()
    On Error GoTo CreateTableError
    
    Dim tbl As New Table
    Dim cat As New ADOX.Catalog
    
    ' Open the Catalog.
    cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
        "Data Source='c:\Program Files\Microsoft Office\" & _
        "Office\Samples\Northwind.mdb';"
    
    tbl.Name = "MyTable"
    tbl.Columns.Append "Column1", adInteger
    tbl.Columns.Append "Column2", adInteger
    tbl.Columns.Append "Column3", adVarWChar, 50
    cat.Tables.Append tbl
    Debug.Print "Table 'MyTable' is added."
    
    'Delete the table as this is a demonstration.
    cat.Tables.Delete tbl.Name
    Debug.Print "Table 'MyTable' is deleted."
    
    'Clean up
    Set cat.ActiveConnection = Nothing
    Set cat = Nothing
    Set tbl = Nothing
    Exit Sub
    
CreateTableError:

    Set cat = Nothing
    Set tbl = Nothing

    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Sub
' EndCreateTableVB
3. 테이블 생성 및 인덱스 추가
' BeignCreateIndexVB
Sub Main()
    On Error GoTo CreateIndexError

    Dim tbl As New Table
    Dim idx As New ADOX.Index
    Dim cat As New ADOX.Catalog
    
    'Open the catalog.
    ' Open the Catalog.
    cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
        "Data Source='c:\Program Files\Microsoft Office\" & _
        "Office\Samples\Northwind.mdb';"
    
    ' Define the table and append it to the catalog
    tbl.Name = "MyTable"
    tbl.Columns.Append "Column1", adInteger
    tbl.Columns.Append "Column2", adInteger
    tbl.Columns.Append "Column3", adVarWChar, 50
    cat.Tables.Append tbl
    Debug.Print "Table 'MyTable' is added."
    
    ' Define a multi-column index
    idx.Name = "multicolidx"
    idx.Columns.Append "Column1"
    idx.Columns.Append "Column2"
    
    ' Append the index to the table
    tbl.Indexes.Append idx
    Debug.Print "The index is appended to table 'MyTable'."
    
    'Delete the table as this is a demonstration
    cat.Tables.Delete tbl.Name
    Debug.Print "Table 'MyTable' is deleted."
    
    'Clean up
    Set cat.ActiveConnection = Nothing
    Set cat = Nothing
    Set tbl = Nothing
    Set idx = Nothing
    Exit Sub
    
CreateIndexError:
    Set cat = Nothing
    Set tbl = Nothing
    Set idx = Nothing
    
    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Sub
' EndCreateIndexVB
4. Foreign Key 생성
' BeginCreateKeyVB
Sub Main()
    On Error GoTo CreateKeyError

    Dim kyForeign As New ADOX.Key
    Dim cat As New ADOX.Catalog

    ' Connect the catalog
    cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
        "Data Source='c:\Program Files\Microsoft Office\" & _
        "Office\Samples\Northwind.mdb';"
    
    ' Define the foreign key
    kyForeign.Name = "CustOrder"
    kyForeign.Type = adKeyForeign
    kyForeign.RelatedTable = "Customers"
    kyForeign.Columns.Append "CustomerId"
    kyForeign.Columns("CustomerId").RelatedColumn = "CustomerId"
    kyForeign.UpdateRule = adRICascade
    
    ' Append the foreign key
    cat.Tables("Orders").Keys.Append kyForeign
    
    'Delete the Key as this is a demonstration
    cat.Tables("Orders").Keys.Delete kyForeign.Name
    
    'Clean up
    Set cat.ActiveConnection = Nothing
    Set cat = Nothing
    Set kyForeign = Nothing
    Exit Sub
    
CreateKeyError:
    Set cat = Nothing
    Set kyForeign = Nothing
    
    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
    
End Sub
' EndCreateKeyVB
[출처] : MSDN 자료중에서 ADOX 관련 Example 일부를 정리한 것입니다.

[이 게시물은 오원장님에 의해 2013-02-28 16:41:58 오피스팁에서 이동 됨]

댓글목록

등록된 댓글이 없습니다.

전체 440
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
200 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8250 07-08
199 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9307 07-05
198 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8799 07-04
197 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8873 07-02
196 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8092 07-01
195 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4866 10-29
194 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5320 08-31
열람중 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10434 08-31
192 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5280 08-31
191 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6575 08-31
190 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7000 08-29
189 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4827 08-29
188 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5711 08-24
187 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5148 08-09
186 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6008 08-09
185 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5576 04-07
184 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 13227 01-07
183 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7347 12-01
182 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7477 04-08
181 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9552 04-08