Microsoft Access: Different Queries Based On Input - sql

I want to create a form that includes a drop-down list of queries, and then a button that when i press it, runs the query which i have selected from that drop-down.
I don't mind manually filling in the drop-down and then adding comparison code if that is possible. What is the best way to do this?

You can populate a combo or probably better a listbox using the following query.
This would list all queries that were named starting with qryRep... and display them as Rep...
SELECT MsysObjects.Name, Mid([Name],4) AS QueryName
FROM MsysObjects
WHERE (((Left([Name],6))="qryRep"))
ORDER BY MsysObjects.Name;
By restricting the display to a named group, you don't list every query in the database.
To run the query from the double click event of the list box - assuming it was called lstQueries;
Private Sub lstQueries_DblClick(Cancel As Integer)
Dim txtQueryName As String
txtQueryName = Me.lstQueries
DoCmd.OpenQuery txtQueryName , acViewNormal, acReadOnly
End sub
This will open the query in a read only view.

Related

How to use a combo box to fill mutltiple field in a subform in MS-Access?

I have a sub form in Access that allows a user to select records using check boxes. In addition to this sub form there is a combo box with a list of names.
Is it possible to select a few records using the check boxes and assign a name to them with a press of a button?
The button is called [assignButton] , the sub form is called [list subform] and the combo box is called [personCombo] .
This seems like a simple task, but I am unfamiliar with how the VBA for the button triggering this kind of action would be written?
All records are bound to an existing table called "tbl_jobs" this table has the same amount of rows and columns as the subform in the screenshot
Disclaimer: The following code is only intended to illustrate how to update the subform rows, so it includes no error handling or other common process code. Also notice that the SQL statement has no additional criteria in the WHERE clause, primarily because the question lacks the subform's actual RecordSource query and/or information about how the subform table is prepared and/or filtered for each time the form is loaded.
Private Sub AssignPerson_Click()
Dim qry as QueryDef
Set qry = CurrentDb.CreateQueryDef("", _
"UPDATE tbl_jobs SET Person_Name = [ParamName]" & _
" WHERE [Select] = True")
qry.Parameters("ParamName") = PersonCombo.Value
qry.Execute
Me.list_subform.Requery 'Ensure subform displays updated rows
End Sub
Really this question is borderline "Too broad" for Stackoverflow. There are multiple ways to accomplish what you want, and your problem is one of design, coding and understanding. But in the end, this model code is rather simple and satisfies the basic requirements so I feel it not worth nitpicking.

How to select table values from combobox selection?

I'm using an Access 2003 database and have 2 comboboxes I am trying to work with. The first box I have perfected already, which is a dropdown of different tables (categories of parts). Once that table is selected, I want to be able to look at the part numbers within that category through a dropdown box selection. From here I want to be able to pull up the correct report for that category with that part number in it so I can print a report for every part number. I'm sure I'll have to write some sort of VBA, Query or Macro AfterUpdate() code, but I just don't know how to fill that second combobox with the selected table's part numbers.
Click here for an image of my Menu layout
Here's my Query for the first box to show the tables I want:
SELECT Msysobjects.Name
FROM Msysobjects
WHERE (((Msysobjects.Name) not Like "MSYS*"
And (Msysobjects.Name) not like "_*"
And (Msysobjects.Name) not like "~*"
) AND ((Msysobjects.Type)=1))
ORDER BY Msysobjects.Name;
And I think this is what I'll need to print after the second box has it's selection:
Private Sub partnumberselect_AfterUpdate()
DoCmd.OpenTable Forms![_Datasheet Printing].Form.TagLabelSelection.Column(1), acViewNormal
End Sub
Thank you in advance and let me know if you have any questions.
You are attempting what are called "cascading comboboxes" which means the second box is dependent on the selection of the first.
This is accomplished through the control source of the second combo box.
The first thing you should do is write a query that returns all possible options of the second combobox, without caring so much about filtering it based on the first combo selection. Once you have it returning the correct data, you will add a WHERE clause to the second box's control source that's something like:
WHERE Msysobjects.Name Like Forms![_Datasheet Printing]!TagLabelSelection.Value
This is referencing your first combobox on your form. So after a selection is made in the first combobox, the underlying control source of the second will have the proper criteria to return the appropriate options.
However, you will need to add some VBA to the AfterUpdate() event on the first combobox. Once the selection is made, you need the second box to refresh the control source to populate the correct selections. The code is simply:
Forms![_Datasheet Printing]![MySecondComboboxName].Requery
Please see the example below.
Private Sub cboCountry_AfterUpdate()
On Error Resume Next
cboCity.RowSource = "Select tblAll.City " & _
"FROM tblAll " & _
"WHERE tblAll.Country = '" & cboCountry.Value & "' " & _
"ORDER BY tblAll.City;"
End Sub
You can read all about this concept, and others, in the link below.
http://www.fontstuff.com/access/acctut10.htm

