NewAccessDatabase()

D

Dirk Goldgar

a said:
Thank youI'm using Office Access 2003Why this code not work?And this
message box appear to
me?'''''''''''''''''''''''''''''''''''''''''''''''''''Runtime error
'91'Object variable or with block variable not set
''''''''''''''''''''''''''''''''''''''''''''''''''''' Include following in
Declarations section of module.
Dim appAccess As Access.Application

Sub NewAccessDatabase()
Dim dbs As Object, tdf As Object, fld As Variant
Dim strDB As String
Const DB_Text As Long = 10
Const FldLen As Integer = 40

' Initialize string to database path.
strDB = "C:\My Documents\Newdb.mdb"
' Create new instance of Microsoft Access.
Set appAccess = _
CreateObject("Access.Application.9")
' Open database in Microsoft Access window.
appAccess.NewCurrentDatabase strDB
' Get Database object variable.
Set dbs = appAccess.CurrentDb
' Create new table.
Set tdf = dbs.CreateTableDef("Contacts")
' Create field in new table.
Set fld = tdf. _
CreateField("CompanyName", DB_Text, FldLen)
' Append Field and TableDef objects.
tdf.Fields.Append fld
dbs.TableDefs.Append tdf
Set appAccess = Nothing
End Sub


If you're using Access 2003, don't specify Access version 9 in the call to
CreatObject. Try this:

Set appAccess = _
CreateObject("Access.Application.11")

or this:

Set appAccess = _
CreateObject("Access.Application")

Also, depending on your operating system and folder structure, you may need
to change the folder path specified for the database to be created in this
line:
strDB = "C:\My Documents\Newdb.mdb"

On my Vista system, that folder doesn't exist, and I had to use:

strDB = "C:\Users\Dirk\Documents\Newdb.mdb"

Other than that, the code worked for me.
 
A

a

Thank youI'm using Office Access 2003Why this code not work?And this message
box appear to me?'''''''''''''''''''''''''''''''''''''''''''''''''''Runtime
error '91'Object variable or with block variable not set
''''''''''''''''''''''''''''''''''''''''''''''''''''' Include following in
Declarations section of module.
Dim appAccess As Access.Application

Sub NewAccessDatabase()
Dim dbs As Object, tdf As Object, fld As Variant
Dim strDB As String
Const DB_Text As Long = 10
Const FldLen As Integer = 40

' Initialize string to database path.
strDB = "C:\My Documents\Newdb.mdb"
' Create new instance of Microsoft Access.
Set appAccess = _
CreateObject("Access.Application.9")
' Open database in Microsoft Access window.
appAccess.NewCurrentDatabase strDB
' Get Database object variable.
Set dbs = appAccess.CurrentDb
' Create new table.
Set tdf = dbs.CreateTableDef("Contacts")
' Create field in new table.
Set fld = tdf. _
CreateField("CompanyName", DB_Text, FldLen)
' Append Field and TableDef objects.
tdf.Fields.Append fld
dbs.TableDefs.Append tdf
Set appAccess = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top