SELECT Queries In VBA In Access

Dec 12, 2005

I've been wracking my brain and scouring the net for information on how to do SELECT queries in Access and I can't figure it out or find correct information. Some idiot had a website that said to use DoCmd.RunSQL to do SELECT queries, but failed to mention that only ACTION queries can be used in that method, so I'm back at ground zero. What I want to do is this:

I want to be able to run a select query from a table, and store all the information in some sort of object so that I can access all the individual fields returned in the query and do things with them. Is there anyway to do what I'm asking for?

View Replies


ADVERTISEMENT

Queries :: Select Largest Value Which Is Smaller Than Another Value (Access 2007)

Jun 4, 2014

I have a table of logged entries. Each record has a date field (ValueDate) and an account identifier field (AccountID)

I also have a table of rates. Each record has the same account identifier field (AccountID), a date field (EffectiveDate) and a rate field (BankRate)

Entries can be logged for any given ValueDate. But there may or may not be a corresponding EffectiveDate in the rates table.

I need to write a query that will return all of my logged entries and the largest EffectiveDate which is on or before the ValueDate (as well as the BankRate corresponding to that EffectiveDate)

This is as far as I've gotten but it returns multiple records for each logged entry. I need one record per logged entry.

Code:
SELECT tblLoggedEntries.EntryID, tblLoggedEntries.AccountID, tmp.BankRate, MAX(tmp.EffectiveDate) AS EffectiveDate
FROM tblLoggedEntries
LEFT JOIN
(SELECT tblRates.AccountID, tblRates.BankRate, tblRates.EffectiveDate
FROM tblRates) AS tmp ON tblLoggedEntries.AccountID = tmp.AccountID
WHERE tmp.EffectiveDate<=tblLoggedEntries.ValueDate
GROUP BY tblLoggedEntries.EntryID, tblLoggedEntries.AccountID, tmp.BankRate

View 11 Replies View Related

Queries :: Select Query To Gather Results Of Other Select Queries

May 11, 2014

I'm fairly new to Access. 's various select queries containing useful and useless results. I want to create a select query that will pick out all the useful figures into a 1 row table that can then be pasted into Excel.

e.g Existing Select Query 1 returns 1 row showing Average Age, Average Price, Total rainfall
Existing Select Query 2 returns 1 row showing Average Weight, Average Salary, Total snowfall
Existing Select Query 3 returns *2* rows: It returns Distance from London, Hours daylight and population for Town A and Town B

I want a select query that returns 1 row showing (6 items):

Total rainfall, Total snowfall, Town A Distance from London, Town A Population, Town B Distance from London, Town B Population.

I've been able to handle getting Total rainfall and Total snowfall. But I cant figure out how to get Town A Distance from London, Town A Population, Town B Distance from London, Town B Population to appear in the same row of the same query results as Total rainfall, Total snowfall.

View 3 Replies View Related

Queries :: Prepare Csv For Access Table Import Using Select Query

Oct 26, 2014

I have generated two queries called "qry_GetWellName_GetAPI" and "qry_GetWellProduction" that extracts all the data I need from a csv to make two tables.

My issue is that I can not gather all the production data into a query formatted properly for a make table query that contains production history by month / api number.

Ideally the data would be placed in a table called tblProductionHistory with the following unique table index --> [api_fk] [data date]. The api_fk would link to the table that was created by extracting well names using qry_GetWellName_GetAPI.

tblProductionHistory field structure I am trying to achieve:

[api_fk]--[data date]--[oil rate]--[gas rate]--[water rate]--[days produced]--[cumulative oil]--[cumulative gas]

Currently the qry_GetWellProduction shows each well name and the year it had production as a separate record from the month names and the month's production values. I need to consolidate the data so each record can be matched to the unique table index.

Is there a sub query I need to use to manufacture a data date for each month that includes the year and last day of that month for that year? Also would I need a separate sub query to match each month's production to the api number?

View 1 Replies View Related

Queries :: Making A Table From A Select Query In Access 2013

