Trying to set up a query where one of the fields is selected by the value in a combobox. Can't figure out the right syntax. I'm in the SQL view of the query builder, using the following:
SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, Deviations.Active, Deviations.[Forms]![DeviationSelectionForm]![cmbAircraft]
FROM Deviations
WHERE Deviations.[Forms]![DeviationSelectionForm]![cmbAircraft]=True
ORDER BY Deviations.Deviation DESC
The combo box selects the tail number of the aircraft which is a checkbox in the table. There are multiple aircraft tail numbers as checkbox fields. So if there were no combobox and I wanted the query to use as example aircraft 416, the query would be:
SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, Deviations.Active, Deviations.[416]
FROM Deviations
WHERE Deviations.[416]=True
ORDER BY Deviations.Deviation DESC
When I test this query it works as I need it to.
So the question is how to I create the query with the combobox in a form identifying the field name of a table?
Build the Query SQL viacode in the combobox afterUpdate event.
Dim strSql as String
strSql = "SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, " _
& "Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, " _
& "Deviations.Active, Deviations." & Me!cmbAircraft _
& " FROM Deviations " _
& "WHERE Deviations." & Me!cmbAircraft & " =True " _
& "ORDER BY Deviations.Deviation DESC"
CurrentDb.QueryDefs(<yourqueryname>).SQL = strSql
That said, it seems like you have built a spreadsheet instead of a database.
Your tables are not normalized. Aircraft should be records, not columns. I suggest you read up on database design and normalization.
Related
I have an Access form with different functions users can do. They are pulling lists of data, all from 1 table. Based on different filters or operations they want to do, I dynamically construct the SQL, use that to update the query defs of a query, and then set the subform object to that updated query. The problem is that the one I update the subform source object to the query, I need the columns that show to be in the order of the query.
This is the code I use, at different points of use on the form, depending on if they are interacting with a combo box, entering a filter into a textbox control on the main form, and I use similar code to set the default sql/query defs on load of the form. Why don't the columns always show in the same order as the sql that was updated to the query's definition?
Dim fsql As String
fsql = "SELECT table1.field1, table1.field2, table1.field3, table1.field4, table1.field5, table1.field6, table1.field7 " & _
"FROM tblVFileImport "
If Nz(Me.cboFilterA, "") = "" Then
fsql = fsql & "ORDER BY table1.field2, table1.field3 "
Else
fsql = fsql & "WHERE table1.field6 = '" &Me.cboFilterA & "' "
fsql = fsql & "ORDER BY table1.field4, table1.field5 "
Me.cmdExtrabutton.Visible = True
End If
CurrentDb.QueryDefs("qrySubformSpecs").SQL = fsql
Forms!frmDoStuff.Form.frmDoStuff_SubResults.SourceObject = ""
Forms!frmDoStuff.Form.frmDoStuff_SubResults.SourceObject = "Query.qrySubformSpecs"
The columns get in order some times, but not consistently.
I also want to fit the columns a small as they can, fitting the contents of their respective data, if that's possible. The column fit is not as big of a deal, because the user can select the left corner and then double-click between the columns if they really need to. The order of the columns is definitely more important here.
If anyone knows how to accomplish this, I would really love the help. Thanks!
FWIW, the backend is SQL, so this is all coming from 1 big linked table. The performance is quick and not an issue, but the column order is important and needing to work right.
I'm creating a form that displays information about an individual that is taken from two tables when a name is entered by the user. I want the fields to be output into they're own text boxes. My code looks similar to what's below.
When I run the code it displays the literal query "SELECT name etc..." in the textbox. I saw Dlookup works for textboxes but to my understanding it doesn't work well with more than one table. Any advice would be greatly appreciated!
PS I'm a VBA/access newbie
Dim SQL, SearchInput As String
SQL = "SELECT name" & _
"FROM tablename INNER JOIN othertablename ON tablename.name = othertablename.name" & _
"WHERE tablename.name LIKE ""*" & SearchInput & "*""
Me.txtbox = SQL
I'm pretty sure this is a duplicate, but it's faster to answer than hunt for the other posts.
You need to declare a recordset and assign the data returned from the select statement to it. This will leave you with something very similar to an array. After that you need to just line up the array element to the positions of the columns. IE. rs(0)=name in the select statement above.
Dim rs As Recordset
Dim SQL As String, SearchInput As String
SQL = "SELECT name " & _
"FROM tablename INNER JOIN othertablename ON tablename.name = othertablename.name " & _
"WHERE tablename.name LIKE ""*" & SearchInput & "*""
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
Me.txtBox = rs(0)
That should work.
Since you are new to MS Access & VBA in general, I'll propose a solution which avoids VBA entirely.
In your current code, I assume that SearchInput is sourcing its value from another control on your form.
I would suggest creating a new saved query (called it MyQuery) with the following SQL:
select table2.name from table1 inner join table2 on table1.name = table2.name
Then, in the Control Source for your textbox, use a DLookup expression with the following arguments:
=dlookup("[name]", "MyQuery", "table1.name like '*" & [SearchInput] & "*'")
Here, [SearchInput] refers to the name of the control on your form containing the search criteria.
I have this ACCESS form that contains a subform:Form & Subform
I'm trying to create a search button (using VBA) to find a student by name (students' names are not in the table behind the main form).
I've done the first step, which is to search the subform for a student's name, but I'm having trouble with my desired second step. I'd like the code to then take the CWID number of the student and find the matching record in the main form. How would I do this? (My current code is below)
I've tried DoCmd FindRecord and GoToRecord, but it is completely stumping me. I'm Google-learning how to do this and think I'm fundamentally misunderstanding something and am thus unable to search for or understand the answer. Any help would be greatly appreciated.
Private Sub btnSearch_Click()
Dim SQL As String
SQL = "SELECT [AWN Banner].CWID, [AWN Banner].FirstName, [AWN Banner].LastName, [AWN Banner].Freshman, [AWN Banner].Instructor, [AWN Banner].Course " _
& "FROM [AWN Banner] " _
& "RIGHT JOIN [AWNEntry] ON [AWN Banner].CWID = [AWNEntry].CWID " _
& "WHERE [LastName] LIKE '" & Me.txtKeywords & "*' " _
& "ORDER BY [AWN Banner].LastName "
Me.subAwnObj.Form.RecordSource = SQL
Me.subAwnObj.Form.Requery
End Sub
When I create my search boxes I put in the underlying form source a WHERE clause WHERE [field] LIKE "*" & [MySearchBox] & "*" I put such a clause in the subform and the main form I create a JOIN to the subform record with the same WHERE clause defined. Then on click I request all recordsources.
I am currently working in MS ACCESS. I have 2 forms one called MyForm and the second one name Transit. I have one table called SimulationTable which have 4 fields Fiscal_Year, Scenario_Number, Description and Operating_Unit. I would like with VBA code when clicking in a button to display result of SQL query in the textbox called TXTBOX which is in Transit form. I have tried many times but it doesn't work.
Any idea of how to fix ? Thank you.
Here is the SQL query:
SELECT SimulationTable.Description FROM SimulationTable WHERE Fiscal_Year=Forms!MainForm!OperatingFY AND Operating_Unit = Forms!MainForm!Text3 AND Scenario_Number = Forms!MainForm!Selected_scenario
Could just use DLookup():
Me.TXTBOX = DLookup("[Description]", "SimulationTable", "Fiscal_Year=" & Forms!MainForm!OperatingFY & _
" AND Operating_Unit = " & Forms!MainForm!Text3 & _
" AND Scenario_Number = " & Forms!MainForm!Selected_scenario)
Are these criteria fields all number type? Is TXTBOX bound to a field? If not, could even just put the DLookup() expression in ControlSource property.
It works. I have added apostrophes around the "Operating_Unit" which is a field in text format. Thanks very much June7.
Me.TXTBOX = DLookup("[Description]", "SimulationTable", "Fiscal_Year=" & Forms!MainForm!OperatingFY & " AND Operating_Unit ='" & Forms!MainForm!Text3 & "' AND Scenario_Number =" & Forms!MainForm!Selected_scenario)
Okay I am simultaneously trying to learn VBA and SQL so please excuse me if this question seems dumb. I am trying to pull data from multiple excel files that meet certain conditions then insert that data into an Access table. We are trying to automate some label printing and the first step is to pull the data from these multiple excel sheets and insert it into the access sheet.
Could someone just give me some ideas on how to do this? I'm coding from Access, I figured out how to SELECT the data from one sheet and then INSERT it into the Access table but not how to do it with multiple tables. Also these excel files may have the same column names across along with multiple sheets of tables in each excel file.
Here is some code I used for the pulling of data from a single sheet
dim UDA817 as string
UDA817 = "UDA00817_00"
//'MARK_PER_UDA_00817 is a field name in my MASTER table
sql2 = "INSERT INTO MASTER ([MARK_PER_UDA_00817]) " _
& "SELECT [F4] " _
& "FROM " & UDA817 & ";"
Thanks!
There are a couple of options on how to approach this, but I think the simplest one is going to be using the UNION operator. Try this:
Option Compare Database
Option Explicit
Dim UDA817 As String
UDA817 = "UDA00817_00"
//'MARK_PER_UDA_00817 is a field name in my MASTER table
sql2 = "INSERT INTO MASTER ([MARK_PER_UDA_00817]) " _
& "SELECT [F4] " _
& "FROM " & UDA817 & ";" & _
"UNION " & _
"SELECT Field 1, Field 2 " & _
"FROM tblTable2 "
'// Utilize the UNION operator to concatenate 2 or more SELECT strings