Modules & VBA :: Getting Max From SELECT Query

May 13, 2015

I currently have 2 queries..

Query 1 select statement getting count of field

Query 2 getting max of Query 1

Instead of the queries I want to do this in vba with a recordset

Code:
' Select from first query
sql = "SELECT tblOriginal.ROUTE, Count(tblOriginal.ROUTE) AS CountOfROUTE " & _
"FROM tblOriginal " & _
"GROUP BY tblOriginal.ROUTE " & _
"ORDER BY Count(tblOriginal.ROUTE) DESC"
' Select max from result of first query
sql2 = "SELECT Max(qryRouteCount.CountOfROUTE) AS MaxOfCountOfROUTE " & _
"FROM qryRouteCount;"

The slq works fine for the first Query, I could then do a iMax = rs("CountOfROUTE") to get the max value as they are ordered.

but how can I write sql 2 so that it selects the Max from the Query 1 select statement.

View Replies


ADVERTISEMENT

Modules & VBA :: Passing TempVar To SELECT Query

Feb 25, 2014

Picture, if you will, a table with multiple fields, each of which contains the date of a certain action in the process tracked by each record. I have a standard report format that will be used to view the progress of the actions. I want to be able to sort the report - on demand by non-technical users - by whichever date field they choose.

I've created a form with a combo box that is linked to a table with the name of each field in the source table. A Macro has been configured to open the form when the report is opened, which will prompt for the selection of the sort field. That value is then captured into a TempVar.

When the user clicks OK control passes to a procedure in a module that executes a SELECT statement (SQL) that configures the Query that is used for report generation. The "ORDER BY" portion of the statement needs to be modified with the name of the sort field desired. If the statement is hard coded as, for example:

...ORDER BY Main.[Initiated Date] DESC;

it works fine. I want to use the value of the TempVar to provide the name of the date field (ex.: [Initiated Date]), but I can't determine the correct syntax to get the statement to accept it. Concatenating doesn't seem to work, and I've searched high and low for hints on the web. Here's one version of what I've tried (and which fails):

"...ORDER BY Main." & [TempVars].[SortParm] & ";"

The error returned by the above version is: "Object doesn't support this property or method"

(SortParm is the name of the TempVar, and it is correctly populating, and keeping, the value I need from the form.)

View 3 Replies View Related

Modules & VBA :: Select Query Returning String Instead Of Value

May 2, 2014

Why is strCreditCheck returning the actual query string and not the result? Of course, this is not working.

Code:
Private Sub b_PrintWorkOrder_Click()
Me.Requery
Dim strCreditCheck As String
Dim strCompany_id As Integer
Dim stlinkCriteria As String

[code]....

View 6 Replies View Related

Modules & VBA :: How To Create And See Results Of SELECT Query

Mar 22, 2015

I was beginning to think I had got the hang of creating queries on the fly in VBA. After several hours I have just learned that docmd.runSQL does not work for simple SELECT statements. I'm not going to worry about why that would be. I'm sure there's a jolly good reason.

So... I am able to dynamically create the SQL string for the SELECT statement that I need.

How do I run it? It just needs to return results as a datasheet so I can see them. I'm running Access 2013. I've been searching on this subject for quite a while and found many references to DAO, ADO, ADODAOD, YODELAEYYOUDELAYIO! and other things I don't yet understand ...

View 8 Replies View Related

Modules & VBA :: Programmatically Find And Select Item In Multi Select List Box

Apr 23, 2015

I have a multi slect list box (simple) and I need to find and select an item using vba - e.g., the bound column is the ID field and I need to select a specific ID (which will be different each time) as opposed to selecting the 100th record for example. How do I do this?

View 2 Replies View Related

Modules & VBA :: How To Process A Select Query Using Listbox Value As Where Variable

May 6, 2015

I am using Access 2013. I have the ability to pull a selection from a listbox. I can create a Select Sql string using that variable

sql As String, strCompany As String, strWhere As String
strCompany = strCompany & Me.lstResource.Column(0, varItem)
strWhere = "[Company name]=" & "'" & strCompany & "'"
sql = "select * FROM tblResources WHERE " & strWhere

From here I have trouble. I see lots of examples to run an active query but not much on a select query. I have tried a number of things with no success. How to use this select statement to actually run against an existing access table? I am not putting it into a form or report at this time, just running the query to check results.

View 1 Replies View Related

Modules & VBA :: Write Select Query That Will Search The Data From Bottom To Top Of Table

Dec 3, 2013

