How to select table values from combobox selection? - vba

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

Related

How do i use Dlookup for Multiple Criteria to create a dynamic Combobox

I have two Comboboxes on a from: txtKategorie and txtTyp.
The values for the frist Combobox (txtKategorie) are fix!
I want the values of the second Combobox (txtTyp) to change according to what the user chooses in the first one.
If the user chooses "Datalogger" the second combobox should only contain "Base Layer Classic" and "Base Layer Plus" as can be seen in the image.
The same idea is true for "Accelerometer".
I've put my code in the AfterUpdate Event of the first Combobox:
If txtKategorie.Value = "Datalogger" And txtTyp.ListCount = 0 Then
i = 1
Do While txtTyp.ListCount < DCount("ID", "tblNomenklatur", "[Kat] = 'K'")
txtTyp.AddItem DLookup("[Typ]", "tblNomenklatur", "[ID] =" & i And "[Kat] = 'K'")
'And "[Kat] = 'K'"
i = i + 1
Loop
When the form opens only the first Combobox "txtKategorie" has Values. When the user chooses Datalogger the code checks how many records in the table have the [Kat] = "K" to define how long the Do While-Statement will run. Then the "txtTyp.AddItem"-Statement should add "Base Layer Classic" and "Base Layer Plus" to the "txtTyp" Combobox. But unfortunately the Code doenst work. There is a problem with the Dlookup-Statement containing tow criterias. If i remove either one of the two criterias the Code works but delivers wrong results for the second Combobox obviously. If i leave it like this the second Combobox stays empty.
Does someone know what im doing wrong?
You can do it easily by below code. Change table name with your table name.
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.RowSource = "SELECT DISTINCT Table1.Typ FROM Table1 WHERE Table1.Kategorie='" & Me.txtKategorie & "'"
Me.txtTyp.Requery
End Sub
Or you can do it graphically from row source query builder. Below are steps.
Combobox txtKategorie is fix. Fine!
For second combobox txtTyp follow the below steps.
Select combobox txtTyp. From property windows select Row Source then click on query builder ... small three dot. See screenshot.
In query builder window build a query from your data table like screenshot and set criteria for Kategorie column is [Forms]![Form1]![txtKategorie] Save and close the query bulder window.
Now for Combobox txtKategorie in After Update event write below line to requery txtTyp. You are done!
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.Requery
End Sub

MS-Access Form shows Number, not Name on load

I got a Database, where I apply to a name a Main and Subgroup.
When I enter a MainGroup f.e. Granades, just subgroup elements like "attack-granades" etc. should be shown.
In genereal it works by writing in this into the MainGroup-Combobox at my Form.
Private Sub MunHauptgruppeRef_AfterUpdate()
Me.MunUntergruppeRef.Requery
Me.MunUntergruppeRef.RowSource = " SELECT UnterGrpNR, UnterGrpName FROM tbl_UnterGruppen WHERE UnterHauptGruppenNr = " & MunHauptgruppeRef.Value & " ORDER BY UnterGrpName ASC"
The Problem is, if I load the datas in my form again, it just shows the related Number to the "Sub-Combobox data" 1( f.e. 35 for Attack-Grenade ) , but not the Name itself. After I reselect the entry in my Main-Combobox(Grenade), it shows the right sub-data which was saved. 2
Tried Requery on Form_Load or Requery of the Combo-Boxes itself. nothing helped so far.
Made some Video3
You need to add the code from Private Sub MunHauptgruppeRef_AfterUpdate() to your Form_Current event, this will refresh your combo box row source to the current value of your MunHauptgruppeRef combobox as you cycle through your records. Also you need to call the Me.MunUntergruppeRef.Requery after you've set the Me.MunUntergruppeRef.RowSource.
You can also add a check when your in a New Record, for the code not to run on the Current Event. See here https://learn.microsoft.com/en-us/office/vba/api/access.form.newrecord

Creating an SQL query in MS Access for a dropdown box lookup in a subform?

I have the following two tables, the first called Projects and the second called Parts:
I then have a form (lets call it Form 1) that uses Projects as its Record Source with a subform that links Project on the Projects table to Project on the Parts table and displays only the items associated with the selected Projects record source, like so:
Now, what I'd like to be able to do is have a dropdown on Form 1 that only has the Items listed on the subform selectable, but I can't seem to find the SQL code to do this.
My current dropdown uses the following code, but of course this just shows all items, not the ones only on the subform:
SELECT [Parts].[ID], [Parts].[Item] FROM Parts ORDER BY [Item];
What I'd like to do would be like this I think, but obviously using the correct syntax:
SELECT [Parts].[ID], [Parts].[Item] WHERE [Parts].[ID]= & Me![ID] FROM Parts ORDER BY [Item];
Put this in the form's Load event:
Me!MyCombo.RowSource = "SELECT [Parts].[ID], [Parts].[Item] FROM Parts WHERE [Parts].[ID]= '" & Me![ID] & "' ORDER BY [Item];"
Me!MyCombo.Refresh
You will need to take the single quotes out of it if Parts.ID is a Numeric field, and leave them in if it's a Text field.
Use the form's "Current" event to set the combo's RowSource property, so whenever the active row in your form changes you get the updated list on your combo.
Me!MyCombo.RowSource = "SELECT Project, Item FROM Parts WHERE Project = '" & Me.Project & "' ORDER BY Item"
Sorry, user2174085: This should be a comment on you answer, but I don't have the option to make comments available.

