473,378 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating dynamic sql

I want to do simply this in MS Access.
PARAMETERS pTableName;

SELECT * FROM pTableName;
This doesn't work ( if it did I wouldn't ask you :) ) but is it clear
what I want to do? Just with passing table names or columns as
parameters I want to change my query but not using any programming
language as Java, VB, C# ... Just like Stored Procedure in SQL Server I
want to create this query and call it from my proc.

Your urgent helps needed. Thanks.

DeepHalo

Jan 4 '06 #1
9 11456
On 4 Jan 2006 05:53:29 -0800, "DeepHalo" <de******@gmail.com> wrote:

As you found out, Access does not support this. Alternatives include:
* Use a dynamic sql statement. Something like:
dim sql as string
sql = "select * from SomeTable"
DoCmd.OpenForm "frmTest",,,,,sql 'I hope that was enough commas

and in frmTest:
Private Sub Form_Open(Cancel as Boolean)
Me.RecordSource = OpenArgs
End Sub

* Set the SQL property of an existing Querydef
dim sql as string
sql = "select * from SomeTable"
Currentdb.Querydefs("SomeQuery").SQL = sql

-Tom.

I want to do simply this in MS Access.
PARAMETERS pTableName;

SELECT * FROM pTableName;
This doesn't work ( if it did I wouldn't ask you :) ) but is it clear
what I want to do? Just with passing table names or columns as
parameters I want to change my query but not using any programming
language as Java, VB, C# ... Just like Stored Procedure in SQL Server I
want to create this query and call it from my proc.

Your urgent helps needed. Thanks.

DeepHalo


Jan 4 '06 #2
Here is my SQL Server SP:

