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

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.

Related

Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.
Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

problem when try to Automatically fill in Access text boxes based on a combo box selection

Hello every one please can you help me
I tried this code
=DLookUp("[b]","table1","[table1].[a] = " & Str([Combo332].[Text]))
in access form
to get value b from table1 based on combobox to put it in text box in the control sourse
but when I change the value in combobox I get nothing in textbox I get this #Type!
can some one help me thanks

Getting a Value if a Field and Record number is known?

Good Evening,
I am working on a Combo Search Form that is designed to search for information by criteria. The form has a combo box containing field values and a text box beside it. The selection of a field value in the combo box will fill in the text box beside it with the relevant information for that record, all the relevant information is contained in the PetTable.
I have managed to get the combo box to display the fields from the PetTable by setting the rowSource to PetTable and the sourceType to Field List... however that's where I hit my dead end.
In the Text Box beside the combo-box I tried grabbing the value of the combo box and putting it into the textbox by making the Text box control source "=ComboBox", however this just created a textbox which has a literal text string to that of the combo box.
My next thought was to make the text box Control source "=PetTable.PetComboBox" my thought was that the PetTable references the table with my information and the "PetComboBox" becomes the field a need to get. This did not work either and gave a #Name error"
What should be happening is: In the Combo-box if I selected [Pet Name], I would hope that the textbox beside it becomes "Fido" but instead it also becomes [Pet Name].
Any and all help would be appreciated!
Thanks
Desired Effect
What you need to do is to change the Row Source Type of the Combo Box to "Table/Query". Then in the "Row Source" click on the "..." to open up the Query Builder. Select the table that you want. Add the columns that you want. I would suggest the table's primary key PetID, and then any other fields - in your case at least PetName. You may also want to sort by PetName to make it easier for the user to scroll through. Close the Query Builder and save the changes. Change the combo box's ColumnCount to 2, and set the Column Widths to be "0cm;6cm" (setting the first column to have a width of 0 means that it is not displayed to the user).
Now move to you TextBox, and set the Control Source to be:
=[Combo0].Column(1)
Note that columns in a combox box are 0-indexed, so the first column is column 0, the second (in your case containing PetName) is column 1.
As you actually want to show the field names, rather than the data in the combo box, then you will need to set the RowSourceType to be "Field List", and then select the table name as the RowSource.
You will then need a small piece of VBA to lookup the value of that field in the table for the current record:
Sub sListFieldData()
If Not IsNull(Me!Combo0) Then
Me!Text2 = DLookup(Me!Combo0, "tblPet", "PetID=" & Me!PetID)
Else
Me!Text2 = ""
End If
End Sub
And you will then need to call this procedure in the combo box's AfterUpdate event (to catch when it has been changed by the user) and also in the form's Current event (to catch when the user moves between records):
Private Sub Combo0_AfterUpdate()
Call sListFieldData
End Sub
Private Sub Form_Current()
Call sListFieldData
End Sub
Regards,

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

Microsoft Access 07 VBA: Using Text Box or Combo Box as a Control to hide Columns

I currently have a form. The form contains a subform that displays a Query. The Form also has 2 Text Boxes and buttons for both Boxes. The first box acts as a filter for the query to filter specific record. The second box is intended to be used to hide columns in the query. My issue is that my code will not recognize the text box, or any outside source from my form as a Field Name to be found. Here is my current code:
Private Sub Command137_Click()
Forms![Vermont]![Query1 subform].Form.[Query1 Field Name].ColumnHidden = True
End Sub
Currently if I replace "Query1 Field Name" with any field name that exists in the query the column will hide. However if replaced with anything else I recieve the following error:
"Runtime Error '2465'
Microsoft Access Can't Find the Field '|' reffered to in your expression"
I am pretty sure that I am not referencing the Form Control correctly. I have tried replacing [Query1 Field Name] with the following:
[Text142.Text]
[=Text142]
[Text142]
[Forms![Vermont]![Text142]]
I am very new to VBA but I definitely feel as if this is an easy fix; if possible.
Thank you in advance for any help!
Subform references are always a bit weird. This should work, though:
Forms![Vermont]![Query1 subform]![Text142].ColumnHidden = True