Querying a Database for Information with Variable Values

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

Additional query words: 6.0 winword odbc sql word6 7.0 word95 word7 macro

Keywords : kbinterop
Version : WINDOWS:6.0,6.0a,6.0c,7.0; winnt:6.0
Platform : WINDOWS winnt
Issue type :


Last Reviewed: January 3, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.