how to populate textbox with the database value based on combobox change in access - ms-access-2007

I am new to Access and vb and i have failed to get the result what i am thinking to get ,hope i will resolve with your help guys
->my access database contains one table with following fields
firstname lastname middlename phone
I have two combo boxes and two textboxes
combobox- it populates firstname and based on this selection the other combo box has to fill with last name which i did successfully with the help of Me.Combo2.Requery
but i dont know how to fill middle and phone in textboxes
here i am failing in mapping recordset and connectionstring with my table and getting result to textbox value can any one help on this

You still have not said where the connection string comes from or why. The big advantage of Access is that it is fast and easy, for the most part, you do not need connection strings.
First, select your table, then choose create form. This will create a single form bound to the table. You could have created a query first and chosen that instead.
That is it. You have a form that displays your data. The key to this is the property sheet for the form, which you can find by switching to design view and double-clicking the small square at the top left of the form just under the form name. The property sheet will show the name of the table or query on the Data tab under the Record Source propery. You can, of course, set this property manually.
You can now add a combobox to select records, but you do not have to, there are navigation buttons at the bottom of the form.
To add a combobox that selects records on your form, you must have first bound a recordset to your form using the steps above. Next, ensure that the wizard is selected for the toolbox. Unless you have unselected it, it will be selected. Now choose combobox from the toolbox, it will open the wizard.
Choose find a record on my form and press next to choose the relevant ID field / column and any other fields that you need. When the wizard completes, in MS Access 2010 (and probably in 2007) it will add an embedded macro that finds your record and in earlier versions it will add code. Other properties set by the wizard will look something like this:
Row Source : SELECT ID, Firstname, Lastname FROM Table1
Column Count : 3
Column Widths : 0cm;2.54cm;2.54cm
Your table should have an ID, add one before you start, if it does not. The wizard will not add a Control Source when this option is selected because it is not needed, in fact, it would be a disaster to add a Control Source to a combo that finds records. Once again, you can do this manually. The code to find a record might look something like:
Private Sub MyCombo_AfterUpdate()
With Me.Recordset
.FindFirst "ID=" & Me.MyCombo
End With
End Sub
This works because the bound column of the combo is set to 1, the first field of the select statement, which is ID.
The combo includes two other fields / columns and you can refer to these in a textbox by setting the control source of the textbox to:
= MyCombo.Column(2) ''Lastname
Where you count columns from zero:
Row Source : ID (0), Firstname (1), Lastname (2)
Cascading combos are another story altogether, and you can read it here How to synchronize two combo boxes on a form in Access 2002 or in Access 2003 and here Is there a simple way of populating dropdown in this Access Database schema?

Related

Show a name instead of an id