Jan 21, 2014

I have made a new access 2013 database. I have created a linked table that has imported a substantial amount of data from an external data source, (an Excel spreadsheet). So far no problem. I created a select query that plucked data from the original table mentioned. Again, no problem. Then I decided to create another table, using certain fields only from the select query. Microsoft's guide tells me to start with CREATE, then Table design. I am happy to use just 4 fields from my query, but what I keep ending up with is a table, that, when I double click on it gives me the following:

ID Field1 Field2 Click to add
(New)

It is presumably expecting me to enter an ID number and it will come up with some record, but I want a complete table that should show several hundred records.

View 1 Replies View Related

Queries :: Select All Dates Since A Given Date Without Referencing A Table? (Access 2007)

Apr 23, 2014

Is it possible to create a query to select all dates from a given reference date? I don't mean all dates in a table - I mean all dates generally?

(The idea being to fill the first field in the resultant dataset with the list of dates, then run subqueries off that to fill the remaining calculated fields)

I'm currently using a date field in one of my tables to populate this first field (the full SQL is in a separate thread here)

But that was just a convenient way of getting a list of dates; the dates in that table don't actually have any significance to the resulting dataset (other than they should roughly overlap with the dates I'm looking for)

The flaw in that method is that the table from which I get those dates can only ever have dates up to and including yesterday. I also need to get today's date in there (and calculate the subqueries based on that date as well).

It's also possible - although unlikely - that there could be random dates missing from that table as well - in which case I need to plug those gaps and calculate my fields for those missing dates as well.

