VBA to select Listbox value based on table value - vba

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!

Related

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

sql show listbox values only when combobox is populated

Firstly, I'm pretty new to access so I apologise if this is basic but I can't find the right answer either.
I've had some help setting this up so there are parts that have been created that I don't fully comprehend.
I have created a form that is to be used for data entry which consists of the following fields:
TEAMS - Combobox the rowsource of which is a table TEAMS; CALLREASONS - Multi-Select Listbox the records of which currently exist in a table CALLREASONS; ACTIONS - Multi-Select Listbox the records of which currently exist in a table CALLACTIONS; SUBMIT - Button with VBA which sends selected data to a number of tables.
The database works fine and things a saving where they should be and in the format they should be, the problem I'm having is with the display of the form.
I would like for the form to display blank when opened and after each time a record is submitted. However, if I set the rowsource of the listboxes to the tables all options show when the form is loaded and after each submission the options and the previous selections show.
How can I make it so that the first listbox CALLREASONS only displays possible options once a selection has been made in the combobox TEAMS and the second listbox CALLACTIONS only displays possible options once at least one selection is made in the listbox CALLREASONS ?
Thanks in advance for any help.
You can either change the controls row sources on the Update or Enter events, or you can simply enable/disable them on the events.
Lets use 3 nested comboboxes for an example.
Combo1 Has rowsource set permanently as it is the top level.
Combo2 and Combo3 Have no rowsource set.
On the Enter event of Combo2, you can set the rowsource to
SELECT Pkey, DisplayText FROM tblOptions WHERE OptionGroupField = " & Forms!Form1!Combo1BoundField
On the Enter event of Combo3, you can set the rowsource to
SELECT Pkey, DisplayText FROM tblOptions WHERE OptionGroupField = " & Forms!Form1!Combo2BoundField
So on so forth.

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.

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.

VBA for taking information from text boxes and inserting into table

So I have an input form that I want to use to update a table with certain fields of information. I have the ID of the record automatically coming up in a text box. I have three text boxes that I need to add to the table (excluding the ID) on a button click.
Name
Date
Method
are the field names.
As_Name
As_Date
As_Method
are the text box names
The table name is POC, and the ID is POC_ID (its an autonumber).
So I do not want these objects (text boxes) to be bound to the table because this is a little separate "pop-up" form that comes from a form that the table, and I only want the input to be relative to the POC_ID that is already selected via the original form.
So how do I write the vba for this to 1)check to make sure that records do not already exist....2)update the fields (listed above) with data input from the text boxes(listed above). I want to be able to use this with a button click....
EDIT:
actually it is one table not two; i have two forms that I want to be able to send information to the same table (different information though). this db was already built by someone else and know I have been deamed to take it over.
I need to add a second little pop up form for additional information to be added based on new requirements (there is literally no where for me to place this on the other one). I have already done that, and used a suggested object approach to reference the first forms (from which this second "pop-up" form springs from) to add the relative id fields. Now I have this second little pop up form that just asked three values to be inputted (which are the ones listed above).
I just simply do not know how to link the text box, with a field so that once a user enters in the information, clicks "save" it saves the information to the table relative to the TripID that is there (one mentioned above). the only way I know how to get the text boxes to save data to the table is to use the builder/wizard when I create a new one.
I would like to learn how to link an object (text box, cmb, list) etc on a form, to a table with an "On Click" method so that I can use a save button. Basically that is it!
The OpenForm method of DoCmd allows for several arguments, including Where and Openargs. You can take advantage of these.
However, something seems to be quite wrong with your table design in that you appear to be holding the same information in two tables and for no stated reason. Have you read http://www.r937.com/relational.html?
I would suggest that the design you need probably only includes a numeric field POC_ID that is a foreign key to the main table.
Still not sure I understand your situation, but let me offer the outline of an answer. If my outline is not close enough, please explain where I went astray.
Your parent form, frmParent, has a command button (cmdMoreFields) which opens the child form (frmChild) where you will enter values for 3 additional fields in the record currently displayed in frmParent. After the user enters those values in frmChild (in text box controls named As_Name, As_Date, and As_Method), she will click a command button (cmdSave) to store those values to fields (Name, Date, and Method) in table POC, and close frmChild. Also, frmParent includes a text box (txtPk_field) which holds the value for the primary key (field pk_field in table POC) of the current record.
However, I'm not sure which field/control you're using for txtPk_field, and doubt that value is available if the the current record has not yet been saved. So, I'll suggest this code for the cmdMoreFields "on click" event:
If Me.Dirty Then Me.Dirty = False
DoCmd.OpenForm "frmChild"
In the "on click" event of cmdSave (on frmChild), try code similar to:
Dim strSql As String
strSQL = "UPDATE POC SET [Name] = """ & Me.As_Name & """, [Date] =#" _
& Me.As_Date & "#, Method = """ & Me.As_Method & """ WHERE pk_field = " _
& Forms!frmParent.txtPk_field & ";"
Debug.Print strSql
CurrentDb.Execute strSql, dbFailOnError
DoCmd.Close
If that approach works, consider passing the pk_field value to frmChild with Openargs, as Remou suggested.
Note: I assumed the data type for Name is text, Date is date/time, and Method is text. You will have to change the UPDATE statement delimiters for any fields whose data types differ from my guesses.