I currently have these options set in a combo box:
This box will show the projectIDs that a current company has which is set with the criteria. I just want to show the name of the project as people will not know just by the id. If I remove the check box for the projectID it will just remove that field completly and not show anything. I have debugged what companyBox.Value is and it is indeed a number for a company and if taken this id and replaced it with the following query which is built from the picture and it returns the results I want. I just cant get the values to show in the combo box.
SELECT projects.projectName
FROM companys INNER JOIN projects ON companys.companyID = projects.companyID
WHERE (((companys.companyID)=7));
Gives me all the projectNames where the companyID is 7
And as seen below that same query just gives me blank spaces instead of the names:
Ok, if you have say a query, then as noted, then you can use a "join" to pull in the other table (based on that "projectID").
So, for a general report etc., then your approach of using the query you have is correct.
HOWEVER!!!
For a combo box? They have this feature and ability built in. In other words you do NOT in general need a join.
The combo box has two parts:
A sql query that "drives" or "fills" the combo box. This can (and will be) of course based on the Projects table.
VERY important:
Our combo box is to save, store, put, use the project ID into a column in our CURRENT form. That form of course is based on a differnt table (the forms current table). So, keep in mind the two concpets:
Combo box can be driven by any table to display data.
Combo box will/can use a column from that "other" table to SAVE into a current column on the current form.
Now of course the Project "id" is the VALUE we want, but we sure as don't want to display that "project id" value, since as you note, humans want the nice looking text.
First Rule:
ALWAYS, but ALWAYS make sure the FIRST column of the combo box "sql query" is the column we WANT to save into the current form, but ALSO the column we are going to HIDE AND NOT show to the user.
So, a combo box can with great ease HIDE the first column. You can hide other columns, but as a general rule a VERY HIGH number of my combo boxs will have two columns. So, you might need company "id", but would want to display company name as nice text to the user, but store/save/use the "id" of company for this purpose.
So, in your case? Change the order of your columns.
You want:
ProjectID, ProjectName, and you ALSO can continue to have a filter based on company.
So, once you get above setup, and you WILL NOT need a sql join. Remember, the combo box has it own "whole sql statement" based on table projects.
So, the combo box will:
Save/store/use the ProjectID when you select a project.
And it will display the project name, but behind the scenes it will use + save ProjectID.
So, just make sure that you set the length of the first column in the combo box to 0 (to hide that projectID from display).
Next, make sure you set WHICH column value from the query the combo box is to save.
That column will be the FIRST column, so you want to set that to 1
Your combo settings will look something like this:
In above, the CONTROL source is your CURRENT form and table.
So, I want to get a Hotel "ID" from the table hotels, but I am going to save the results of the combo box selection INTO a column called Hotel_ID
And note VERY careful - I set the bound column = 1 (that is the FIRST value from the query that drives the combo box.
Next up:
We want to hide the first column value, so you need this setting in format tab of the property sheet for the combo box:
NOTE VERY close in above. I set the FIRST column width = 0. this is HOW you hide the "ID" of hotels - I only want the user to see the nice hotel name. (or in your case Project name).
But, the 2nd column, I have a width for the Hotel name (or in your case project name).
So, make your first column in the query for the combo box the "id" of project id. Set the width of the first column = 0.
So, the query that drives the combo box? It is based on the ONE table, but you need to ensure that the "id" that you going to use is the first column, and simple hide it from display.
So, a combo box has a sql query that drives the combo box, and that query in most cases will NOT be the current table.
So, you have to set BOTH settings (the column the combo sql query will use - but to avoid confusing, then just adopt the habit of making the 1st column the "id" or value you want from that sql.
But, after setting above, you STILL have to set what column to shove/put that column into on the current form. (that setting is the control source).
In effect, a combo box is a kind of look-up into the other table, and it can display nice user friendly text columns, but still use + store the ID.
So, your query should look more like this:
SELECT ID, projectName
FROM projects
WHERE (((companys.companyID)=7));
I don't know if your first column of Projects table is "ID" (or is it ProjectID), but as you can see, the combo box ONLY needs to be based on the one table, and with our new rule - we always use the first column of the query the "id" value we want.
Now, of course the above is "hard coded" for company, and your original query that drives the combo box was fine - just that the order of the columns display was incorrect.

Update table column line by line based on yes/no values from combo box

I am very new to Access and VBA but what i'm trying to do is simple:
I have a table which i want to show in a form, i have put said table in a subform box.
I want to have a column in which i want to update the status based on options from the combo box (yes/ no/ maybe).
So as shown in the picture: i want to select a line in the subform and update the column status by selecting an option in the combo box.
You can modify the selected record in the subform in the AfterUpdate event of the combo box.
Private Sub cboStatus_AfterUpdate()
subForm1.Form!Status = cboStatus
End Sub
This assumes that your combo box control is named cboStatus, your subform is named subForm1, and the field you want to update is named Status. It also assumes you aren't using numerical ID's as foreign keys for your values.
You will only be able to update one row at a time.
Beyond the scope of your question....
There are numerous other ways to develop an interface to edit the row. One way is to modify the field Lookup properties in the table. You can change the Display Control to a Combo Box, set the Row Source type to Value List, and then set the Row Source to a list of possible values separated by semicolon.
Now whenever you open that table in a datasheet view (like the example on your subform) a dropdown will appear in that column. A user can edit directly in that view without requiring a separate combo box control.

Limit Combobox input based on other control on same form

I have a database in access for which I made some forms, which is going pretty well.
I only have one small issue I am running into.
I have this form on which I have several comboboxes, which get their Row source from another table. That is working fine.
But for combobox B I wanted the options limited/filtered based on the selection in combobox A. So I filled the following in the row source of combobox B:
SELECT JOBS.Numbers
FROM JOBS
WHERE CITY = Me.CITY
So there is another combobox on the same before where the CITY is picked.
Now when open combobox B it asks me to fill in the city manually and then it filters/limits the options in the way I want. However, I want this to happen automatically based on the selection in combobox A (on the same form).
I hope you can help me.
Thanks in advance.
I find this feature very useful. For example I have a form where project managers select there name from a combobox(A) then in the next box(B) select their project. B uses the criteria from the first box to only list their projects.
Specifically the project table I pull the list from has an owner field. I use the name entered into A as criteria on the Owner to pull just their projects.
On the combobox query build I input the Owner criteria like this. The query runs then with whatever data is entered into boxA.
Forms![Main]![cboOwner]
On the first combo box "After Update" event add Me.ComboboxB.Requery. That will make combo box B refresh its contents based on the current value of combo box A every time A's value is changed.

Trying to open another form in Access based on a field in the current form

I have a form that's just a list of descriptions (desc.) of other forms. Right now, when you click on a desc., the appropriate form opens; but it's done through a series of if statements in a macro. This isn't going to scale well once more forms are created.
I have a table that has the desc. and the form that is supposed to go to. I want to write a script that uses this table to open the new form based on the desc. clicked, but not using if statements. The end goal is to be able to just add a row to the table for any future forms that are created without making changes to the script or form. Is there a way to do this?
Use a combo box whose row source is a query which selects the form description and name fields from your table.
The combo will have 2 columns. You can set the width of the form name column to zero if you want to present only the form descriptions. If you make the form name column the combo's bound column, you can reference it conveniently in a DoCmd.OpenForm statement. For example, you could have a command button whose click event opens the form which is currently selected in the combo ...
DoCmd.OpenForm Me.YourComboName.Value

Combo Box Column Count culls data?

Basically, I have a combo box that I'm using to select a record. That combo box then populates several textbox's that are found on my form based on the ID. See the two images:
.
I'd like the combo box to show the first two columns in my query, which consists of 16 fields. The Abbreviation and the Name, to be exact. However, I found that if I hid the fields in the query or if I limited the column count of the combo box so it only displays the two, it actually culled the data so that the textbox's would not find it (thus be blank).
My question is, how can I just show the first two columns of the combo box yet still use it's ID to reference all sixteen?
Query
SELECT Tradeshows.tradeShowAppreviation,
Tradeshows.tradeShowName, Tradeshows.tradeShowID,
Tradeshows.tradeShowWebsite, Tradeshows.tradeShowLocation,
Tradeshows.tradeShowDateStart, Tradeshows.tradeShowDateEnd,
Tradeshows.tradeShowBoothSize, Tradeshows.tradeShowShipDate,
Tradeshows.tradeShowAudience, Tradeshows.tradeShowFocus,
Tradeshows.tradeShowMailer, Tradeshows.tradeShowDoorDrop,
Tradeshows.tradeShowProductFocus, Tradeshows.tradeShowAttendees,
Tradeshows.tradeShowMembers, Tradeshows.tradeShowMemberships
FROM Tradeshows;
Do not change the column count, change the column widths.
Column width: 0cm;2cm;1cm;0cm
And so on.
it should not be difficult to only select the relevant columns. You could post the Row Source property of your combo.
It seems to me that you should be binding you table to the data source TradeShows and using the wizard to create a combobox that finds record.
You can do this by selecting the table Tradeshows and choosing either create form or form. You will now have a form with the RecordSource property set to TradeShows. Next, choose to add a combobox to the form, ensuring that you have selected the wizard (the magic wand button is highlighted). You will be given the option to "Find a record on my form based on the value I select in my combobox". You can choose this and step through the wizard. When you get to the step where you choose fields, make sure you choose the TradeShowID first, and then the name and / or abbreviation. If TradeShowID is the primary key, as it should be, the column will be automatically hidden in the next step, which will show the fields selected.
The result of these actions will be a combobox with the following properties:
Row Source : SELECT [Tradeshows].[tradeShowID], [Tradeshows].[tradeShowName],
[Tradeshows].[tradeShowAppreviation] FROM [Tradeshows];
Bound Column : 1
Column Count : 3
Column Widths : 0cm;2.54cm;2.54cm
You will also have [Embedded Macro] in the After Update event (2010 and possible 2007) or [Event Procedure] for earlier versions. The wizard code is pretty poor, but that is another days work.
If you still wish to do all the work of setting each control to the (possibly changing) column order, you can follow a similar lay out for the combo, setting counts and widths appropriately.