Querying a Database for Information with Variable Values

Last reviewed: February 5, 1998
Article ID: Q127127
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, version 7.0
  • Microsoft Word for Windows NT, version 6.0

SUMMARY

You can use WordBasic to query an MS Access database to retrieve information using variables as criteria. This article contains a macro that illustrates this procedure using DDE.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

For best compatibility across with all Access versions, keep the quotations for the SQL statement in proper sequence. This should avoid any Word/Access, or DDE errors.

Sub MAIN
 CompName$ = "Eastern Connection"
 ContName$ = "Ann Devon"
 Connect$ = "TABLE Customers"
 DSN$ = "C:\MSOFFICE\ACCESS\SAMPAPPS\NWIND.MDB"
 SQL$ = "SELECT * FROM [Customers] WHERE (([Contact Name] = '" + ContName$
+ "') AND ([Company Name] = '" + CompName$ + "'))"
 InsertDatabase .Connection = Connect$, .SQLStatement = SQL$, .DataSource =
DSN$
End Sub

Note: In case it's difficult to distinguish single quotation marks from double quotation marks in the SQL$ = line above, the following line uses SQ for the single quotation mark and DQ for double quotation marks:

SQL $ = DQSELECT * FROM [Customers] WHERE (([Contact Name] = SQDQ + ContName$ + DQSQ) AND ([Company Name] = SQDQ + CompName$ + DQSQ))DQ


KBCategory: kbmacro
KBSubcategory:
Additional reference words: 6.0 6.0a 6.0c winword odbc sql word6
7.0 word95 word7 macro
Keywords : kbinterop kbmacro
Version : 6.0 6.0a 6.0c 7.0
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.