Fill in a text box when selecting a combo box option on Access

Im not very advanced in Access and I am struggling with defining the VBA code/expression build/ or Control Source for an instruction that can make the following isntruction:
If i make a selection on a Combo box (with my key field "TypeService" on my table TblMain)
Then; my text box is able to return the TypeConfig rows related to the TypeService selected before (my table TblConfig contains TypeServiceFK and TypeConfig fields)
So, my objective is to get a dynamic textbox that depends of a multi option combobox.-
Thanks for the help!!
Put a listbox on your form, let's say it's called List2, and cancel the wizard if it pops up.
In the Row Source property of the listbox, put this SQL statement
SELECT tblConfig.TypeConfig FROM tblConfig WHERE (((tblConfig.TypeService)=[Forms]![frmMain]![Combo0]));
This assumes your form is called frmMain and your combobox is called Combo0, so change those as necessary.
Right click on Combo0 and choose Build Event, then Code Builder. Replace the Sub...End Sub stub with
Private Sub Combo0_Change()
Me.List2.Requery
End Sub
Now every time Combo0 changes, the SQL statement behind List2 will run and return the TypeConfigs associated with that TypeService.

Display queries in access subform from a selected listbox

Within Access 2010, I am trying to make a form display a query within a subform QueriesSubForm that when selected from listbox QueryListBox and then click on the button runbtn. It is supposed to display the query within the linked subform, and change when you do this whole steps again from a different choice selected on the listbox and click on the button.
At the moment I can only get it to work, if I want it to display the queries in a new task window, when programmed like so:
QueriesListBox Listbox - SQL View
SELECT MSysObjects.[Name]
FROM MSysObjects
WHERE (((MSysObjects.[Type])=5) AND ((Left([Name],1))<>"~"))
ORDER BY MSysObjects.[Name];
runbtn Button - VBA View
Private Sub runbtn_Click()
DoCmd.OpenQuery QueryListBox, acViewNormal
End Sub
It works and appears to just open one of the queries from the queries list and display it to me, which I do not want.
This might be an easy thing, but I can not see how it is done with either SQL, VBA or Macro, which I am guessing is need for this to work
Ok, here's one way to do it. First make sure your subform is set up with no source object. Then setup your button click like so:
Private Sub runbtn_Click()
Me.QueriesSubForm.SourceObject = "Query." & QueryListBox.Value
End Sub
That should get you the result you're looking for. Alternatively, if you want your form to open with a certain query to displayed on open, you could set up your subform's source object to that query. Using the properties window find the source object drop down and find the query you want to show when the form opens.

Microsoft Access, auto generate columns in DataSheet subform

I want to create a form in MS Access 2003 that lets the user pick from any existing query, and then have it display the results inside the form (as a sub-form in DataSheet view). The user will then be able to select one or more records and click a button on the parent form to do certain actions based on the selection. I want it to be able to work with any query, with very few limits, and display the full results of the query (all columns). The only requirement I might have is that it include certain fields for certain actions. For example, if I have a "send email" action, the query will require a field named "email", or maybe "to" and "subject".
Changing the DataSource of the DataSheet sub-form at run-time isn't a problem, I've done that before using VBA. Getting the columns displayed to change is the problem.
In a .NET WinForms app this could be done with the "auto generate columns" on a GridView control, or using the GridView.Columns collection directly in code. In VBA I don't see a way to add/remove columns from a DataSheet view. I also don't see a way to auto generate them based on the query. It appears the columns are controlled by the controls placed on the form (in form view), and while it is possible to add/remove controls using VBA, the form would have to be placed in Design View and require exclusive access to the database -- sounds very messy and I would like to avoid the exclusive access part.
Am I missing something? Is there an easy way to do this?
Here's how I would go about it. Create a blank subForm control on your main form. To change the source and the columns just leave the source object blank, then when you set it with code, the columns will reset to whatever source you use. So set it like so:
Private Sub setSource()
Me.subForm.SourceObject = "Query.myQuery"
End Sub
Then to get the selected items, assuming you know what column you want, you would do something like this:
Private Sub getSelected()
Dim rs As Recordset
Dim f As Form
Set f = Me.subForm.Form
Set rs = f.RecordsetClone
Debug.Print f.SelTop
rs.MoveLast
rs.MoveFirst
rs.Move f.SelTop - 1
Debug.Print rs!ID
End Sub
If you don't know the column explicitly you can use this to loop through the columns of the selected item and run some analysis on each name until you determine it's the column you want.
Dim i as Integer
For i = 0 To rs.Fields.Count - 1
Debug.Print rs.Fields(i).Name
Next