For clarity; that first field (AsOfDate) should contain every weekday from the earliest date in that table (i.e. Min([tblBalances].[BalanceDate]) up to and including today. It doesn't matter if any of the dates inbetween are missing from tblBalances as the subqueries will just return zeroes for those dates (which is exactly what I want to see).

View 3 Replies View Related

Queries :: Access 2007 - Select All Dates Between Two Dates?

Apr 9, 2015

I have a table of records, which has within it two date fields (effectively, a 'start' and 'end' date for that particular record)

I now need to create a query to perform a calculation for each date between the 'start' date and the 'end' date

So the first step (as I see it anyway) is to try to create a query which will give me each date between the two reference dates, in the hope that I can then JOIN that onto another query to perform the necessary calculation for each of the returned dates.

Is there a way to do this?

So basically, if for a particular record, the 'start' date is 01-Apr-2015 and the 'end' date is 09-Apr-2015, can I produce a dataset of 9 records as follows :01-Apr-2015

02-Apr-2015
03-Apr-2015
04-Apr-2015
05-Apr-2015
06-Apr-2015
07-Apr-2015
08-Apr-2015
09-Apr-2015

(The *obvious* solution would be to create a separate table of dates, from which I could just SELECT DISTINCT <Date> Between #04/01/2015# And #04/09/2015# - but that seems like a dreadful waste of space, if that table is only required to generate the above? And it would have to cover all possible options; so it would either have to be massive, and contain every possible date - ever! - or maintained, adding new dates as necessary when they are required. Seems horribly inefficient!)

Is it possible to just select each date between the two reference dates? Or can you only query something which exists somewhere in a table?

View 4 Replies View Related

Forms :: Multi Select Listbox Access 2003 Code Gives Syntax Error In MS Access 2010

Oct 24, 2013

I've been using the following code successfully in Access 2003 & now I need to migrate to Access 2010. The purpose of the code is to use the items that the user selects in the list box to build the criteria of a query. Access 2010 keeps giving me a syntax error when I try to run the query & I don't know why:

My code is:

On Error GoTo Err_Command151_Click

' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String

[Code] .....

The syntax error I get in Access 2010 is:

Syntax Error in query expression 'SELECT * FROM
qryContractListSummarybyDateContract3TYPEBREAK WHERE
qryContractListSummarybyDateContract3TYPEBREAK.Rep ortableName IN('Adbri
Masonry NSW');'

View 12 Replies View Related

Queries :: SELECT All But The Top Row

Sep 10, 2013

I've taken over a database written in Access '97 so it's quite old and I don't want to rewrite the whole thing. I have a query as the record source for a report like this:

Code:
SELECT TOP 1 ID1Child5.ID, ID1Child5.CODE1, ID1Child5.CODE2, ID1Child5.CHARGE, ID1Child5.DEFAULTS FROM ID1Child5 WHERE (((ID1Child5.ID)=[Forms]![ID1MainForm_0]![ID])) ORDER BY ID1Child5.DEFAULTS;

Which gives me the first value.

Now I need to reverse that and create a query that gives me all the other results. Basically everything except the top row.

View 1 Replies View Related

Case Select To Run Queries

Oct 27, 2005

Please help with the code below.

I am trying to select a different query based on the selection in the [Product] Field. I have 4 different products and 3 different queries to run. Each product is specific to a particular query.

What I would like is:
When Product 3 is chosen "Query 1" is ran.
When Product 1 is chosen "Query 1" is ran.
When Product 2 is chosen "Query 2" is ran.
When Product 4 is chosen "Query 3" is ran.

As it is set up now it appears that when choosing Product 1 I open Query 2 and when choosing Product 2 I open Query 1, but it is the opposite of that. I get the correct query with this code by choosing Product 1 and the Query that opens is actually Query 1, I don't know what is going on here.

When I open Products 3 & 4 it always opens Query 2, no matter what Query I specify.

Please help me sort this out. I am not very familiar with case select. Thanks

Private Sub run_query_Click()
On Error GoTo Err_run_query_Click

Dim stDocName As String
stDocName = "Query 1" (Should be for Product 1 & 3)
stDoc1Name = "Query 2"(Should be for Product 2)
stDoc2Name = "Query 3" (Should be for Product 4)Select Case stProtocolName
Case [Product] = "Product 1": DoCmd.OpenQuery stDoc1Name, acNormal, acEdit

Case [Product] = "Product 2": DoCmd.OpenQuery stDocName, acNormal, acEdit

Case [Product] = "Product 3": DoCmd.OpenQuery stDoc1Name, acNormal, acEdit

Case [Product] = "Product 4": DoCmd.OpenQuery stDoc2Name, acNormal, acEdit

Exit_run_query_Click:
End Select
Exit Sub

Err_run_query_Click:
MsgBox Err.Description

View 2 Replies View Related

Queries :: Select All Rows Except Last

Jan 22, 2015

How I might select all the records within a query except for the most recent one? I have an AutoNumber ID field and a date stamp to use , but I can't figure out how to exclude just the biggest record.I am trying to run an update query on the After Insert Event, so that when the user inserts a new record it will mark down similar older records as 'superceded'.

At the moment I can select all the records, and I can select just the largest one, but I can't seem to take one away form the other. Here is the SQL in it's current state:

Code:

UPDATE [Personnel-Qualifications] SET [Personnel-Qualifications].[Superceded?] = True
WHERE ((([Personnel-Qualifications].PersonnelID)=[Forms]![Personnel]![PersonnelID]) AND (([Personnel-Qualifications].QualificationID)=[Forms]![Personnel]![Child21]![qualificationID]));

View 1 Replies View Related

Queries :: Select Statement To Max Value

Jul 12, 2013

I'm trying to do a select statement and put it in a variable which i can then output to a text box. How do I get the value into a variable? I can't seem to get my syntax right

This is what I currently have

Maxvalue = "SELECT MAX[Record Num]FROM Joblog"

View 2 Replies View Related

Queries :: Select Query From Another Mdb?

Mar 20, 2013

are you able to specify the location of another database that you want to select data from?

For example: I want to do something like this:

SELECT [field1], [field2] from C:DesktopFolderMyOtherDatabase.mdbTableName

View 4 Replies View Related

Queries :: Either Or Select Distinct Row

Aug 12, 2014

Is it possible to change this query to search for either a Name or by an employee ID number? The query runs a report on after update. Currently I can only enter the name not the employee ID

Code:
SELECT DISTINCTROW First(Inventory.Employee) AS FirstOfEmployee, Inventory.[Employee ID]
FROM Inventory GROUP BY Inventory.[Employee ID] ORDER BY First(Inventory.Employee);

View 6 Replies View Related

Select And Append Queries... At The Same Time?

Oct 10, 2007

I've done some searching, and haven't found any threads that match what I am attempting...

I have a database that tracks expenses that are incurred under different ongoing projects. I currently have a report based on a query that asks the user for the project number, and shows all the expense invoices that have not been paid for that project. This report is basically an internal invoice that gets assigned to a department, and so it is assigned an internal "invoice" number. Currently, the user types in this internal number before the report is generated and it is displayed on the report for printing purposes.

Here's the tricky part (for me anyways): I would like to be able to filter the invoice records by project number, and then append the user-entered internal invoice number to the applicable records. There is already a field in the table for this data, and it is currently being entered on individual records after the report is printed. My goal is to be able to do this all in one step.

Any help will be greatly appreciated... Thanks guys!

View 14 Replies View Related

Populating Combo Box With Certain Select Queries

Jan 7, 2008

I have a form that has a combo box that contains every query in my database. This worked fine until i started having to create cascading queries to return the data I want. How can i populate my combo box with only certain queries in my database.

This is the code I was using to put all queries into my combobox.

FROM MSysObjects
WHERE MSysObjects.Name NOT LIKE "~*" AND
MSysObjects.Type = 5
ORDER BY MSysObjects.Name;

Upon clicking a button or clickin the query in the list, I want the query to run......So the main question is how to I include certain queries in my combo box but not all of them? I do not know SQL or any programming, so as specific as possible will help immensely. Thank you everybody.

View 1 Replies View Related

Queries :: Variable In A Select Query?

Jul 10, 2013

Im trying to pass a list box variable in a select query. I understand you cannot pass a variable directly but have to pass it through a function. I may be wrong in this, but whatever I do I cannot get it to work. Here's my code:-

Public Sub GetEquipment()
List387.RowSourceType = "Table/Query"
List387.RowSource = "SELECT findequipstr() FROM Equipment"
End Sub

Public Function findequipstr() As String
If IsNull(List371.Value) Then GoTo function_end
findequipstr = List371.Value
function_end:
End Function

If I MsgBox(findequipstr()) within my Getequipment function, the variable is messaged,

View 3 Replies View Related

Queries :: How To Select Multi Parameters

May 28, 2014

have a query that works fine when I have to select one parameter, however I don't know how to select multi parameters...In this query I would like to able to select as well as the specific 'POSTITION' value Also 'BASE' and all those parameters that have a check box empty or full.

Code:
SELECT tblCrewMember.StaffNumber, tblCrewMember.Surname, tblCrewMember.Name, tblCrewMember.Position, tblCrewMember.Base, tblCrewMember.Nationality, tblCrewMember.StartingDate, tblCrewMember.Resined, tblCrewMember.ResinedDate, tblCrewMember.Birthday, tblCrewMember.IDCrewMember, [GroupBy] AS Expr1
FROM tblCrewMember
WHERE ((([GroupBy])=[Position]))
ORDER BY tblCrewMember.StaffNumber;

View 2 Replies View Related

Queries :: Command To Select First 30 Characters

May 7, 2013

I have the command to select first 30 characters in a simple select query.

View 3 Replies View Related

Queries :: How To Select Individuals With More Than One Skill

Jan 2, 2014

How do I select individuals who have, for example, both access and excel skills? I have a table of candidates and child table with the list of skills.

I have a form (called "search" at the moment) with a subform (called "skillslist subform"). The subform's record source is an empty table called "searchlist" with a single combo box using the master list of skills as the source. This is so I can select the skills I am looking for.

Then there is a button that (1) saves the records and then (2) opens the query, as follows:

Code:

SELECT SkillList.CandidateID, searchlist.searchlist
FROM SkillList INNER JOIN searchlist ON SkillList.Skill = searchlist.searchlist
ORDER BY SkillList.CandidateID;

Is this problem need to be solved with a subquery?

View 13 Replies View Related

Queries :: Totals In A Select Query

Jul 28, 2014

After you "group by", is there a way to have the next field presented without a sum or avg etc.. if I know there is only ever one value to present?

View 1 Replies View Related

Queries :: SELECT TOP 3 Returning More Records

Mar 24, 2015

My statement below is current returning 4 records. Two of the records have the same GBPAmount value.

RequestID is the Primary Key

Code:
SELECT TOP 3 RequestID, GBPAmount, Currency, RequestDate
FROM PayRequest
WHERE (((Currency)="CAD Canadian Dollar") AND ((RequestDate)>#11/16/2014#))
ORDER BY GBPAmount;

Code:
RequestID GBPAmount Currency RequestDate
10207 8.17 CAD Canadian Dollar 03/02/2015
9874 33.82 CAD Canadian Dollar 20/01/2015
11327 109.58 CAD Canadian Dollar 23/02/2015
10495 109.58 CAD Canadian Dollar 05/02/2015

View 3 Replies View Related

Queries :: Select First Record Of Each Year

Aug 28, 2014

How can I select the first record of each year working with a table like this:

Code:
ID, value, date
0, 30, 01/01/2000
1, 40, 03/02/2000
2, 20, 10/03/2000
3, 10, 02/05/2001
4, 20, 09/08/2001
5, 10, 01/02/2001

I'd like to get this result from my Query:

Code:
30, 01/01/2000
10, 01/02/2001

View 2 Replies View Related

Queries :: Get A Query To Select All Records?

May 3, 2014

I'm having a problem get a query to select all of the records it should be.

When I filter the source table (200_STANDARDIZED NRGL) to show the data I want to see (PC2 = 6000; GAAP = 02; CGL = 0950, 2735, 2736, 3500 and 3501; STD VENDOR NAME = blanks), I get 33 records.

NOTE: Had to take the PC2 records that were not '6000' out of the dB I've attached in order to be able to send dB bu the PC2 filter is needed in the complete dB.

When I create a select query to the do the same thing, I get either:3 records (when I set STD VEND NAME to Like '') or 30 records (when I set STD VEND NAME to NOT Like '*' ) I've attached the dB - Query 1 is the subject of bullet 1 above and Query 2 is the subject of bullet 2 above.

BTW, the three records that show up in Query 1 are the same records missing from Query 2.

View 3 Replies View Related

Queries :: Select Records With Minimum Value

Mar 12, 2015

I would like to select records based on Minimum values of specific vendor.

Example:

MasterItemID Price PriceDate VendorID
1 2 2/5/14 30
1 3 2/5/14 31
1 7 2/5/14 32
2 3 2/5/14 31
2 2 2/5/14 32

So wants the all columns where price is minimum

Example: output required

MasterItemID Price PriceDate VendorID
1 2 2/5/14 30
2 2 2/5/14 32

View 6 Replies View Related

Queries :: Select Query With Different Values Only

Aug 14, 2015

I Want to Create an Query to Find Different Values in A Table/ Query

Like
Month Year Name School Post
Jan 2012 Ankur School 1 Post 1
Feb 2012 Ankur School 1 Post 1
Mar 2012 Ankur School 2 Post 1
Apr 2012 Ankur School 2 Post 2
May 2012 Ankur School 2 Post 2
June 2012 Ankur School 3 Post 2

Now i want in result of Query is only like

Month Year Name School Post
Jan 2012 Ankur School 1 Post 1 (Starting Ledger)
Mar 2012 Ankur School 2 Post 1 (Change in School)
Apr 2012 Ankur School 2 Post 2 ( Change in Post)
June 2012 Ankur School 3 Post 2 (Change in School)

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved