views:

59

answers:

1

I want the column and data-type of a table in VBA ms access form

how to do it easily?

A: 

Give this a try:

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
Oorang
I don't understand the reason for using ADO for the output.
David-W-Fenton
rofl Hi David, we have to stop meeting this way;) Like everything else there are twenty ways to skin a cat. You could VB's native IO capabilities, SQL, the FSO, etc. You could even put the whole thing in Excel and pull the info instead of pushing it. (Which is what I do professionally, I wrote myself a little "developer add-in" that performs various analytics on databases.) I mostly used ADO, because I knew Access would already have the reference set and if someone decides to explore the code, the advanced functions of an ADO.Stream are tad more discoverable than the native File IO tends to be
Oorang
Oh wait... I think I see what you are asking:D ...Like PowerUser, I wasn't 100% clear on the question. But he tagged it Excel, so I just took an educated guess on what he might be wanting to do based on things I've done myself:)
Oorang