Dlookup in a form with 2 subforms (The requirement for the dlookup is in the second subform)

Access 2007
Form Name is: MineLocationQuery1
1st Subform name is:MineExtractionSubform
2nd Subform name is: ExtractionLineSubform2
I am trying to use dlookup to get check a value in a table and then multiply it by another value. It works with: NO Subform and with 1 Subform. But when I add 2 subforms I cannot get it to work.
Here is what I have tried so far
Simple form lookup that works:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [Forms]![ExtractionLine]![AtomicRef] & "'")*[Tonnage]
Here is the working example from a form created with 1 subform:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [Forms]![MineExtraction1]![ExtractionLineSubform]![AtomicRef] & "'")*[Tonnage]
And here is 1 of the many attempts I have made with 2 subforms and it is not working.
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & Forms![MineLocationQuery1]!MineExtractionSubform.Form!ExtractionLineSubform2.Form.AtomicRef & "'")*[Tonnage]
What am I doing wrong other than getting tired and frustrated at copying and pasting many attempts into the worths textbox control source and repeatedly getting #Name errors
If I wish to refer to a nested subform in a textbox on the main form, I might say:
=[SubformControl1].[Form].[SubformControl2].[Form].[AControlName]
Note that this uses the name of the subform control and Form as a reference to the object contained by the control.
So this (split for ease of reading) looks right:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & Forms![MineLocationQuery1]!
MineExtractionSubform.Form!ExtractionLineSubform2.
Form.AtomicRef & "'")*[Tonnage]
However, it is very easy to get names wrong. For example, you may be referring to the form contained, rather than the subform control name. You can use the Expression Builder to troubleshoot, or you can refer to the form bit by bit in the immediate window (Ctrl+G) to ensure you have the names right. For example:
?Forms![MineLocationQuery1].Name
?Forms![MineLocationQuery1]!MineExtractionSubform.Name
More information: http://access.mvps.org/access/forms/frm0031.htm
EDIT re comments
If you work in subform 2, as I believe you should in this case, you can simply refer to [atomicref]:
=DLookUp("[Price]","[Resource]","[AtomicRef]='" & [atomicref] & "'")

Populate Text box on access form based on the value of combo box on the same form [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MSAccess - populate text box with value from query
I have a single form on which there exists several combo boxes and text box. one combo box value (for the Wells) will be filled independently, then I needs the text box to have its value based on the value of the wells combo box value.
I have created a query that solved the propblem partially, it requires parameter which the wells combo box value. If I ran with query a part from the form, it wroks good and asks for the parameter and it is OK.
I do think to make use of VBA, adding a code which process a SELECT statement (of the query mentioned above), then to tell it to take its parameter from the wells combo value which will ready on the form.
Can someone help on this. Can this works as I descriped.
Thanks in advance.
Mohamed
Further to my question above, I have tried the following solution:
Private Sub Well_ID_Change()
Last_Ref.ControlSource = " SELECT TOP1 New_Ref FROM" & _
" BSW_Transactions WHERE BSW_Transactions.New_Ref Is Not Null AND BSW_Transactions.Well_ID = " & Me.Well_ID.Value & _
" ORDER BY BSW_Transactions.Sample_Date DESC"
End Sub
the Last_Ref is the text box I want to fill in with result of the embedded SELECT statement in the code. The Well_ID is the combo box which value will be the parameter of the SELECT statement. The Well_ID is number field and it displays the well_name and stores the associated ID value in the table. Upon running the form after saving changes, the Last_Ref text box showed (#Name?). I guessed that the text box (is a number field) found a text in the combo box Well_ID, so I added ".Value" to the above syntax at the criteria Me.Well_ID. However the problem still exists.
May I mistaken in the syntax, would someone help on this. Can this works fine?
Thanks in advance.
Mohamed
You are doing this the wrong way around. Bind the form to a table, the wizards will do it for you, then add the combobox. Choose "Find a record on my form" from the wizard. The code or macro to do this will be generated. Once this is done, selecting a record in the combo will populate the form with the data for that record.
Try this in the AfterUpdate event of your combo box:
Private Sub MyTextBox_AfterUpdate()
MyTextBox.ControlSource = DLookup("WellsField", "WellsTable", "CriteriaField = '" & [MyComboBox] & "'")
MyTextBox.Requery
End Sub
This will tell your TextBox to lookup the value in your Wells table and the Requery ensures the value is up to date every time your combo box is updated. Note the ' in the criteria part of DLookup. These ' are only required for String values.