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 ...
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
I have a db with 2 tables, one containing basic info FirstName, Surname, YearGroup, and another table with other data. I imported a list of names into the first table, firstname and surname only were imported, other field was left empty, other table was also left empty. I made a simple select query to pull all records from both tables and i get no results at all. None of the imported names show on the query results. what I am missing?
I am a novice with Access and would like a steer with what I am sure is a simple issue but I can't find an answer. :confused: I currently have 2 queries based on 2 separate tables.
Ops_Log_996_Query SELECT [996_Table].Unit, [996_Table].Location, [996_Table].hiredate FROM 996_Table WHERE ((([996_Table].hiredate)=Date()));
Ops_Log_SQTU_Query SELECT SQTU_Table.Unit, SQTU_Table.Location, SQTU_Table.hiredate FROM SQTU_Table WHERE (((SQTU_Table.hiredate)=Date()));
When run separately the first query returns 2 results and the other 1 result - fine so far. I am now trying to combine the results for display in a report so I have a third query which takes its info from the first two -
SELECT DISTINCTROW Ops_Log_996_Query.Unit, Ops_Log_996_Query.Location, Ops_Log_996_Query.hiredate, Ops_Log_SQTU_Query.Unit, Ops_Log_SQTU_Query.Location, Ops_Log_SQTU_Query.hiredate FROM Ops_Log_996_Query, Ops_Log_SQTU_Query GROUP BY Ops_Log_996_Query.Unit, Ops_Log_996_Query.Location, Ops_Log_996_Query.hiredate, Ops_Log_SQTU_Query.Unit, Ops_Log_SQTU_Query.Location, Ops_Log_SQTU_Query.hiredate;
This displays the 2 separate records in the first 3 columns ok but in the last 3 columns the info in record 2 is a repeat of record 1. :confused:
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.
I have a query that is returning inaccurate "Date Completed" results. Here is the SQL:
PARAMETERS [Enter Start Date] Text ( 255 ), [Enter End Date] Text ( 255 ); SELECT DISTINCT [Qry BDM FA English Email Test Part 1].ADI_DISTRIBUTOR_ADVISOR, ADL_AAH_ADVISOR_ACTIVITY_HISTORY.AAH_DATE_COMPLETE D, [Qry BDM FA English Email Test Part 1].AGA_EMAIL, ADL_AAH_ADVISOR_ACTIVITY_HISTORY.AAH_EMP_ID, DateAdd("d",180,Date()) AS [Next Update],
I have a simple select query on a SQL table from Access. The query is:
SELECT tbl_Orders.OrderID, tbl_Orders.Approved FROM tbl_Orders WHERE (((tbl_Orders.Approved)=0) AND ((tbl_Orders.Completed)<>0)) ORDER BY tbl_Orders.OrderID;
The strange thing is that sometimes it pulls 34 results, and sometimes 38. From what I can tell, it should be pulling all 38.
What can I do to make sure it gets all the records?
I have a query that pulls up the sum of records that holkd a date value between two dates, and groups them by the person that created them.
How can I show these results in a graph? that will update every time a user clicks a buttons (as they may wish to change the two dates to search between)...
i've created a database for city permits to be issued (which works perfectly thanx to alll your help), but now i need to be able to print out 1 permit at a time when it is issued (immediately after the data has been entered into the forms). i have 3 tables & 3 forms: contractors/owners, permtOrders (which has tabs for the different types of permits), and permitOrderDetails - each form has a button to open the next. now i need to be able to print an actual permit (like the carbon copy, landscape kind that usually tears out of a book). once the user enters the information into the forms, i need a little bit of that info from each form to be carried onto the actual printable permit. i've tried to create a query w/data from the 3 tables, but it isn't returning any results. the relationships between the tables were crreated w/the wizard & i've tried created the query a couple of different ways. help :confused:
also (i know...i'm dumb), if someone could tell me how to take a snapshot of my query it would help w/my explanation of this problem. i only know how to do this w/a report.
I have a question where I need to effectively invert the results of a select query.
I have multiple tables in the database, but the 2 I'm looking at are TBL.Trip and TBL.TripDiary
A trip is logged in the trip table, and then an operator logs a diary entry against the trip. One of the options that the operator selects is when they receive an email back from the traveller, this is logged as a diary entry.
I need the query to look at the DIARY_Action field and select all records in the TBL.Trip that DO NOT CONTAIN an entry for Diary Action "4".
I can produce a select query that gives me the results to see all trips that HAVE had a response, with a simple IS LIKE "4" query in the action field.
If I change this to IS NOT LIKE "4", I get the results that I need, but duplication due to the various other "diary" entries in the table".
I simply need the inverse of the IS LIKE query but cannot see how to remove the duplicates?
Let's say Table (T1) has fields F1 and F2. After a massive update to T1, there are some records with F1 = "" because a Dlookup using F2 as criteria to another Table (T2) resulted in a null. I created a select query to show unique T1F2 values where T1F1 = "". The user can use this query to find out which F2 values need to be added to T2.
How do I create an update query that will update T1F1 with values from T2 using the T1F2 results from the select query to again use the Dlookup to T2 (of course after T2 has been updated to contain the missing F2's)?
I have a rotating number of tables that are created from excel spreadsheets that are imported. The Tables will change, but when they are there I need to be able to create a query that will merge them all together so I can run one query against all the tables. In SQL I know you can use a * to say Select * From Table_1
Is there a syntax for the From portion so I can say:
Select * From * (AKA all the tables in the Database)?
I have searched for a wildcard for the FROM statement that works like in the select statement but have been unsuccessful at finding an answer. Can anybody help? I'd list the Tables in the from Statement but there are 266 of them. Unless someone knows how to say:
Select * From All tables in a folder with 266 excel spreadsheets
I have a database of around 15,000 users and I'd like to create a query that I can run on a weekly basis and save the results to an Excel spreadsheet. The results need to be logical and understandable by my coworkers.
Unfortunately, the actual results of the query are not (in their raw form) logical or easy to interpret.
Let's say I have a table called "users" and within that I have:
Surname Forename FieldA FieldB FieldC
FieldA has a value of either NULL or a 12-digit number FieldB has the values are "ENABLED", "DISABLED" and "N/A" FieldC contains a value of either "1" or NULL
This means nothing to my coworkers who want each user to be sorted into a "category". As I'm running this on a weekly basis, I'd like this query to do the work for me, so I don't have to manually assign everyone to a category in Excel. Plus, of course, there is no chance of human error if the query does this for me.
Sooo... I'd like my query to categorise for me as follows:
Category1 = FieldA IS NOT NULL and FieldB="ENABLED" Category2 = FieldA IS NOT NULL and FieldB="N/A" Category3 = FieldA IS NULL and FieldB="ENABLED" Category4 = FieldA IS NULL and FieldB="N/A" Category5 = FieldA IS NOT NULL and FieldC = 1 ... etc.
I'd like the final column in the query results to simply list the category name, so I can simply copy and paste the data into an Excel spreadsheet and be done with it, safe in the knowledge that it makes sense to all.
Basically, I have a database table that is maintained every week. It is about e-mail account licenses for Office 365.
Each column represents a week of license data for every mailbox account which is about 10 000 plus users with the date as the field headers( something like "License information as of 06122013").
Basically, I have created 52 queries based on the license type I require to be calculated & I have a form created to control it. However, right now it is all output only as I have yet to create any user parameter to specify on any of the queries about the date I want
As I am currently unsure how to specify all the queries to use one field date header which I want the user to specify via drop down list in order to calculate all the licenses on this specific date.
For example, if I want to see the license information for 06/11/2013, I would choose "License as of 06112013" & it would then run all the queries based on the header specified earlier & output that information on the form.
Right now the queries are all configured as the "Select" type.
I have a query which returns a list of reports I have created this session.
I want a button that when I click it runs the query and then stores the results into a variable.
I looked online and found a pretty good connection code and loop code to get the data but I'm having a hard time getting it to store into a variable.
What I want to do next is using code print the reports that were listed in the query.
That Means I need to be able to take that variable and pop the information off it, storing it into another variable (or if i can use an array just use that) and use that to concatenate into a command to print that report.
I am having problems with the syntax of this though. I think I'm just missing some key elements.
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.
It works great, except that it is also copying the column headings. Is there any way to copy only the results without the headings?
I'm copying this data to the clipboard because I want to be able to paste it in a program called DataLoad which will load this data into one of our company's legacy systems.
My company is finally using my accounts package (alongside our original package until we can be certain there are no bugs).
Anyway just adding nice to have features at the moment.
I've got a form that has 5 combo boxes where the user can select 5 customers. Then a button so they can graph the amount of money generated by each customer and compare them.
I've just made a query to return the top 5 highest grossing customers.
What I what to do now it feed the query results into the combobox values when I press a new button on my form.
I have an Access 2010 application that talks to SQL Server. Part of the application creates tables on the SQl Server and inserts data to it (I know SQL Server has its on data inport/export wizard and bulk insert capabilities but for reasons outside of my control it needs to be done from Access). So I have a connection to the SQl Server and i have VBA that happily executs SQL statements on the SQl Server. The problem is I want to return a recordset from the result of a SQL query issued to the SQL Server. The idea being to check for the presence of tables already created, and if so, append to the already existing table, otherwise if it does not exist, create it and insert data to it.Ive tried the following code but it seems no recordset is being returned:
Set objConnection = New ADODB.Connection objConnection.Open "DRIVER={SQL Server};SERVER=10.200.3.14;trusted_connection=yes; DATABASE=" & Me.Combo54 strSQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = '" & Me.Combo54 & "'" strSQL = "USE " & Me.Combo54 & " " & strSQL Set Rst = cmd.Execute("USE " & Me.Combo54 & " " & strSQL)
I basically want to Loop through a query (or if not possible trough a table) and show the results in my form. For each record in the table I am doing additional checks when loading the form and showing that result accordingly. I do not want to do these checks in additional queries - I think it is more efficient doing it in the form directly. Here is the code I have:
Dim dbsSR As DAO.Database Dim rstValQry As DAO.Recordset Set dbsSR = CurrentDb Set rstValQry = dbsSR.OpenRecordset("qry_val_tbl_ind_rec-rev_import") With rstValQry While (Not .EOF)
[Code] ....
The Loop seems to work because in debug it is going through it 3 times (the number of records I have in the query), but the result is always the same - it seems the above code is not checking / refreshing based on the individual query records.
Is it possible to have a macro button to pass query results to a listbox? I have a database where I have some fields with dates. What I need to do is to show a list of all cases that have a start date (and also the end date). So I will have 2 buttons, one that says "show started cases" and second one "show closed cases". If I press the first button, it will run a query and show only those cases that have a date filled in the started case field. I made a button that shows the results in a report, but I would like to have it show up in a listbox so I can double click it and go straight to the case.
I have a form with a list of names. When a specific name is selected from the list, a button is clicked and a query is run with the specific name as the criteria/filter.
I've written code to export these same query results to an Excel sheet, and I want the Excel file name saved with the name selected in the form.
Below is the code that I've written that doesn't work.
I have successfully used VBA to populate select query results into an excel worksheet on open, for a co-worker. Now I am trying to populate the records from an append query to the bottom of those results, which are now in a table on an excel spreadsheet, Contractor EIF. I am trying to make this work because my co-worker modifies the results in the table, deleting rows, adding fill color etc. and as new projects begin he would like those added to the projects already in the table (without rewriting the entire table).
Private Sub Window_Open() Dim strDB As String Dim strMyPath As String Dim strDBName As String