2009-09-14 5 views
0

VBA ms 액세스 형식에서 테이블의 열과 데이터 형식을 원합니다.시트를 추출하는 테이블 디자인을 추출하는 방법은 무엇입니까?

쉽게 수행하는 방법은 무엇입니까?

Option Explicit 

Public Sub ExportTableDesign() 
    Const strFileName_c As String = "testFldOutput.txt" 
    Dim strFullPath As String 
    Dim db As DAO.Database 
    Dim tdf As DAO.TableDef 
    Dim strTblName As String 
    Dim fld As DAO.Field 
    Dim strm As ADODB.Stream 
    Set db = Access.CurrentDb 
    strFullPath = Environ("USERPROFILE") & "\DeskTop\" & strFileName_c 
    Do 
     strTblName = InputBox("Please enter name of table to extract:", "Enter Table Name", strTblName) 
     If LenB(strTblName) = 0& Then Exit Sub 
     If TableExists(strTblName) Then 
      Set tdf = db.TableDefs(strTblName) 
      Exit Do 
     End If 
     MsgBox "Can not find table: " & strTblName, vbExclamation 
    Loop 
    Set strm = New ADODB.Stream 
    With strm 
     .Open 
     .LineSeparator = adCRLF 
     .WriteText "Field Name" & vbTab & "Data Type", adWriteLine 
     For Each fld In tdf.Fields 
      .WriteText fld.name & vbTab & TypeToString(fld.Type), adWriteLine 
     Next 
     .SaveToFile strFullPath, adSaveCreateOverWrite 
    End With 
    db.Close 
    Shell "Excel.exe " & strFullPath & "", vbMaximizedFocus 
End Sub 

Private Function TypeToString(ByVal typeValue As DAO.DataTypeEnum) As String 
    Dim strRtnVal As String 
    Select Case typeValue 
    Case dbBigInt: strRtnVal = "Big Integer" 
    Case dbBinary: strRtnVal = "Binary" 
    Case dbBoolean: strRtnVal = "Boolean" 
    Case dbByte: strRtnVal = "Byte" 
    Case dbChar: strRtnVal = "Char" 
    Case dbCurrency: strRtnVal = "Currency" 
    Case dbDate: strRtnVal = "Date/Time" 
    Case dbDecimal: strRtnVal = "Decimal" 
    Case dbDouble: strRtnVal = "Double" 
    Case dbFloat: strRtnVal = "Float" 
    Case dbGUID: strRtnVal = "GUID" 
    Case dbInteger: strRtnVal = "Integer" 
    Case dbLong: strRtnVal = "Long" 
    Case dbLongBinary: strRtnVal = "Long Binary (OLE Object)" 
    Case dbMemo: strRtnVal = "Memo" 
    Case dbNumeric: strRtnVal = "Numeric" 
    Case dbSingle: strRtnVal = "Single" 
    Case dbText: strRtnVal = "Text" 
    Case dbTime: strRtnVal = "Time" 
    Case dbTimeStamp: strRtnVal = "Time Stamp" 
    Case dbVarBinary: strRtnVal = "VarBinary" 
    Case Else: strRtnVal = "Unknown" 
    End Select 
    TypeToString = strRtnVal 
End Function 

Private Function TableExists(ByVal tableName As String) As Boolean 
    On Error Resume Next 
    TableExists = LenB(Access.CurrentDb.TableDefs(tableName).name) 
End Function 
+0

당신이 질문을 바꿔 수 : – PowerUser

답변

0

이 사용 해보세요? 미안, 이해가 안돼.
+0

출력용으로 ADO를 사용하는 이유를 알지 못합니다. –

+1

rofl 안녕 데이비드, 우리는이 길을 만나는 것을 멈춰야 만한다.) 다른 모든 것들과 마찬가지로, 고양이를 피부로 만드는 20 가지 방법이있다. 당신은 VB의 네이티브 IO 기능, SQL, FSO 등을 할 수 있습니다. Excel에 모든 것을 넣을 수도 있고 푸시하는 대신 정보를 가져올 수도 있습니다. (내가 전문적으로하는 것은 데이터베이스에 대한 다양한 분석을 수행하는 "개발자 추가 기능"을 직접 작성한 것입니다.) Access에서 이미 참조 집합을 갖고 있고 누군가 코드를 탐색하기로 결정했기 때문에 대부분 ADO를 사용했습니다. , ADO.Stream의 고급 기능은 네이티브 File IO보다 더 쉽게 발견 할 수 있습니다. – Oorang

+0

오, 기다려라. 나는 당신이 묻는 것을 보았다고 생각한다 : D ... PowerUser처럼, 나는 100 % 명확하지 않았다. 질문에. 그러나 그는 Excel로 태그를 붙였습니다. 그래서 저는 제가 스스로 해낸 일들을 토대로 그가하고 싶은 일에 대해 교육받은 추측을했습니다. :) – Oorang

관련 문제