需要VBA编译错误对象

时间:2016-11-29 14:47:12

标签: vba ms-access

我继承了一个vba项目,并且最近在vba开发方面受到了影响。运行我最近编辑过的部分代码时,我遇到了错误Compile Error Object Required。由于它是一个编译错误,我无法在运行时确切地看到哪一行引发了错误。我修改过的唯一代码是以下代码中的ElseIf块...感谢所有帮助。

            If tableType = 1 Then 'Resident/Local
                Set db = CurrentDb
                db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
            ElseIf tableType = 4 Then
                Set conn = New ADODB.Connection
                Dim connectionString As String
                Set connectionString = DLookup("[Connect]", "[MSysObjects]", "[Name] = '" & TableName & "'")
                conn.connectionString = connectionString
                conn.Open
                Dim cmd As ABODB.Command
                Set cmd = New ADODB.Command
                cmd.ActiveConnection = conn
                cmd.CommandText = "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
                cmd.Execute
                conn.Close
            Else 'Linked Table
                Set wsp = DBEngine.Workspaces(0)
                Set db = wsp.OpenDatabase(DLookup("[Database]", "[MSysObjects]", "[Name] = '" & TableName & "'"))
                db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"  'reset the autonumber column value
            End If

2 个答案:

答案 0 :(得分:2)

从此行中删除Set

Set connectionString = DLookup("[Connect]", "[MSysObjects]", "[Name] = '" & TableName & "'")

Set仅用于分配对象引用,而connectionStringString

为了将来参考,如果您从菜单中使用Debug-> Compile,VBE将突出显示编译错误的行。

答案 1 :(得分:-1)

通常情况下,如果我正在写作,如果声明o会像这样

If tableType = 1 Then 'Resident/Local
            Set db = CurrentDb
            db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
        Else
If tableType = 4 Then

给那个bash。可能只是格式化错误