如何通过DAO增加MDB字段的大小?

时间:2012-09-12 09:12:14

标签: ms-access size dao ms-access-97

如何使用MDB增加DAO字段大小的大小?

2 个答案:

答案 0 :(得分:2)

使用DDL可以更轻松地完成这项工作:

Set db = CurrentDb

sSQL = "ALTER TABLE table1 ALTER Column atext text(150)"
db.Execute sSQL, dbFailOnError

显然,这在MS Access 97中不可用,但在15年以来的任何版本中,我都建议DDL是最简单的方法。

答案 1 :(得分:1)

来自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