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

Distinct Recordset SQL as Loop Variable

Hey Guys,

I'm just playing around with some code I wrote for work and am trying
to minimize the coding as much as possible.

So two questions:

1. What are your best practices for organizing your code, aside from
comments? I typically split my code up into sub procedures and then run
them all from the top of the code. My problem is that my code page is
taking me a while to scroll through and find the code i'm looking for.

2. I have two queries. The first creates a recordset of unique values
of a single field. The second uses that list of unique values as a
variable to loop through another recordset and pull the correct
records. Is there away to write some SQL that would combine these two
queries.

Private Sub Tabs()

Dim db As DAO.Database
Set db = CurrentDb()

db.Execute _
"SELECT DISTINCT[Customer Group] FROM [Customers];", _
dbFailOnError

End Sub
__________________________________________________ _____________________
I hacked this up for privacy, hopefully you can make sense of it! :)

Private Sub Export()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim XLS As DAO.QueryDef
Dim rs1 As String
Dim SQL As String

Set db = CurrentDb()
Set rs = db.OpenRecordset("Tabs", dbOpenSnapshot)

Do Until rs.EOF

rs1 = rs.Fields("Customer Group")

SQL = etc.

Set XLS = db.CreateQueryDef(rs1, SQL)

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8,
rs1, "c:\test\loss.xls", True

db.QueryDefs.Delete rs1

rs.MoveNext

Loop

rs.Close
XLS.Close

End Sub

Thanks guys, hope that clear enough

Jan 26 '07 #1
1 6075
>>
...
rs1 = rs.Fields("Customer Group")

SQL = etc.

Set XLS = db.CreateQueryDef(rs1, SQL)
...
<<

SQL = etc. must be the 2nd query. Could you post what that second
query looks like - then we can combine it with the first query. The
combining will looke something like this:

Select ... From tblX t1 Join tbly t2 On t1.ID = t2.ID
Where exists
(SELECT DISTINCT[Customer Group] FROM [Customers] t3 Where t3.ID =
t1.ID)

This will return all the rows for each distinct Customer group in one
shot. The query above will only work if the primary table and the Where
Exists table has a primary key field (which in this case I called ID).

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Jan 26 '07 #2

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

Similar topics

4
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use...
8
by: dmiller23462 | last post by:
My brain is nuked....Can anybody tell me right off the bat what is wrong with this code? Along with any glaring errors, please let me know the syntax to display a message (Response.Write would be...
8
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
2
by: Rhanda Zak | last post by:
Hi NG! I just managed copying data from one table to another table (boah, what a big deal :). But there are still some problems: 1) I want to use the keyword "BETWEEN" in combination with...
1
by: Patrick.O.Ige | last post by:
I have a xml file and i want to format it using XSL My XSL file and XML below I needed to do a distinct which is ok on the first node "Code" For the "programDescription" i did below which gets the...
23
by: PW | last post by:
Hi, I'd like to close a recordset and set the database to nothing if a recordset is open if an error has occured. Leaving a recordset open and a database open isn't a good idea, right? ...
4
by: =?Utf-8?B?R3JlZw==?= | last post by:
My background is in MS Access and I am in the process of migrating my skillsets to Visual Basic.Net. So far I am able to display data in DataGridViews and Combo Box, etc with not problem. What I'm...
5
by: bittman | last post by:
I have code as follows: Dim conn As New ADODB.Connection Dim rst As New ADODB.Recordset conn.ConnectionString = "Provider=MSDAORA;Password=tr7025ds;User ID=system;Data Source=WIN10" conn.Open...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...

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.