473,394 Members | 1,734 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,394 software developers and data experts.

Dynamic Query in Ms-Access

Hi, I'm trying to create a dynamic query in MS-Access. I have a table
with 3 fields and my dynamic query will be constructed with those 3
fields. Since I haven't done this before, I searched for help in this
forum and tried to use some, but still I'm getting errors. Can anyone
help me with this?

Find below the code which I'm using.

Sub Defaults()

Dim sql As String
sql = "select count(*) from " & Default_value.Table_name & " where" &
Default_value.Field_Name & "=" & Default_value.Default
CurrentDb.Querydefs("SomeQuery").sql = sql

End Sub

Thanks
Arun

Jun 13 '06 #1
2 16600
Hi Arun,

Here is something you can try that is a little bit simple - for
starters. Then you can use the values contained in textboxes to get
table/field names dynamically.

----------------------------------------------

Private Sub Command3_Click()
Dim qd As DAO.QueryDef, db As DAO.Database

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.Name = "test1" Then
db.QueryDefs.Delete qd.Name
Exit For
End If
Next
Set qd = db.CreateQueryDef("test1")
qd.SQL = "select fname, lname, num from tbl1"
DoCmd.OpenQuery "test1"
End Sub

--------------------------------------------
this example is using a tbl called "tbl1" which contains the fields
fname (text), lName (text) num (number). To implement dynamically - you
can do this
------------------------------------------------

Private Sub Command3_Click()
Dim qd As DAO.QueryDef, db As DAO.Database

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.Name = "test1" Then
db.QueryDefs.Delete qd.Name
Exit For
End If
Next
Set qd = db.CreateQueryDef("test1")
qd.SQL = "select " & txtFname & ", " & txtLname & ", " & txtNum & " from
" & txtTblName
DoCmd.OpenQuery "test1"
End Sub

----------------------------------------------------

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Jun 13 '06 #2
Thanks Rich, It helped a lot.
Rich P wrote:
Hi Arun,

Here is something you can try that is a little bit simple - for
starters. Then you can use the values contained in textboxes to get
table/field names dynamically.

----------------------------------------------

Private Sub Command3_Click()
Dim qd As DAO.QueryDef, db As DAO.Database

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.Name = "test1" Then
db.QueryDefs.Delete qd.Name
Exit For
End If
Next
Set qd = db.CreateQueryDef("test1")
qd.SQL = "select fname, lname, num from tbl1"
DoCmd.OpenQuery "test1"
End Sub

--------------------------------------------
this example is using a tbl called "tbl1" which contains the fields
fname (text), lName (text) num (number). To implement dynamically - you
can do this
------------------------------------------------

Private Sub Command3_Click()
Dim qd As DAO.QueryDef, db As DAO.Database

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.Name = "test1" Then
db.QueryDefs.Delete qd.Name
Exit For
End If
Next
Set qd = db.CreateQueryDef("test1")
qd.SQL = "select " & txtFname & ", " & txtLname & ", " & txtNum & " from
" & txtTblName
DoCmd.OpenQuery "test1"
End Sub

----------------------------------------------------

Rich

*** Sent via Developersdex http://www.developersdex.com ***


Jun 26 '06 #3

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

Similar topics

0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
2
by: george_Martinho | last post by:
I'm having a problem in getting a dynamic query in a sp working and the code is below so can someone please help me? It would be very much appreciated. Thanks Girogio ...
2
by: chets | last post by:
Hi All, I am facing problem in executing one dynamic query in PRO *C program on linux. I want to update table mytable by data MADURAI for a column mycolumn1 where primary key is myPK.I want to...
2
by: kalaivanan | last post by:
hi, how far dynamic query built inside stored procedures is efficient than normal queries inside stored procedures. does dynamic query have advantage over ad hoc queries and in what way. kalai...
1
by: khushbubhalla | last post by:
Hi, I am writing a dynamic query in a procedure. the query returns a set of rows and cols..since its dynamic the no of columns keep changing thats why i cannot store the result using INTO. Can...
2
by: tarunsanju15 | last post by:
Hi, I am writing a dynamic query in which i have to select a records depend upon the no. of values entered by user like: select * from _table where _tableColumn IN (values entered by user) the...
0
by: emccormick | last post by:
Hi, Using BIDS / Report Designer with SSRS / SQL2005(SP2) and for one dataset, I'm trying to make a CTE into a 'dynamic' query by wrapping the accessor query in an Iif() function. Here it is: ...
1
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
i have a GridView which i can Page on (go to page 2,10,...) the thing is that i have a dynamic query which set the GridView from starts on page 1. the thing is that when i start to go over the...
11
by: julie.siebel | last post by:
I'm working on a rather complex booking system for building European trips, in a combination of SQL/VBScript/Javascript. There are tons of query string variables that get passed back and forth...
2
by: Mucahit ikiz | last post by:
I cant make a full dynamic query in LINQ I have 2 situation methods (only_exp_query, only_tbl_query) those are working. .... using System.Linq.Dynamic; using System.Data.Linq; .... string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.