see below the code . The select statement searches the Printpoolno value from the top to bottom in table tblmaster. As in my table tblmaster there are thousands of records and it takes long to search for that Printpoolno from the table . Is there anyway we can write a query that will search the table from bottom to top as the Printpoolno will always be in the bottom records and not in the top records.

Code:

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim r As Long

[Code].....

View 3 Replies View Related

Modules & VBA :: Criteria On MS Access Query Not Working With Multi-Select Listbox

Aug 22, 2014

I am attempting to filter records using a multi-select listbox, but all records are being returned. Here is my code.

Private Sub btnKeyboxCount_Click()
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String

[Code] ....

View 1 Replies View Related

Modules & VBA :: Getting Error End Select Without Select Case?

Sep 5, 2014

When I run the below code I am getting the error "End Select without Select Case" I figured it might be because I have the "End Select" before the "End With" however when I move the "End Select" after the "End With" I get the error "Loop Without Do".

Code:
Private Sub cmd_Update_Conditional_Codes_Click()
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset

[Code].....

View 3 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

Modules & VBA :: List Box Select Value

Jul 21, 2014

I have a listbox with *Like* search function as attached file. However, when the cursor moves to the items in listbox, a specific item can not be selected by Key Press even (Enter key). I do not know what my mistake is .

View 2 Replies View Related

Modules & VBA :: Add A Format To Sql Select For Currency

Jul 25, 2014

add a format to the Sql select for currency.This is just a section of the full code before

Code:

Function RunLWSCA()
Dim cnn As ADODB.Connection
Dim rstUplo As New ADODB.Recordset
Dim rstDiag As New ADODB.Recordset
Dim rstTemp As New ADODB.Recordset
Dim SQL As String
Dim strDiag

[code]...

View 12 Replies View Related

Modules & VBA :: Select Rows From Different Reports

Apr 9, 2014

Im working on a DB with multiple tables. My DB consists in a lot of tables, each one for a different element and his specifications (e.g.: transmitter, sensor, relay). The user can create loops selecting different elements (All the elements of the same loop have the same Loop ID).

I want that when the loop is finished the user can select one of the loops he has create and see all the elements and specifications he has selected for this loop. The problem is that when I want to show in a form or report all the elements the user has selected before I cant select each different element from his table and show in a form or report.Until now I have this code in a form to select an element from a report and show it, but it doesnt work very well.

-) List3 is a list box in my form where is the list of elements to select.
-) Boton1 is the button in my form to open the report when the element is selected
-) Transmitter Specifications is the report with the list of elements and his specifications.

Option Compare Database
Option Explici
Private Sub boton1_Click()
On Error GoTo Err_boton1_Click

[code]....

View 1 Replies View Related

Modules & VBA :: Select All Fields That Contain Textbox Value

Oct 20, 2014

I'd like user to input what he's looking for in a textbox and then to filter all the records that contain that word.

values could be like rome, paris, belgrade

When user inputs rome, all fields containing rome should appear

tblTest like '%rome%'
should this work

tblTest like '" & Me.txtTest & "'
tblTest like '%" & Me.txtTest & "%' "

these give filters in report respectively :
tblTest like 'rome'
tblTest like '%rome%'

I might have something else wrong, but i don't get the desired results. No records are displayed...

View 4 Replies View Related

Modules & VBA :: SQL SELECT SUM GROUP Not Working

Nov 8, 2014

I have a select clause, it executes fine, BUT the totamt is not calculated. The field is Amount. The code is not working.

strSQL = "SELECT Account, Dept, " & "Sum([Amount])" & " As [totamt] FROM TB201410 GROUP BY Account, Dept;"

View 5 Replies View Related

Modules & VBA :: Select Case Function

Aug 17, 2014

My problem now is about evaluating the result of that query in order to calculate quarterly and annual performances. The query returns correctly:

1) NULL - when I don't have values in the given month
2) 0 - when one of the components of the performance formula is 0
3) value (positive or negative) which can include any positive or negative value

In order to calculate the quarterly values I need the performance of the last month in the quarter and the first month in the quarter. My monthly values are stored in columns so say for Quarter 1, I would need something like this:

Code:
[mar]/[jan]-1

As you might have thought, the problems come when I have NULL or 0 values. Let me give you a couple of examples.

[jan] is NULL
[feb] is a value
[mar] is a value

Than my formula would have been

Code:
[mar]/[feb]-1

Second case:

[jan] is 0
[feb] is 0
[mar] is 0

