DOCUMENT:Q142001 11-JAN-2001 [vbwin] TITLE :FAQ: SQL Queries in Visual Basic PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0,6.0 OPER/SYS: KEYWORDS:kbGrpDSVBDB ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning Edition for Windows, versions 5.0, 6.0 - Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0 - Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0 - Microsoft Visual Basic Standard Edition, 32-bit, for Windows, version 4.0 - Microsoft Visual Basic Professional Edition, 16-bit, for Windows, version 4.0 - Microsoft Visual Basic Professional Edition, 32-bit, for Windows, version 4.0 - Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows, version 4.0 - Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows, version 4.0 ------------------------------------------------------------------------------- SUMMARY ======= This article covers some of the most frequently asked questions (FAQ) about the Structured Query Language (SQL) for Microsoft Visual Basic for Windows. You can find this and other FAQ articles by querying in the Microsoft Knowledge Base using the keyword FAQ. The Visual Basic 3.0 FAQ covering SQL Queries may contain additional information regarding a portion of these questions. This FAQ is in the process of being completed. More frequently asked questions will be added to this article by the end of January 1996. NOTE: Many of the SQL statements in this article use the Biblio.mdb Access database that ships with Visual Basic. For more information on SQL queries, please see the following articles in the Microsoft Knowledge Base: Q128388 : INF: Rushmore Query Optimization Paper Available on MSL MORE INFORMATION ================ 1. Q. How can I test my SQL query quickly and easily? A. Use VisData. VisData is a sample application that ships with Visual Basic 4.0. You'll find it in C:\VB\Samples\VisData\VisData.vbp. It has a window to test SQL queries, can open all of the database types that Visual Basic supports, and has proven to be very stable. 2. Q. How do I write an SQL query that has values that contain single or double quotation marks? A. Single quotation marks ('), double quotation marks ("), and the pipe symbol (|) are all special characters in Microsoft Jet SQL. Double up the quotation marks or, if you have only one type of quotation mark (single or double) in your data, use the other one as the delimiter. All other characters are handled as is. 3. Q. How do I use a Visual Basic variable in an SQL query? A. Visual Basic variables and properties must be concatenated into the SQL string. If you embed the name of the variable, your code will search using the variable name rather than the variable value. 4. Q. How can I call stored procedures (queries) for SQL Server databases? A. You can pass the stored procedure name to an ExecuteSQL, a CreateDynaset, or a DataControl's RecordSource property. 5. Q. How can I select a specific list of values or exclude certain records in a Select statement? A. Use the In keyword to identify the values you want to include or exclude, adding the Not keyword to exclude, as in this example: Select * From Authors Where Au_ID In (1, 3, 5, 7, 9) Select * From Authors Where Au_ID Not In (1, 3, 5, 7, 9) 6. Q. How can I build a Select statement that works with multiple tables? A. A simple Select statement across unrelated tables returns a useless cross product of all the records in the tables listed in the From clause: Select * From Publishers, Titles To get a useful list of data, your data must have a common key field (or set of fields) that relate the tables. You can retrieve data from your tables by simply ensuring that these key fields match for each row you retrieve: Select * From Publishers, Titles Where Publishers.PubID=Titles.PubID However, this returns rows without recognizing the relationship between the records in the separate tables, so the resulting RecordSet will not be updatable. To return an updatable recordset, you need to join the tables as in this example: Select * From Publishers Inner Join Titles On Publishers.PubID=Titles.PubID For more information, please see the "Joins" topic in the Visual Basic Help file and the following articles in the Microsoft Knowledge Base: Q102681 : PRB: Error When Updating Fields in Dynaset That Has 2+ Tables 7. Q. How can I build a Select statement that includes multiple tables in more than one database? A. You can specify an external database with the