How do you make a dropdown list to populate its list based on other dropdown items selected? - vb.net

I have 3 drop downs in vb.net which are querying the database. I want the user to be able to choose the first drop down item, and based on his/her selection the third drop down does its respective query. How would I do that?
cmdString = "select distinct t.desc, t.id " & _
" from Table t" & _
'" where t.id=" & Me.ddFilter1.SelectedValue & _
I have commented part of my command string; I am using the selected value from my first dropdown, however when I Run to Cursor the value stays the same. my third dropdowns which is suppose to change shows nothing, because data reader is empty (since it cannot get anything off of the query in cmdstring)
how come the selectedValue in my first dropdown (ddFilter1) does not change when user selects a different item from first dropdown?

you could switch the selected item of the first one. and then depending on which case it is fill the next dropdown with values?

where t.id=" & Me.ddFilter1.SelectedItem.Value
Has fixed the issue.

Related

VBA to select Listbox value based on table value

I'm certain this is a basic issue, but I can't find an answer. It may be wording my question poorly.
I have the listbox on a form linked to a table. On that table I have a field called "Functional Area." This field automatically gets populated based on the user's office symbol.
The listbox lists all of the functional areas.
I want the listbox to automatically start with the user's functional area selected. The user can select a different functional area from there.
This is my code, it works, but obviously selecting the 0 value of the listbox doesn't get me what I'm looking for.
'Detect Office
Me.Office = DLookup("[Office]", "*Local - User List - Active Directory", "[UserID] =" & "'" & Me.UserID & "'")
'Detect Function
Me.Function = DLookup("[Functional Area]", "0 - Active User - Funtional Area")
Me.[Functional Area] = Me.Function
'Set Listbox Value
Me!LetterSourceListBox.Selected(0) = True
Me.Refresh
Is there a way I can find the row number of the item in the listbox and then select that?
Thank you,
-Stephen
Per June7's comments, I just needed to make the functional area a bound field and everything works nicely!
Cheers!

Append 2 rows from 1 row with unique data in each

