链接表

时间:2016-07-15 19:00:45

标签: ms-access access-vba

当下面的函数执行'.Append fld'时,它会产生错误3057.我怀疑需要一个完全限定的名称,但该方法正在逃避我。调用此函数的子函数通过strTableName“tbl_elements”传递,但这对于此函数来说还不够。

我已尝试使用另一段已成功到达链接表的代码。

CurrentDb.Execute "ALTER TABLE [" & CurrentDb.TableDefs(strImportHoldingTable).Connect & "].[" & strImportHoldingTable & "] DROP COLUMN romis_tran_id;"

下面的问题

Public Function CreateAutoNumberField(ByVal strTableName As String, ByVal strFieldName As String) As Boolean

On Error GoTo Err_CreateAutoNumberField

    Dim db As DAO.Database
    Dim fld As DAO.Field
    Dim tdef As DAO.TableDef

    Set db = Application.CurrentDb
    Set tdef = db.TableDefs(strTableName)
    Set fld = tdef.CreateField(strFieldName, dbLong)
    With fld
        .Attributes = .Attributes Or dbAutoIncrField
    End With
    With tdef.Fields
        .Append fld
        .Refresh
    End With

    CreateAutoNumberField = True

Exit_CreateAutoNumberField:

    Set fld = Nothing
    Set tdef = Nothing
    Set db = Nothing
    Exit Function

Err_CreateAutoNumberField:

    CreateAutoNumberField = False
    With Err
        MsgBox "Error " & .Number & vbCrLf & .description, vbOKOnly Or vbCritical, "CreateAutoNumberField"
    End With
    Resume Exit_CreateAutoNumberField

End Function

1 个答案:

答案 0 :(得分:0)

在HansUp的帮助下,我解决了修改链接表的问题。新问题是AutoNumber字段没有重置为1而没有Compact和Repair到后端,这是我在工作时间内无法做到的。

Public Function resetAutoNumber(ByVal strTableName As String, ByVal strFieldName As String, ByVal strIndexName As String) As Boolean
On Error GoTo ErrTrap

Dim db As DAO.Database
Dim fld As DAO.Field
Dim tdef As DAO.TableDef
Dim strDbPath As String

strDbPath = Mid(CurrentDb.TableDefs(strTableName).Connect, 11)

Set db = DBEngine.OpenDatabase(strDbPath)
Set tdef = db.TableDefs(strTableName)
Set fld = tdef.CreateField(strFieldName, dbLong)
Set ind = tdef.CreateIndex(strIndexName)

'clear table
db.Execute "DELETE * FROM " & strTableName
'delete index
tdef.Indexes.Delete strIndexName
'delete field
tdef.Fields.Delete strFieldName
're-create field
    With fld
        .Attributes = .Attributes Or dbAutoIncrField
        .OrdinalPosition = 0
    End With
    With tdef.Fields
        .Append fld
        .Refresh
    End With
'recreate index
ind.Fields.Append ind.CreateField(strIndexName)
tdef.Indexes.Append ind

Set ind = Nothing
Set fld = Nothing
Set tdef = Nothing
Set db = Nothing

ExitHere:
Exit Function
ErrTrap:
MsgBox Err.description
Resume ExitHere

End Function