If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > ASP with MS Access SQL query using a dynamic Table name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-25-08, 00:58
rjaric rjaric is offline
Registered User
 
Join Date: Nov 2008
Posts: 10
ASP with MS Access SQL query using a dynamic Table name

This is for a problem/defect tracking system for products.
Per the thread title I am using ASP and MS Access.

I'm trying to keep the whole system very dynamic and simple for the user.
To achieve this. many data fields use drop down lists populated from a table.
Once finished, there will be an ability to edit the data in those tables.

One of the Drop Down lists is to the Model name of the device.
I want to be able to use the data from the Model name field to dynamically choose which table the data is added to.
There are many reasons why I'm using a seperate table for each product.

Process

1 - Data Entry Form
User enters the data into text fields and chooses other variables from drop lists presented in an ASP page.
When SUBMIT is clicked the data is posted to the next page.

2 - Data Process Page (invisible to the user)
On this page I have the following line of code to open the table

strSQL = "SELECT * FROM TABLE;"

I'd like to be able to use a variable for the table name.
The variable will come from the previous page in the form of a POST.
(it will be chosen from a drop list).

I imagine it should look similar to this:
strSQL = "SELECT * FROM &TABLENAME&;"

Any help greatly appreciated.
Rob
Reply With Quote
  #2 (permalink)  
Old 11-25-08, 09:49
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Sounds like an icky design, but you're the boss!
Code:
strSQL = "SELECT * FROM " & Request.Form("TableName")
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 11-25-08, 18:07
rjaric rjaric is offline
Registered User
 
Join Date: Nov 2008
Posts: 10
GeorgeV - You the man

Thanks so much.
I've been stuck on this for a bit.

Cheers
Rob
Reply With Quote
  #4 (permalink)  
Old 11-25-08, 20:06
myle myle is offline
(Making Your Life Easy)
 
Join Date: Feb 2004
Location: New Zealand
Posts: 1,143
rjaric

what I do is

strSQL = "SELECT * FROM [TABLENAME];"
^ just make it easyer to read

strSQL = replace(strSQL,"[TABLENAME]",Request.Form("TableName"))

then it can come

strSQL = "SELECT * FROM [TABLENAME] WHERE personid=[ID];"

strSQL = replace(strSQL,"[TABLENAME]",Request.Form("TableName"))
strSQL = replace(strSQL,"[ID]",Request.Form("ID"))

...
...
__________________
hope this help

See clear as mud


StePhan McKillen
the aim is store once, not store multiple times
Remember... Optimize 'til you die!
Progaming environment:
Access based on my own environment: DAO3.6/A97/A2000/A2003
VB based on my own environment: vb6 sp5
ASP based on my own environment: 5.6
VB-NET based on my own environment started 2007
SQL-2005 based on my own environment started 2008
MYLE
Reply With Quote
  #5 (permalink)  
Old 11-25-08, 22:16
rjaric rjaric is offline
Registered User
 
Join Date: Nov 2008
Posts: 10
Thanks myle.

I think your suggestion will give more flexibility to create dynamic Queries.

Much appreciated.
Rob
Reply With Quote
  #6 (permalink)  
Old 11-26-08, 00:50
rjaric rjaric is offline
Registered User
 
Join Date: Nov 2008
Posts: 10
Hi Myle

I can get this to work:
strSQL = "SELECT * FROM [TABLENAME];"
strSQL = replace(strSQL,"[TABLENAME]",Request.Form("TableName"))

But this doesn't work:
strSQL = "SELECT * FROM [TABLENAME] WHERE personid=[ID];"
strSQL = replace(strSQL,"[TABLENAME]",Request.Form("TableName"))
strSQL = replace(strSQL,"[ID]",Request.Form("ID"))

I get the Error:
Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause.
/Display.asp, line 31
Line 31 is:
rsBBT.Open strSQL, adoCon

I'm just using ASP, not ASP.NET.
In what environment did this work for you?

Thanks in advance
Rob
Reply With Quote
  #7 (permalink)  
Old 11-26-08, 03:34
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Why don't you Response.Write the strSQL variable to screen and see if you find a syntax error. If you struggle after this, post the results of the write here and we will take a look.
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 11-26-08, 17:25
myle myle is offline
(Making Your Life Easy)
 
Join Date: Feb 2004
Location: New Zealand
Posts: 1,143
Sorry wrong hat on


msaccess needs the tablename after the select
strSQL = "SELECT [TABLENAME].* FROM [TABLENAME]
strSQL = replace(strSQL,"[TABLENAME]",Request.Form("TableName"))

Code:
Sub Writeit(This,This1)
'This will Display vlaue on a web site
Response.Write "<b>" & This & "</b> = [" & This1 & "]<BR>"
end sub
I have this Sub in my main inc file
then when I want to see something on screen all i need to do is

call writeit("strSQL",strSQL)

it out is

strSQL=[SELECT [TABLENAME].* FROM [TABLENAME]]
__________________
hope this help

See clear as mud


StePhan McKillen
the aim is store once, not store multiple times
Remember... Optimize 'til you die!
Progaming environment:
Access based on my own environment: DAO3.6/A97/A2000/A2003
VB based on my own environment: vb6 sp5
ASP based on my own environment: 5.6
VB-NET based on my own environment started 2007
SQL-2005 based on my own environment started 2008
MYLE

Last edited by myle; 11-26-08 at 17:28.
Reply With Quote
  #9 (permalink)  
Old 11-26-08, 18:14
rjaric rjaric is offline
Registered User
 
Join Date: Nov 2008
Posts: 10
Thanks GeorgeV and Myle,

I did a response.write to the screen and was able to see the SQL query.
Found that I needed to fine tune the first line a bit

So this: strSQL = "SELECT * FROM [TABLENAME] WHERE personid=[ID];"
Became:strSQL = "SELECT * FROM [TABLENAME] WHERE personid ='[ID]'"

Replace lines remained the same:
strSQL = replace(strSQL,"[TABLENAME]",Request.Form("TableName"))
strSQL = replace(strSQL,"[VALUE]", Request.Form("ID"))

So it was just two missing single quotes.
I did find that having ; at the end did nothing.

Thanks again.
Rob
Reply With Quote
  #10 (permalink)  
Old 11-27-08, 03:32
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
You only need to quote character fields - I would have guessed that ID was a number...

And the semi-colon is good practice to include; it terminates the statement explicitly.
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On