Access combo box filling text box - sql

I want to use the table "services" which is filled with (id, service, price) at the moment i have a combo box (combo51) which lists all of those. now i have to get price from the selected thing in the combo box and get the price for it.
i have a query, but honestly i have no idea how to apply it to the text box and if it would even work.
SELECT price
FROM services
WHERE Services="Combo51";
From what i've seen in other topics they don't really make much sense to me, if someone could just explain basically what i have to do it'd be appreciated.

You shouldn't need to query the db after the combobox values are populated. Set the bound column property of the combobox to whatever value you want, and access it with combo51.value
If you insist on querying the db again, try something like this (assuming ID is the bound column):
msgbox dlookup("price", "services", "id=" & combo51.value)

You are already halfway. In the VBA code dialog, you can refer to the ID easily, e.g.
Debug.Print Combo51
but if you want the price, assuming the price is the third column in your table, use
Debug.Print Combo51.Columns(2)
So, if you want it displayed in a separate text box, add an 'On Change' event to the combo box and add the following code:
TextBox = Combo51.Columns(2)
Now every time you make a selection in the combo box the price will be displayed.

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

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,

Spotfire: Need to select all values in a Multi select List Box Document Property

I have zero knowledge of coding. Can someone please help me with code.
My situation is as follows:
Dashboard has a Multi select List Box (Document Property). I am using that to restrict the data and calculations.
User does not want to select values each time. He wants a button where in (s)he click it once and all the values present in the list box gets selected.
Can someone please help me.
Regards,
Subro
This is fairly easy to do. Let us say that the Document Property that the list box is used for is named "Test" and it is of type "String List". Then you can use an IronPython script like so:
Document.Properties["Test"] = ["First Choice","Second Choice","Third Choice"]
Where the string list you see above, are all the values in your list box. Make sure that the strings you assign to the document property are the ones you want to populate your list with, since sometimes the displayed values can be different than the ones assigned to the document property(The case of using the "Fixed Values" option when setting up the control).
Hope that this helps.
Zenios

MS Access: text box value show #Name?

I have created a form and set the form's RecordSource to a query. Below is my query:
SELECT GeneralT.*, SalaryT.[Status]
FROM GeneralT INNER JOIN SalaryT ON GeneralT.Deposits = SalaryT.Deposits;
In the form I have 4 textboxes. In the first 3 textboxes I show value from GeneralT table and in fourth textbox I show SalaryT.[Status] value. But this fourth value doesn't show in the textbox rather it show #Name?.
If I open the query in datasheet view I can see all the value properly. I don't understand what is wrong here. Please help me to show the result properly in the form.
Displaying #Name? for a field says that field has a control source that does not match with the query linked to the form. Some things to check:
Make sure Status is a selection from one of the pre-existing options in the Control Source drop down combo box. Click on the combo box to make sure.
Double check to make sure it is a "Text Box" and not a custom
control.
Make sure there isn't another text box named Status
Try chaning the control source to just Status instead of SalaryT.[Status]. If the field name does not conflict with the naming of a field in GeneralT, the selected SalaryT.[Status] will actually be displayed named Status.

Populate list box from a table in vba

I am developing a vba form for employee database, in that there is a search criteria for userid and employees name with the userid should be displayed in a list box control which comes from a single table
I need to populate a list box with value from a table according to a value in a text box which act as a search box (eg: userid)
Please help me how to do this
You question is hard to answer because it depends on some things like data types of the search field etc. So this answer is going to be vague on some of those points...
First off you need to create your listbox with search criteria that will look on the form for the search value and filter accordingly.
You do this by setting the the RowSource property of the listbox. HEre is an example rowsource for a a listbox that looks for a textbox on a form for its filter value...
SELECT tblAgencies.AgencyID, tblAgencies.OrganizationName
FROM tblAgencies
WHERE (((tblAgencies.OrganizationName)
Like "*" & nz([Forms]![frmMainMenu2]![txtSearchAgencies],"") & "*"))
ORDER BY tblAgencies.OrganizationName;
The key part is the Like... line. A couple of things about it...notice that the query looks to the form for some criteria. You see that in the [Forms]![frmMainMenu2]![txtSearchAgencies] part of the query. So there is a search textbox on frmMainMenu2 that is called txtSearchAgencies.
Notice also that I am using NZ function to ensure that the peek onto that textbox returns at least an empty string. Finally notice that is uses the Like operator with wild cards at both ends so that the user can type a partial string.
Finally...next to your search box put a command button to execute the filter/search. All that it has to do is REQUERY the listbox like this...
Me.lstAgencies.Requery.
You could also try to Requery at the OnChange event which would filter as they type. But if your query is slow this may not work well.
Seth
Let's say you have a table TABLE1 that has fields userid, employee.
You should create a form that has a combobox(named boxid) and textbox(named EdtEmployee).
Define a rowsource value of combobox like
SELECT table1.userid FROM table1 WHERE employee like EdtEmployee & "*";
Then define a lostfocus event of a textbox like this
Private Sub EdtEmployee_LostFocus()
BoxId.Requery
End Sub
I hope, this works for you
I agree with mik.
I just would use an AfterUpdate event instead of the LostFocus which I've neber used.