2016-07-15 42 views
1

當下面的函數執行'.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 

回答

0

隨着HansUp的幫助下,我決定修改鏈接表的問題。新問題是,如果沒有在工作時間內不能完成的緊湊和修復到後端,AutoNumber字段不會重置爲1。

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