473,406 Members | 2,707 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,406 software developers and data experts.

Access and Dynamic SQL

23
(I'm using Access 2000 and ADO)

This is probably a daft question, I'm used to programming where I make everything msyelf- can't quite get sued to Access wanting to do things for me!

I have an unbound form, that creates a dynamic SELECT statement, but then what do I have to do with that sql string to get the results shown in a query datasheet or report?

I've been using ADO, and it connects to the database and appears to run the query, but I have no idea what to tell it to do with the recordset it returns!

would i be better off scrapping it and changing to DAO? Or am I miles off in how I think about Access?

Thanks in advance.
Charli
Apr 22 '09 #1
6 3349
DonRayner
489 Expert 256MB
Hi Charli,

Can you let us know what you want to accomplish with the recordset that you have returned. And if you could include the code that you are using that would be helpful as well.

Don
Apr 22 '09 #2
Delerna
1,134 Expert 1GB
Hi, you sound like you are used to opening recordsets and displaying the results into a table on a web page.

In an access form you don't use ADO (or DAO for that matter) in VBA code to display data on that form in that way. You need to bind the form to a tabledef but preferably to a querydef and not a tabledef.

You could use vba to change the recordsource property of the form
Expand|Select|Wrap|Line Numbers
  1. me.recordsource="Select field1,field2 from Table1"
  2. me.requery
  3. me.refresh
  4.  
That won't work properly though if the number of fields or the field names change because the form needs controls on it that are bound to the fields you are selecting. Having said that you could dynamically create and bind the controls on the form, yuk! Access is not really ideal for that sort of thing.
You generally bind a form and its controls to a query and then that form never changes in the way you seem to be talking.

Another possibility
You could use VBA code to create a new querydef in your database using your dynamic query as the code for the querydef.
Then all you would need to do is open the new querydef from VBA.
I don't know if this idea will work for you but here is an example using DAO

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.    Dim db As database
  3.    Set db = CurrentDb
  4.    sql = "Select * from table1"
  5.    db.QueryDefs.Delete "qryNew" 'Only do this if the query already exists
  6.    db.CreateQueryDef "qryNew", sql
  7.    DoCmd.OpenQuery "qryNew"
  8. End Sub
  9.  
Good luck.
Apr 23 '09 #3
charli
23
Easy to tell isn't it- I can work recordsets to the web and to Excel, just can't work out Access wanting to do things for me!

I have a completely unbound form, it doesn't display anything from the database- you just make selections as to how you would like the data (it basically builds up the WHERE clause of SQL, example: grouped by week/month/year)

I've managed to get a report to do the SQL-building (based on the open form), and change its own RecordSource, but then I have to repeat all the SQL-building code in every report which seems a horrible thing to have to maintain!

I cannot have a query that just has the form fields in as criteria- the query is a bit more complex than that (what you select is dependant on form fields too)

Essentially I want the recordset to be displayed on a report (or an open query datasheet would do), but what is selected and what is in the where clause (and how it is grouped) can change a lot.

Easiest way may be to make excel talk to the access database? Then i can just throw SQL at it and collect the results! I just can't seem to think like Access does.

Is there an ADO equivalent to the DAO querydef? I'd prefer not to use DAO as this will be migrated to SQL Server (eventually, maybe, one day)
Apr 23 '09 #4
charli
23
Worked the DAO->ADO thing out, here is the code to make a querydef in ADO

Expand|Select|Wrap|Line Numbers
  1. Dim cat As New ADOX.Catalog
  2. Dim cmd As New ADODB.Command
  3. cat.ActiveConnection = CurrentProject.Connection
  4.  
  5. On Error Resume Next
  6. cat.Views.Delete "qryFilter" 'deletes the query if it already exists, ignores errors if it doesn't
  7. On Error GoTo 0
  8.  
  9. cmd.CommandText = sql 'this should be your sql SELECT string
  10.  
  11. cat.Views.Append "qryFilter", cmd 'makes a new one
  12. cat.Views.Refresh 'this is meant to make it so you can view the new query in the query tab, it doesn't always work. however if you then go to manually create a new query, you can see 'qryFilter' has been created as its in the drop down
  13.  
  14. Set cat = Nothing
  15. Set cmd = Nothing
Apr 23 '09 #5
Delerna
1,134 Expert 1GB
If its only the where clases that are going to change then there is another possibility of achieving what you want without going down the dynamic query track.

In the query designer you can link the criteria for a field to a control on a form.
Apr 23 '09 #6
NeoPa
32,556 Expert Mod 16PB
When opening a Form or Report from code there is a parameter that allows you to specify a WHERE clause on the fly. If it is simply the criteria that may need to change, then this is a good way to handle that.
EG.
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.OpenForm(..., WhereCondition:="([TxtFld] Like '*New*') AND ([NumFld]<0)", ...)
The WhereCondition parameter is formatted like a SQL WHERE clause, except without the word WHERE. It is just as powerful and flexible.

Welcome to Bytes!
Apr 23 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
1
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
5
by: rob | last post by:
Hi to all. I am pretty new to using Access and am having a problem I hope someone can help me with. I want to access a MS-Access database from a web page. I have managed to get it "sort" of...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
8
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
2
by: Jim | last post by:
Lately, I've been seeing a lot of those dynamically created access code images on web forms. You know, some little jpg or gif image that has a random series of letters and numbers. The form says...
5
by: Dave | last post by:
I need to filter an Access 2000 result set in ASP 30 using the ADO recordset.filter. I build the filter in pieces. The first clause of the filter is this... WHERE word LIKE 'S%' ... to...
12
by: slinky | last post by:
Can an Excel spreadsheet or a section of one be embedded into an Access form and serve as a subform from which other parts of the Access form can get data? Thanks!
4
by: Bongard | last post by:
I have a dynamic range that I would like to use as a linked table into Access. The problem is that Access doesn't seem to want to to recognize the dynamic range when you click on "show named...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...

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.