473,804 Members | 3,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11482
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",,,,,s ql 'I hope that was enough commas

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

* Set the SQL property of an existing Querydef
dim sql as string
sql = "select * from SomeTable"
Currentdb.Query defs("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("MyQu ery")
cmd.Connection = myDataProvider. ERConnection
cmd.CommandType = CommandType.Sto redProcedure

cmd.Parameters. Add(myDataProvi der.ERParameter ("TABLENAME" ))
cmd.Parameters( 0).Value = tableName

da = myDataProvider. ERDataAdapter
da.SelectComman d = 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.goo glegroups.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("MyQu ery")
cmd.Connection = myDataProvider. ERConnection
cmd.CommandType = CommandType.Sto redProcedure

cmd.Parameters. Add(myDataProvi der.ERParameter ("TABLENAME" ))
cmd.Parameters( 0).Value = tableName

da = myDataProvider. ERDataAdapter
da.SelectComman d = 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" <stevenlangenak en-at-@hotmail-dot-.com> wrote in message
news:11******** *******@seven.k ulnet.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("MyQu ery")
cmd.Connection = myDataProvider. ERConnection
cmd.CommandType = CommandType.Sto redProcedure

cmd.Parameters. Add(myDataProvi der.ERParameter ("TABLENAME" ))
cmd.Parameters( 0).Value = tableName

da = myDataProvider. ERDataAdapter
da.SelectComman d = 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
15607
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
1613
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 devices runing Blazer 3.0 or Web Browser 2.0. The browsers SEEM to be JavaScript1.1+ capable and the script that I have works on every desktop-based browser, but not the PDA ones.... Any advice? Or is this a known deficiency?
5
6105
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 create an ODBC connection to the database at runtime in order for the report to grab data from the database. Does anyone have a code snippet that shows how to do this? Thank you very much for your reply. Mike
2
4573
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 easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
2
5426
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
2838
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. Everything is the default settings I believe. IIS is running under Local System. In IIS the DefaultAppPool is running under Network Service. Annonymous access uses the account IUSR_JASMINE (machine name is Jasmine).
7
2233
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
10331
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
3632
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 to create the dynamic controls in the OnInit stage of the lifecycle. Since data from static controls is not yet available in the OnInit stage I can't know what dynamic control I have to create.
13
12805
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 scripting language that makes writing automated tests simpler. Really, I'm looking to kind of abstract the power of the C# language into a simpler language that's easier to learn. The script files would be interpreted by a script interpreter...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.