Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2013, 02:10 PM
Registered User
 
Join Date: Nov 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sub Query for from clause

Hi all,

I am trying to use a select query for my from clause to access a table name. The reason being is I want the user to be able to select a table from a combobox without the prepended "tbleq...". So, I have a table named "tbleqMCC" and I want the combobox to show "MCC". That part works okay. Then, based on user's selection in the combobox, I want to show data from that table. That part is not working. My SQL looks like this:
Code:
SELECT *
FROM (
SELECT Replace([ID], [ID], "tbleq" & [ID]) 
FROM tblTypesofEquip
where ID = [Forms]![Main]![combo26]
);
The result is just the table name, not the data within that table.

Thanks much.

Yazzy
 
Old December 11th, 2013, 04:38 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

I would build the SQL in VBA code first.

Not sure you need a sub query.


Something like this:

Code:
Dim strSQL as String

strSQL = "SELECT * FROM tbleq" & [Forms]![Main]![combo26]  & ";"

' print the  SQL statement into immediate window for debugging
Debug.Print strSQL
Now that you have the SQL built you can assign it to the record source of a form or save is as a query that can be used as a form's record source.

This example may help: DB Manager (Click Here)
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015

Last edited by HiTechCoach; December 11th, 2013 at 04:40 PM..
 
Old December 11th, 2013, 05:42 PM
Registered User
 
Join Date: Nov 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Now that you have the SQL built you can assign it to the record source of a form or save is as a query that can be used as a form's record source."

I am not sure how to do this? The only way I know to utilize the VBA code is with control items (i.e., combo box, command button, etc..). How do I write just a string of VBA code and then apply it to a query?

Thanks much for your quick response.
 
Old December 11th, 2013, 10:03 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

Without more detail about your form design I am not sure which event will be best. You could use the after update event of the combo box that selects the table name.

Here is an example of how to create a saved query using the QueryDef() Collection.


Code:

    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef

    Dim strSQL as String

     strSQL = "SELECT * FROM tbleq" & [Forms]![Main]![combo26]  & ";"

    ' print the  SQL statement into immediate window for debugging
     Debug.Print strSQL


    'Set database to add querydef
    Set db = CurrentDb()

    'Add querydef
    Set qdf = db.CreateQueryDef("Your Query Name Here")
    qdf.SQL = strSQL

    qdf.Close
    db.Close

    'Refresh database window
    RefreshDatabaseWindow
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015





Similar Threads
Thread Thread Starter Forum Replies Last Post
update query error in from clause prasath15@yahoo.co.in Classic ASP Databases 1 June 6th, 2005 06:02 AM
case clause in query mateenmohd SQL Server 2000 2 February 22nd, 2005 05:49 AM
count clause in query mateenmohd SQL Server 2000 2 June 10th, 2004 01:47 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
More than 1 WHERE clause? onlyu2 MySQL 1 March 26th, 2004 01:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.