Statement invalid outside type block

  • Thread starter Thread starter tom
  • Start date Start date
The proper URL is
http://msdn2.microsoft.com/en-us/library/aa200522(office.10).aspx

Turns out there are some errors on that page.

It should be

Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset, lngCode As Long

As well, there's a space missing in the line

rst.Open"Errors", cnn adOpenStatic, adLockOptimistic

it should be

rst.Open "Errors", cnn adOpenStatic, adLockOptimistic
 
Hi, Tom.
I am getting
Statement invalid outside type block

on the statement:

tbl As New ADOX.Table

when running this code:

It's a typo in the declaration. The next line has the same typo. Change
them to the following:

Dim tbl As New ADOX.Table
Dim cnn As ADODB.Connection


HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blog: http://DataDevilDog.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 
I don't know what went wrong when they put the code on MSDN, but here it is
after debugging:

Sub CreateErrorsTable()

Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset, lngCode As Long
Const conAppObjectError = "Application-defined or object-defined error"

Set cnn = CurrentProject.Connection
' Create Errors table with ErrorNumber and ErrorDescription fields.
tbl.Name = "Errors"
tbl.Columns.Append "ErrorCode", adInteger
tbl.Columns.Append "ErrorString", adVarWChar
Set cat.ActiveConnection = cnn
cat.Tables.Append tbl
' Open recordset on Errors table.
rst.Open "Errors", cnn, adOpenStatic, adLockOptimistic
' Loop through first 1000 Visual Basic error codes.
For lngCode = 1 To 1000
On Error Resume Next
' Raise each error.
Err.Raise lngCode
DoCmd.Hourglass True
' Skip error codes that generate application or object-defined errors.
If Err.Description <> conAppObjectError Then
' Add each error code and string to Errors table.
rst.AddNew
rst!ErrorCode = Err.Number
rst!ErrorString = Err.Description
rst.Update
End If
' Clear Err object.
Err.Clear
Next lngCode
' Close recordset.
rst.Close
DoCmd.Hourglass False
MsgBox "Errors table created."

End Sub

Ken Sheridan
Stafford, England
 
Not to mention a comma!

rst.Open "Errors", cnn, adOpenStatic, adLockOptimistic

The MSDN equivalent of the Monday morning car I think<G>.

Ken Sheridan
Stafford, England
 
Back
Top