In this case I would like the formula return '-' because I want to be able to spot the case in which the quarterly performance is 0 because the initial values were 0 from the case in which the performance was actually 0 (i.e., say [jan] = 101 and [mar] = 101 then performance is 0 which is different from having [jan] and [mar] = 0 thus performance '-').You can combine the three states (NULL, 0, value) with the three months and find many combinations (I have found 27 relevant ones).

I initially thought to use SWITCH in SQL to evaluate the 27 combinations but I found out that SWITCH would evaluate non more than 15 conditions. So I guess the solution should be use a function in VBA which does what I was about to do with SWITCH in SQL.

I have built a test function which evaluates the numerator of my formula using only two cases (CASE 1: all variables have a value <>0, and CASE 2: the first month is null, the second is 0 and the third a value). Here the code:

Code:
Function evaluate_s(Var1, Var2, Var3) As Double
Select Case Var1
Case Is <> 0

[code]....

My main question here is what is wrong in the syntax of my function. Why is not correctly evaluating at least the two cases in my function? I always get 0 for all records.

View 8 Replies View Related

Modules & VBA :: Drop Down Select From Another Table

Jul 7, 2015

I had 3 drop down filters that worked when the word being filtered was written in the table. I have since then linked them to new tables to be more database-y. The problem I have found (a couple weeks later too) is that my drop downs broke.

It makes sense why, they are now 1,2,3,4 instead of words but how do I make this work now that it isn't in the current table?

Code:
Private Sub cboMDS_AfterUpdate()
Dim myPlane As String
If Me.cboMDS = 0 Then
myPlane = "Select * from Baseline"

[Code] ....

View 10 Replies View Related

Modules & VBA :: Select List Box Value By Code

Jun 11, 2013

it has a listbox, wich RowSource is a query with 5 fields, BoundColumn is field#1.I have a comboBox with values of field#1 to have an 'AfterUpdate' Event, that highlights the row in the listBox that has the value of the comboBox in in field#1.

View 1 Replies View Related

Modules & VBA :: Dynamic Select Case

Jul 31, 2013

I want to know if it's possible to make a dynamic select case. In my form I have 3 separate combo boxes. What I want to happen is depending on what was selected in one the options in the other two change and if you select something in the second the option in the third narrows down again. The user can select these in any order. They can use one by itself or all three independently. In my code I can do this with many select statements, e.g

Select Case Me.combobox1.Value
Case "choice1"
Forms!CurrentForm!combobox2.RowSourceType = "Table/Query"
Forms!CurrentForm!combobox2.RowSource = "SELECT fieldname FROM tablename WHERE fieldname = '" & Me.combobox1.Value & "'"
End Select

In the place I have "choice" is it possible to write something along the lines of,Me.combobox1.value = "SQL Code"..The idea that this code would still work if the user adds more data to the tables which these combobox choices come from.

View 6 Replies View Related

Modules & VBA :: INSERT INTO SELECT Statement

Mar 4, 2014

I have an append query that contains an IIF statement. I want to code that into a VBA function. The SQL view of the query looks like this:

Code:
INSERT INTO tmpAvailInv ( NUID, Inv_Name, F_Name, M_Name, L_Name, Role )
SELECT tblPeople.NUID, tblPeople.[F_name] & IIf(IsNull([M_Name])," "," " & [M_Name] & " ") & [L_Name] AS Inv_Name, tblPeople.F_Name, tblPeople.M_Name, tblPeople.L_Name, tblPeople.Role
FROM tblPeople
WHERE (((tblPeople.Role)="Investigator") AND ((tblPeople.Archive)=False));

What I wrote for the VBA code is this:

Code:
Dim strSQL As String
Dim db As Database
Set db = CurrentDb

[code]....

Where it chokes is on the IIF statement with the double-quotes in it. I've tried several combinations with single quotes and double double-quotes. I'm just not getting it.

View 4 Replies View Related

Modules & VBA :: Adding WHERE To SQL Select Statement

Sep 29, 2014

I have a SELECT statement which works

Code:
"SELECT Bookings_Table.Booking_Time, Bookings_Table.Num_Slots, Bookings_Table.Booking_Date FROM Bookings_Table ORDER BY Bookings_Table.Booking_Time;"

But when i add the WHERE

Code:
"SELECT Bookings_Table.Booking_Time, Bookings_Table.Num_Slots, Bookings_Table.Booking_Date FROM Bookings_Table WHERE (((Bookings_Table.Booking_Date)=[TB_CAL_DATE])) ORDER BY Bookings_Table.Booking_Time;"

