简体   繁体   中英

VBA compile error object required

I have inherited a vba project and have been recently getting my feet wet with vba development. I have enountered an error Compile Error Object Required when running a portion of code that I have recently made edits to. Since it's a compile error, I cant see at runtime exactly which line is throwing the error. The only code I have modified is the ElseIf block in the following code... all help is appreciated.

            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

Remove the Set from this line:

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

Set is only used for assigning object references, and connectionString is a String .

For future reference, the VBE will highlight the line with the compile error if you use Debug->Compile from the menu.

Normally if I was writing and if statement o would have it like this

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

give that a bash. May just bee the formatting error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM