Accessing an Excel database using Word

G

Greg Maxey

I have some code in a Word VBA project module that I scratched together from
Google searches that I use to import data from an Excel spreadsheet into a
Word userform.

One line of the code reads:

Set db = OpenDatabase(ThisDocument.Path & "\" & "SourceData.xls", False,
False, "Excel 8.0; IMEX=1;")

In an attempt to understand exaclty what that line of code means, I looked
up OpenDataBase in Excel help and the following was returned.

Sub UseOpenDatabase()
' Open the Northwind database in the background and create a PivotTable
Workbooks.OpenDatabase Filename:="c:\Northwind.mdb", _
CommandText:="Orders", _
CommandType:=xlCmdTable, _
BackgroundQuery:=True, _
ImportDataAs:=xlPivotTableReport
End Sub

The two don't appear to have a lot in common :-(

I understand the "FileName" piece. Can someone please explain what "False,"
"False" and "Excel 8.0; IMEX=1;" in my line of code means?

What would be the result in one of the "Falses" were changed to true? What
are some of the other values that IMEX can take. What do they mean.

Thanks.
 
J

Jim Cone

I suspect that most code you see is generated by recording a macro.
I doubt if many people could just sit down and write it out.
I couldn't.
--
Jim Cone
Portland, Oregon USA


FROM THE EXCEL 97 HELP FILE...
OpenDatabase Method
Opens a specified database in a Workspace object and returns a reference to the Database object that represents it.

SYNTAX:
Set database = workspace.OpenDatabase (dbname, options, read-only, connect)

The OpenDatabase method syntax has these parts.

database:
An object variable that represents the Database object that you want to open.
workspace Optional: An object variable that represents the existing Workspace
object that will contain the database. If you don't include a value for workspace,
OpenDatabase uses the default workspace.

dbname:
A String that is the name of an existing Microsoft Jet database file,
or the data source name (DSN) of an ODBC data source.
See the Name property for more information about setting this value.

options Optional:
A Variant that sets various options for the database, as specified in Settings.

read-only Optional:
A Variant (Boolean subtype) value that is True if you want to open the
database with read-only access, or False (default) if you want to open the
database with read/write access.

connect Optional:
A Variant (String subtype) that specifies various connection information, including passwords.

Settings
For Microsoft Jet workspaces, you can use the following values for the options argument.
Setting Description
True Opens the database in exclusive mode.
False (Default) Opens the database in shared mode.


For ODBCDirect workspaces, the options argument determines if and when to
prompt the user to establish the connection. You can use one of the following constants.

Constant Description
dbDriverNoPrompt
The ODBC Driver Manager uses the connection string provided in
dbname and connect. If you don't provide sufficient information, a run-time error occurs.
dbDriverPrompt
The ODBC Driver Manager displays the ODBC Data Sources dialog box,
which displays any relevant information supplied in dbname or connect.
The connection string is made up of the DSN that the user selects via the dialog boxes, or, if the user doesn't specify a DSN, the
default DSN is used.
dbDriverComplete (Default)
If the connect and dbname arguments include all the necessary
information to complete a connection, the ODBC Driver Manager uses the string in connect. Otherwise it behaves as it does when you
specify dbDriverPrompt.
dbDriverCompleteRequired
This option behaves like dbDriverComplete except the ODBC
driver disables the prompts for any information not required to complete the connection.

Remarks
When you open a database, it is automatically added to the Databases collection.
Further, in an ODBCDirect workspace, the Connection object corresponding to the new
Database object is also created and appended to the Connections collection of the same
Workspace object. Some considerations apply when you use dbname:

· If it refers to a database that is already open for exclusive access by another user, an error occurs.
· If it doesn't refer to an existing database or valid ODBC data source name, an error occurs.
· If it's a zero-length string ("") and connect is "ODBC;", a dialog box listing all registered ODBC data source names is displayed
so the user can select a database.
· If you're opening a database through an ODBCDirect workspace and you provide the DSN in connect, you can set dbname to a string of
your choice that you can use to reference this database in subsequent code.


The CONNECT argument is expressed in two parts: the database type, followed by a semicolon (;) and the optional arguments. You must
first provide the database type, such as "ODBC;" or "FoxPro 2.5;".
The optional arguments follow in no particular order, separated by semicolons.
One of the parameters may be the password (if one is assigned). For example:
"FoxPro 2.5; pwd=mypassword"

Using the NewPassword method on a Database object other than an ODBCDirect database
changes the password parameter that appears in the ";pwd=..." part of this argument.
You must supply the options and read-only arguments to supply a source string.
See the Connect property for syntax.

To close a database, and thus remove the Database object from the Databases collection, use the Close method on the object.

Note When you access a Microsoft Jet-connected ODBC data source, you can improve
your application's performance by opening a Database object connected to the ODBC data source,
rather than by linking individual TableDef objects to specific tables in the ODBC data source.
'-----------------



"Greg Maxey"
wrote in message
I have some code in a Word VBA project module that I scratched together from
Google searches that I use to import data from an Excel spreadsheet into a
Word userform.
One line of the code reads:

Set db = OpenDatabase(ThisDocument.Path & "\" & "SourceData.xls", False,
False, "Excel 8.0; IMEX=1;")

In an attempt to understand exaclty what that line of code means, I looked
up OpenDataBase in Excel help and the following was returned.

Sub UseOpenDatabase()
' Open the Northwind database in the background and create a PivotTable
Workbooks.OpenDatabase Filename:="c:\Northwind.mdb", _
CommandText:="Orders", _
CommandType:=xlCmdTable, _
BackgroundQuery:=True, _
ImportDataAs:=xlPivotTableReport
End Sub

The two don't appear to have a lot in common :-(
I understand the "FileName" piece. Can someone please explain what "False,"
"False" and "Excel 8.0; IMEX=1;" in my line of code means?
What would be the result in one of the "Falses" were changed to true? What
are some of the other values that IMEX can take. What do they mean.
Thanks.
 
G

Greg Maxey

Jim,

This was helpful. Thanks.

The part that I still need help with is:

"The CONNECT argument is expressed in two parts: the database type, followed
by a semicolon (;) and the optional arguments. You must
first provide the database type, such as "ODBC;" or "FoxPro 2.5;".
The optional arguments follow in no particular order, separated by
semicolons.
One of the parameters may be the password (if one is assigned). For example:
"FoxPro 2.5; pwd=mypassword""

Where does one go to learn what the various database types are called. For
example, the code I provided works if the database is a Excel2003 type data
base but not if it is an Excel2007 type data base. Where does one go to
learn what some or all of the optional arguments are (e.g., what is IMEX?
what does setting it equal to 1 do?). Thanks.
 
J

Jim Cone

I assume the optional arguments depend on the database being searched
and the kind of database it is depends on who put it together.

So to use them you already have to know what they are. <g>
Maybe an Access group could provide more info, I can't.
--
Jim Cone
Portland, Oregon USA



"Greg Maxey"
wrote in message
Jim,
This was helpful. Thanks.
The part that I still need help with is:

"The CONNECT argument is expressed in two parts: the database type, followed
by a semicolon (;) and the optional arguments. You must
first provide the database type, such as "ODBC;" or "FoxPro 2.5;".
The optional arguments follow in no particular order, separated by
semicolons.
One of the parameters may be the password (if one is assigned). For example:
"FoxPro 2.5; pwd=mypassword""

Where does one go to learn what the various database types are called. For
example, the code I provided works if the database is a Excel2003 type data
base but not if it is an Excel2007 type data base. Where does one go to
learn what some or all of the optional arguments are (e.g., what is IMEX?
what does setting it equal to 1 do?). Thanks.
 

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