it doesn't work [TB_CAL_DATE] is a textbox with a Date in it...

View 3 Replies View Related

Modules & VBA :: How To Select Item From A Listbox

Jul 29, 2013

At the moment I have a query that returns a result based on what's selected in a combobox.

SELECT ComponentT.ComponentType AS ComponentType
FROM ComponentT
WHERE (((ComponentT.TotalComponent) Like [Forms]![DeviceF]![D_ComponentNameCmb] & "*"));

Instead of this I want to rewrite the query to return a result based on what the user has selected in a list box of items. How would this query look. Would I need to increment it in a loop checking all entries into the list box as it can vary in size and if so how?

I tried changing the last line to WHERE (((ComponentT.TotalComponent) Like [Forms]![DeviceF]![D_OutputLsb].[ItemsSelected] & "*")); which returned a result just not the right one. Is there something wrong with the syntax?

View 4 Replies View Related

Modules & VBA :: Displaying Results Of A Select Statement?

Oct 7, 2014

I have a database with several one-to-many relationships and a nested subform based off of those relationships. Relationships are as follows:

One Lender to Many Relationship IDs
One Relationship ID to Many Tax IDs
One Tax ID to Many DocumentsRequired

My forms are nested as follows:

Relationship ID form (contains info for Relationship ID and Lender)
-> Tax ID
-> DocumentsRequired

I believe I'm just overcomplicating this. But I have simple search box (an unbound text box with a command button), which is located on the top-level form for Relationship IDs. As you'll see above, that form only contains the fields for Relationship ID and Lender. However, users have to be able to search by Relationship ID, Customer Name, or Tax ID number, the last two of which are only available on the nested subforms. Currently I have the search box reaching out to grab results from a query. When I assign the results to the Me.RecordSource, it works perfectly except that it's in read-only format. The users have to be able to edit the results of their search. I'm not even sure I'm doing this in the easiest fashion. I would have preferred to just use the select statement to search through the subform, but I'm guessing my syntax was wrong because I never got it to work. Below is what I currently have.

Dim strtext As String
Dim strsearch As String
strtext = Me.SearchBoxTxt.Value
strsearch = "SELECT [Relationship ID] " & _
"FROM CustomerNormQuery " & _
"WHERE [Relationship ID] like ""*" & strtext & "*"" OR [Customer Name] Like ""*" & strtext & "*"" " & _
"OR [EIN/SSN] Like ""*" & strtext & "*"" " & _
"GROUP BY [Relationship ID]"
Me.RecordSource = strsearch

View 4 Replies View Related

Modules & VBA :: Update Select List Coding?

Jun 23, 2013

The coding below works fine. It presents a form with a list box of counties. Allows the user to select ALL, one or several counties and returns a query containing the clients from those counties.

The fields showing in the query are First, Last, Add1, FLAGToMap, City, Prov and Sector_Name.

I want to add in there a choice to select only the records that have are TRUE (-1) in the FLAGToMap field - just like the ALL button, this would be an ALL Selected Button let's say.

I would not know where to begin as I copied and adapted the coding below from a sample database and don't understand - at all - how the query is generated. The only coding in the form is the one below.

Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String

[code]....

View 6 Replies View Related

Modules & VBA :: Report With Only Select Records In Table?

Dec 17, 2014

I have a master table located on the backend of the server with about 3 fields:

Software
Version
Description

I have 4 databases, one for each software on the front end accessing this master table.

So far I have in the master table a dropdown to select 1 of the 4 software's, the version number and a brief description.

One the front end I have a button that when pressed will bring up the report based on the master table.

Can I have the report just select the software that it refers to. For instance if the datebase is Sony when I click on the report it only grabs the records with Sony on it. For my next database when I open up Sharp's database it only makes a report for the Sharp records.

How would I do this? Is this something I have to code or something I can do in criteria or do I create a query?

View 2 Replies View Related

Modules & VBA :: Using Select Case Statement With Array?

Jun 26, 2014

I have a boolean array, foundState(3), whose 4 elements correspond to 4 variables describing conditions that will dictate what action is taken upon closing a form.

There are only 6 possible outcomes for the array, and they can be divided into just 4 cases:

Case {T,T,T,T}

Case {T,T,T,F} OR {T,T,F,F} OR {T,T,F,T}

Case {T,F,F,F}

Case {F,F,F,F}

What the proper syntax would be for this if I'm trying to create a "Select Case" statement for these 4 cases.

View 7 Replies View Related







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