Select correct row in Access Dropdown in vba - vba

I have a drop down box in a Microsoft Access form. The columns appear in this order:
Field0, Field1, Field2, CompanyName, CompanyID
I am attempting to extract the value of the CompanyID column depending on the selection in the drop down. It happens that there are multiple rows containing the same company name. No matter which row I select containing a given CompanyName, the following code always extracts the value of the CompanyID column in the first of the rows, not the selected one. I need to modify the code to select the proper row, and don't know the row until selected by the user.
MyDropDown.Column(4)

Related

How do I create a form to input new records into a table that contains only ID values from other tables?

I am new to databases, and I am working on a final project for a class. I have a database with tables related to each other as shown in the diagram here:
I want to create an unbound form that allows a user to add a new purchase to the purchase table by choosing their name, category, and store from data already in these tables, and then add purchase amount and date.
Since the purchases table does not contain the names of people, categories, and stores themselves, only the ID values from these tables' fields, I am struggling with how to create a form that will add the correct IDs into a new record in Purchases based on the names from other tables.
I am wondering if this requires VBA? I have tried playing around with the property sheet on forms, but I am struggling with which properties to address/what to do with them.
If anyone can explain at least a starting process to create this form.
Simply, use combo boxes that query Buyer, Category, and Store table data, hides the primary key ids, but shows the corresponding lookup value to the user. Users will select by the lookup value(s) but really are saving the id to Purchases table as new foreign key id.
As commented above, use a bounded form to map combo boxes to table id fields. Once you place a combo box, the default wizard will guide you on the steps but below are key property sheet attributes (which may be automatically set with wizard but can be manually adjusted later).
Data
Control Source: The column in table (i.e., PurchaseID, BuyerID, CatID, StoreID) to store the user-selected data of combo box (i.e., a form control).
Row source: A distinct SQL query of primary table id and all needed values for human searches. This can be a named table or saved query or an inline SELECT statement.
Row Source Type: If using SQL, Table/Query.
Bound Column: The position of primary table ID in the query resultset to be stored as foreign key Id. Usually this would be the first column.
Format
Column Count: The total number of columns from the recordsource including hidden, bound column.
Column Widths: To hide column from view, set its positional number within semicolon delimiters to zero. Preview form to decide how large to space out other columns. Do note: you can extend beyond the Width of combo box using List Width.
Column Heads: Optional and best if more than one column to guide users on the lookup value content (e.g., First Name, Last Name).
As example, for Category combo box on bounded Purchases table form, consider below property values:
Control Source: CatId
Row Source: SELECT CatId, CatName FROM Category
Row Source Type: Table/Query
Bound Column: 1
Column Count: 2
Column Width: 0";2.5"
Column Heads: No

Changing Row Source of a Lookup depending on another field

I'm trying to define the row source of a lookup field by selecting the table name from a separate lookup box.
The catalog of products comprises of about 41 Product Groups, which are then further divided into Types, some of which have over 100 types.
I have a table of Product Groups (41 groups), and I then have a separate table of Types for each Product Group (41 tables). All Type table names are exactly as they appear on the Product Group table. I want to be able to select the Product Group from a Lookup Box, and then select the Type from the corresponding table in a separate lookup box.
The images below should help give an idea of what I'm looking to do.
Set up of my first lookup box:
Set up of my second lookup:
Is this possible, and if so can anyone lend a hand ?
Thanks.
Just to summarize, you will need a single table with your TypeID, GroupID, and any other "Type" related fields.
Your Group ComboBox should have the ID field as its first column (makes the filtering much easier), So your control source should be:
SELECT [ProductGroup]![GroupID], [ProductGroup]![ProductGroup]
FROM [ProductGroup]
ORDER BY [ProductGroup];
Then in the properties for Group ComboBox on the Format tab make your column width 0";x" to hide the ID field.
The control source for the Type ComboBox should be:
SELECT [NewTypeTable]![TypeID], [NewTypeTable]![TypeName]
FROM [NewTypeTable]
WHERE [NewTypeTable]![GroupID] Like [Forms]![frmWithComboBoxName]![CboPGroup]
ORDER BY [NewTypeTable]![TypeName];
And again, if you want to hide the ID field, make the first column width 0".
You should also requery the second combobox in the afterUpdate() event of CboPGroup which will filter the second combobox based on the new selection in CboPGroup. The code (VBA) for that would be:
Forms!frmWithComboBoxName!CboType.Requery

MS Access - Select records by rows in a table which does not have unique column

I have two non primary key columns as Field1 and Field2 & the first few records in the table are sometimes not required for executuion. One record in the table displays the text 'Code','TotalValue' in field1 and field2 respectively, and I want all the records to be displayed which are available next to this record. How could I write the query to select the records by rows when I don't have unique values in a column.
Eg:
Field1 Field2
Billing Transactional File bt_billing_transactional_20140626_00000806.dat
Number of Records 44
Number of Accounts 8
Code Total Value
OR20111534 -624989
OR20111072 -485300
OR20111053 -8333
OR20111063 -267828
I want all the four records from the above table which are displayed next to record 'Code','TotalValue'. And the row in which the text TotalValue displayed is not static (it can be in 6th or 7th row). If I select the particular records, I can move these into the new table easily.
Note: It wont help even though if I have the unique column. Because the row order may change when order by a column
Thanks

How can you update many rows in database with only 1 single data in vb 2010?

is it possible with me.tableadaptermanager.updateAll(me.dataset)?
i have 1 textbox.. i want that its value will be update multiple rows in the same table..
for example.. i'll filter the tbTransaction table with EmpName, datebegin, dateend..
so now the table is filtered with EmpName, datebegin, and dateend..
for example, for the filtered data, they all have status field.. they are not the same because they have different dates.. so i want my value in textbox.. only single value from my vb 2010 textbox to update the status field with the same data one i clicked submit button..
is that possible?

VB 2010 Using DISTINCT in SQL

using vb 2010 on an access database
INSERT INTO UniqueTable
SELECT DISTINCT
1,2,3,4,5
FROM DataTable
This will get only unique rows in all fields and if I only specify the one field I want to be distinct it only inserts the data in that field
How can I import all data from every field where field 5 is unique?
If I set the database field properties to not allow duplicates all import fails.
Thanks
Don't use distinct in this case, you can't specify which field need to be distinct, it works for entire columns selected. Use group by instead, like: ..GROUP BY 5 .. HAVING COUNT(*) = 1. That will return all rows having field 5 value appear only once in the table, in other word distinct.