CREATE PROCEDURE MyQuery @TABLENAME VARCHAR(10) AS EXEC('SELECT * FROM
' + @TABLENAME + '')

and the function I use with it:

Private Function RunQuery(ByVal tableName As String) As DataTable
Dim da As IDbDataAdapter
Dim cmd As IDbCommand
Dim ds As New DataSet
Dim dt As DataTable

Try
cmd = myDataProvider.ERCommand("MyQuery")
cmd.Connection = myDataProvider.ERConnection
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(myDataProvider.ERParameter("TAB LENAME"))
cmd.Parameters(0).Value = tableName

da = myDataProvider.ERDataAdapter
da.SelectCommand = cmd
da.Fill(ds)
dt = ds.Tables(0)

RunQuery= dt
Catch ex As Exception
Call ErrorControl(ex, "Error:RunQuery")
Finally
da = Nothing
cmd = Nothing
ds = Nothing
End Try
End Function

It works with SQL Server but not with access :( I don't want to put
another line of code here. I want to do all in my "defined query" in MS
Access. Is there a way?

Thanks,

Deep Halo

Jan 4 '06 #3

You're not comparing like with like.

A stored procedure in SQL Server is more like a procedure (Sub or Function)
in VB or VBA. It is in no way comparable to an Access Query (which is more
like a SQl View).

You will neeed to do as Tom suggests and build the SQL dynamically, or
change the SQL property of the Access query.
--
Terry Kreft

"DeepHalo" <de******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Here is my SQL Server SP:

CREATE PROCEDURE MyQuery @TABLENAME VARCHAR(10) AS EXEC('SELECT * FROM
' + @TABLENAME + '')

and the function I use with it:

Private Function RunQuery(ByVal tableName As String) As DataTable
Dim da As IDbDataAdapter
Dim cmd As IDbCommand
Dim ds As New DataSet
Dim dt As DataTable

Try
cmd = myDataProvider.ERCommand("MyQuery")
cmd.Connection = myDataProvider.ERConnection
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(myDataProvider.ERParameter("TAB LENAME"))
cmd.Parameters(0).Value = tableName

da = myDataProvider.ERDataAdapter
da.SelectCommand = cmd
da.Fill(ds)
dt = ds.Tables(0)

RunQuery= dt
Catch ex As Exception
Call ErrorControl(ex, "Error:RunQuery")
Finally
da = Nothing
cmd = Nothing
ds = Nothing
End Try
End Function

It works with SQL Server but not with access :( I don't want to put
another line of code here. I want to do all in my "defined query" in MS
Access. Is there a way?

Thanks,

Deep Halo

Jan 4 '06 #4
Thanx for your replies, I've decided to create a dll to do this work.

Deep Halo

Jan 5 '06 #5
Terry Kreft wrote:
You're not comparing like with like.

A stored procedure in SQL Server is more like a procedure (Sub or Function)
in VB or VBA. It is in no way comparable to an Access Query (which is more
like a SQl View).

You will neeed to do as Tom suggests and build the SQL dynamically, or
change the SQL property of the Access query.

I think you need to add the @-sign with the parameter name in MS Access

PS. When i've used ADO, this was a shorter way of adding parameters. I
don't know if it works with the IDbCommand:
cmd.Parameters("@TABLENAME") = tableName
Jan 5 '06 #6
You don't need to use the @ sign with parameters in Access.

I think you're missing what the OP is trying to do.

--

Terry Kreft
"Stevel" <stevenlangenaken-at-@hotmail-dot-.com> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
Terry Kreft wrote:
You're not comparing like with like.

A stored procedure in SQL Server is more like a procedure (Sub or Function) in VB or VBA. It is in no way comparable to an Access Query (which is more like a SQl View).

You will neeed to do as Tom suggests and build the SQL dynamically, or
change the SQL property of the Access query.

I think you need to add the @-sign with the parameter name in MS Access

PS. When i've used ADO, this was a shorter way of adding parameters. I
don't know if it works with the IDbCommand:
cmd.Parameters("@TABLENAME") = tableName

Jan 5 '06 #7
No, this is a different provider which can support various databases,
so I don't need to pass any prefixes.

As I said I'll use dll to this and started write it already it's going
well :)

So thnx for all.

Deep Halo

Jan 5 '06 #8
1. You also said:
"but not using any programming
language as Java, VB, C# ...."
Can we assume that you are creating your DLL completely and only from
DAO/JET?

2. Your DLL; what properties and methods will it have that DAO does not
have? How will it differ from DAO?

Jan 5 '06 #9
I was thinking to do this without writing any language except SQL or
SQL based languages because I don't want to write different code for
different databases.

Private Function RunQuery(ByVal tableName As String) As DataTable
Dim da As IDbDataAdapter
Dim cmd As IDbCommand
Dim ds As New DataSet
Dim dt As DataTable

Try
cmd = myDataProvider.ERCommand("MyQuery")
cmd.Connection = myDataProvider.ERConnection
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(myDataProvider.ERParameter("TAB LENAME"))
cmd.Parameters(0).Value = tableName

da = myDataProvider.ERDataAdapter
da.SelectCommand = cmd
da.Fill(ds)
dt = ds.Tables(0)

RunQuery= dt
Catch ex As Exception
Call ErrorControl(ex, "Error:RunQuery")
Finally
da = Nothing
cmd = Nothing
ds = Nothing
End Try
End Function

This is a sample of my function. I cant call queries from different
databases for this function because I have to support MS Access as well
:( myDataProvider is our own data provider and it supports different
databases so dont be confused about code. I just cant do the same
things using defined queries in MS Access that I can do in SQL Server,
Oracle. So I've decided to create a dll using a programming language
that we use for our projects.

I'm sorry that I cant give you detailed answer about the dll but it's
going to use our DataProvider and it will create dynamic tabbed queries
for any database. I'm just trying to reduce the lines of code.

Jan 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Billy Cormic | last post by:
Hello, I am interested in dynamically creating temp tables using a variable in MS SQL Server 2000. For example: DECLARE @l_personsUID int select @l_personsUID = 9842
3
by: Craig Jurney | last post by:
Am having difficulty creating a dynamic <select> element using direct assignment to the element's option array (ie. someElement.option=new Option(someText, someValue);) that will work on Palm...
5
by: Mike | last post by:
I am writing a .NET application in C# that uses Crystal Reports. I want the crystal reports to grab information from a database no matter where the database is located. To do this, I want to...
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
2
by: monkeydragon | last post by:
#define MAX_TABLE = 1024; BYTE* dynamic1D = new BYTE; later.. i want to create a dynamic 2d ARRAY like this: >] >] >]
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
7
by: Varangian | last post by:
Hello is creating controls at runtime the same as designtime ? will a page be faster if controls are created at designtime rather than at runtime ? If so why ? thanks :)
16
by: pukivruki | last post by:
hi, I wish to create a temporary table who's name is dynamic based on the argument. ALTER PROCEDURE . @PID1 VARCHAR(50), @PID2 VARCHAR(50), @TICKET VARCHAR(20)
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.