Very new to Access, thanks for your patience:
I have 2 tables which are records of bridge supports. The supports are numbered. In one of these tables there is one row of information for each support #, lets call it tblSupport. In the other table I would like to have 2 rows corresponding to each support #, each row corresponding to one side of the bridge or the other (e.g. left, right)--lets call the table tblPosts.
I have a form that allows a user to input a record for a new support in tblSupport, including the identifying # for that support. I currently have it so once the info is entered and a "save" button is clicked, it adds the record for that support to the table and appends a row to tblPosts with the same support # so that another user can enter data regarding the posts for that support.
Each time a user enters data for a new support in tblSupport (via a form) and clicks "save" I would like to append 2 rows to tblPosts, both with the same support #, but in another field called 'Side' I would like one row to show 'Left', and the other to show 'Right'. So then tblPosts would be ready for the next user to enter data for both the Left and Right posts for a single Support.
Any tips on how to do this? Please let me know if I need to clarify anything
That would be two sql INSERT actions. But have to make sure the parent record is saved first. Assuming SuppID is text type field, something like:
If Me.NewRecord Then
DoCmd.RunCommand acCmdSaveRecord
CurrentDb.Execute "INSERT INTO tblPosts(SuppID, Side) VALUES('" & Me.SuppID & "', 'Left')
CurrentDb.Execute "INSERT INTO tblPosts(SuppID, Side) Values('" & Me.SuppID & "', 'Right')
End If
The real trick is figuring out what event to put this code into. Maybe SuppID textbox AfterUpdate.
Your approach here could be improved. If your form inserts a row into the row table when the user saves the support record you're generating a row in second table that consist of only a foreign key and no substantive data. A better way to handle this is to only generate rows in the post table when the user needs to actually record data about posts. Then you won't have any rows sitting around with no data.
So first, change your existing save button to only add a record to tblSupport. Then you would create a second form for entering data for tblPosts, and either embed it into your existing form as a subform or add a button like "Add Post Record" to your existing form that opens the second form. I'd use a subform.
The key to making the subform work is to set the Master/Child link fields. You'll find that in the "Data" section of the subform properties, which looks like this:
You would use your "support #" ID field where my example has "HRS_Pkey". This sets the relationship between the two fields, using the "support #" value to link the records in the two tables. When the user enters data into the subform it will be added to the child table with the appropriate "support #" value. You can design the subform any way you like; I tend to use datasheet subforms, but since you always want 2 child records for each parent record you'd likely want something more static.
Due to inexperience with subforms I've chosen to go the route suggested by June7. To clarify here, the 'supports' and 'posts' I've been referring to for demonstration's sake are actually called 'bents' and 'micropiles.'
Ultimately I wanted it to be the case that when the bent person clicks "Save Changes" on 'Bent log form', the list of bents on 'Micropile log' is updated to reflect the same--but I do not want to append duplicate records--the LEFT JOIN ... WHERE.. IS NULL statement takes care of that. And for every new record entered, I wanted 2 rows added to the Micropile log with the same Bent #, but with a row each saying 'River' and 'Mountain' in the 'River/Mt side' column.
It's messy but it seems to work:
DoCmd.RunSQL "INSERT INTO [Micropile log]" & _
"SELECT [Bent log].[Bent #]" & _
"FROM [Bent log] LEFT JOIN [Micropile log] ON [Micropile log].[Bent #] = [Bent log].[Bent #]" & _
"WHERE [Micropile log].[Bent #] IS NULL;" 'Appends added bents to Micropile Log table
DoCmd.RunSQL "UPDATE [Micropile log]" & _
"SET [River/Mt side] = 'River'" & _
"WHERE [Bent #] = [Forms]![Bent log form]![BentNumber]" & _
"AND [River/Mt side] IS NULL;"
If howManyBents < 2 Then
DoCmd.RunSQL "INSERT INTO [Micropile log]([Bent #], [River/Mt side])" & _
"VALUES([Forms]![Bent log form]![BentNumber], 'Mountain');"
End If
variable 'howManyBents' is a DCount function that counts the number of instances that the value in the text box for the record being entered appears in [Micropile log].[Bent #]
Any further suggestions are always appreciated. Thanks to both #Rominus and #June7 for prompt and concise responses to the original question.

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

MSACCESS - Closing Form edits 1st entry

I have table with 3 Columns. It has a AutoNumber Primary ID (cboID), A Text Column (txtLocation), and a Number Column (txtCount).
From this table, I made a form that has a combobox and two text fields. When the combobox is clicked, it shows a list of all the cboID's. After selecting one of the choices, it autopopulates the two other text fields with it's respective Location and Count info. I also have an update statement/save button that allows the user to edit one of the entries, press save, and update one or both the fields.
Here's the statement I have.
Private Sub cmdUpdate_Click()
Dim strSQL As String
strSQL = "UPDATE aDuh SET Location = '" & Me.txtLocation & "', Count = '" & Me.txtCount & "' WHERE ID = '" & Me.cboID & "'"
DoCmd.RunSQL (strSQL)
End Sub
The updating has no problems, but the form updates the 1st entry when I close it. Lets say I just finished editing the third entry and I'm done editing. I want to close the form and do something else. However, I close the form (I've tried putting a button and just closing the tab) and the 1st entry in the table is updated with the third entry. So basically, my last edited entry edits properly, but when the form is closed, it also updates the 1st record. Any idea of how to fix this? (If this isn't clear please let me know and I'll try to clarify)
I'm thinking of writing a statement for the On Close event but I'm not sure what to put.
EDIT: Extra Info
Right now, this table only has three records. In the form, when I click the combobox, it has the drop down list of the three records. Lets say I clicked the second one. After clicking it, the Location field is populated with the related info and same with the count field. Now lets say the current value in the count field is "9" and I want to change it to 4. I type in 4, press update and I'm prompted with a box saying "are you sure you want to update 1 row". I press yes, and it updates. Updating works fine, its just when I close. I'll give two examples
1) I just edited the second record, when I close, it doesnt prompt anything, it just closes smoothly, but when I check the information, the first and second record are now the exact same.
2) Lets say I just edited the first record. When I close it, a window pops up saying "Write Conflict, This record has been changed by another user since you started editing it. If you save the record, you will overwrite the changes". It then has three options, Save Record, Copy to Clipboard and Drop Changes.
Oh and how i'm making it autopopulate, I have the code:
Private Sub cboID_Change()
Me.txtLocation = Me.cboID.Column(1)
Me.txtCount = Me.cboID.Column(2)
End Sub
Hopefully this clarifies a bit more. :(

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.