Querying a Database for Information with Variable Values

ID: Q127127

The information in this article applies to:

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

Last Reviewed: February 5, 1998