Search code examples
ms-accesssizedaoms-access-97

How to increase size of MDB field via DAO?


How can you increase the size of a MDB field size using DAO?


Solution

  • From http://www.freevbcode.com/ShowCode.asp?ID=4599:

    Public Sub change_field_size(DBPath as string, _
      tblName As String, fldName As String, fldSize As Integer)
        ' this routine changes the field size
    
        Dim db As Database
        Dim td As TableDef
        Dim fld As field
    
        On Error GoTo errhandler
    
        Set db = OpenDatabase(DBPath)
        Set td = db.TableDefs(tblName)
    
        If td.Fields(fldName).Type <> dbText Then
            ' wrong field type
            db.Close
            Exit Sub
        End If
    
        If td.Fields(fldName).size = fldSize Then
            ' the field width is correct
            db.Close
            Exit Sub
        End If
    
        ' create a temp feild
        td.Fields.Append td.CreateField("temp", dbText, fldSize)
        td.Fields("temp").AllowZeroLength = True
        td.Fields("temp").DefaultValue = """"""
    
        ' copy the info into the temp field
        db.Execute "Update " & tblName & " set temp = " & fldName & " "
    
        ' delete the field
        td.Fields.Delete fldName
    
        ' rename the field
        td.Fields("temp").Name = fldName
        db.Close
    
    '======================================================================
    Exit Sub
    
    errhandler:
    MsgBox CStr(Err.Number) & vbCrLf & Err.Description & vbCrLf & "Change Field Size Routine", vbCritical, App.Title
    
    End Sub