Hidden Column in Listbox - Access 2007 - ms-access-2007

I have a listbox in Access 2007 that is linked to a table. That table has 4 columns but I only have 2 of those columns visible in the listbox. I use the loop command to identify the selected row but I can only view the contents of the visible columns. I display the visible column when I find it so at least I know I have the right row. It is my belief that the other 2 hidden columns are technically available to me because I see them referenced when I view the SQL associated to the listbox. So I have 2 related questions:
How can I confirm that those 2 hidden columns are actually available to me? And how can I access them (I want to get the primary key associated to the row selected.)
If those hidden columns are available and I wanted to make them visible in the listbox, how would I do that?
Learning Access is now a weekend hobby.Thanks in advance. DaveL

You can make a listbox show whatever you would like. When you have it selected in design view then open the property sheet and select the Format tab. Where it says column count and column widths is where you can modify the obvious. The Data tab on the property sheet will allow you to select all 4 of your fields to appear in the listbox.
Now to use the data in that listbox, you can use the VBA for click() and choose which column you want to work with if you have the primary key associated with that listbox.
Public Sub The_Listbox_Click()
Dim myR as Recordset
Set myR = CurrentDb.OpenRecordset("Table_Name", dbOpenDynaset)
myR.FindFirst ("[Primary_Key_Field] = '" & Me.The_Listbox.Column(0) & "'")
'You can now use that record with myR![Field_Name]
Set myR = Nothing
End Sub

Related

Adding an item to the end of a multi-column listbox

Being a beginner in this, I've tried to search for a solution to my issue. I created a form in Access 2013 with a List Box. The List Box is bounded to a table with 10 fields.
For the List Box, in the Property Sheet, I've set the Column Count to 10, but have hidden some columns, i.e. some has a Column Widths of 0" because I'm only showing important data in the List Box.
The Row Source Type of the List Box is set to Table/Query.
When I open the form, it queries the table and populates the List Box with all data.
I have 4 unbounded Text Box that allows the user to enter data, and have a button to allow the input data to be added to the end of the List Box and also be automatically added to the table.
I've searched for how to do this:
Add items to a multi-column List Box
vba listbox multicolumn add
Adding items in a Listbox with multiple columns
Adding values multiple columns to listbox in form access vba
Excel multi-select, multi-column listboxes
I've tried to use the .List property of the List Box, but it does not exist.
I've tried to do something like:
Me.MyList.AddItem "" & ";" & Me.textBoxValueOne & ";" & Me.testBoxValueTwo & ";" & ""
However, VBA complains I need to change the .RowSourceType to Value List, and when I added it, the List Box clears all the data, and adds this data as the first entry, which is not what I want.
I've added the following code, which adds a blank entry to the end of the List Box as well as table, but I don't know where to go further with it to save the input data from the Text Box to the end of the List Box and table:
DoCmd.GoToRecord , , acNewRec
DoCmd.RunCommand acCmdSaveRecord
Me.MyList.Requery
How can I achieve this?
Thanks.
You need to add the record to the table first and then requery your listbox.
After adding the new record try
Me![column name] = textbox1.value
And other textboxes
Followed by your save command.
Once you've added the record. Simply requery the listbox.
Me.listbox.requery

How to use the value not displayed by a dropdown when connected to access database

My program uses a database in access which consists of BreedID and BreedName. I have a dropdown box in vb.net which shows the name for the user to select, however in my code I would like to store the ID related to that name so that I can send it over to the access database using oledb. Currently this is what it looks like:
Dim BrVar As String = Breed.SelectedItem.Text
But that isn't working! Please help!
You can add hidden columns to your dropdown box, it may already exist. The first column in a dropdown box is column(0) and you can set the width to 0cm. This can be for the ID value. Leaving column(1) for the Name value.
Then you can use Breed.SelectedItem.column(0)
The first thing to do is on the Data tab set up your rowsource to SELECT both the BreedID and BreedName fields (in that order). Then make sure bound column is set to 1.
Then on the Format tab set Column Count to 2 and Column Widths to 0;1.
That will have the result of displaying the BreedName field but using the BreedID field as the value of the combo box.
Use Dim BrVar As Long = Breed.SelectedItem to get the value of BreedID.

Datagridview - fill row from Combobox

I have set a combobox to be visible in column1 of my Datagridview. Now I'm trying to fill same row of Datagridview where Combobox appears, from Combobox_Key_Down event. This is my code for showing combobox:
Private Sub My_DGV_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles MY_DGV.CellMouseClick
If e.RowIndex >= 0 Then
With My_DGV
If .Columns(.Rows(e.RowIndex).Cells(e.ColumnIndex).ColumnIndex).Name = "Column1" Then
.CurrentCell = .Rows(.CurrentRow.Index).Cells(.CurrentCell.ColumnIndex)
Show_Combobox(.CurrentRow.Index, .CurrentCell.ColumnIndex) 'function that shows my Combobox in that cells
Combo.Visible = True
Else
Combo.Visible = False
End If
End With
End If
End Sub
I tried many things, but I don't know how to determine in which row Combobox appears and how give that Datagridview row my Combobox values. Someone please give me a clue of what should I do. Thanks in advance !
The first problem with your approach is that the DGV can have only one DataSource: it can either show the m:m association table or the related elements. If you include columns from one of the tables into the query for display, the table becomes non updatable and users can be confused why they cannot edit something they can see. It seems of little value they way you describe it, since they cannot see the detail data until after they make a selection.
Next, it requires another datatable to supply the details for CboColB. Since you want the DGV bound to a DataTable easy updates, you end up having to poke data into cells over and over.
Finally, consider what the user is confronted with. Using a Country table (200+ countries/locales with ISO code and name) and a list of flag colors, a table for CountryFlagColors will have hundreds and hundreds of rows (at just 2 colors per flag).
A better display might be to filter the m:m table (flagcolor) to a selected item so the user is only confronted with the data subset they are currently interested in:
The datatable used in the DGV is built from the m:m table:
The Country column is hidden.
When they choose from the CBO at the top, that is used as a RowFilter to limit the rows to the relevant ones.
In the RowValidating event, when the country cell is DBNull, copy the SelectedValue from the country combo to the DGV cell to fill in the blank
I would probably really make the user click a button and manually add a row so I could seed the country value then rather than depend on events.
It uses a DataAdapter and after adding X number of flag definitions, da.Update(dtFlagColors) applies/saves all the changes.
Ok, so that provides the core functionality to assign N color selections to define the flag colors for a country. The missing element is the 'details' for the Color item.
I added a meaningless int and string item to the Color table, one way to display these would be to create an alias in the SQL with the important details. Displaying them as discrete elements can either make the query non updatable or invites the user to edit things they cannot edit here. My silly SQL:
"SELECT Id, Name, Concat(Name , ' (' , intItem , ' ' , stritem,')') As Info from FColor"
Then use 'Info' as the display member on the CBO column in the dgv:
dc = DirectCast(dgvCF.Columns(0), DataGridViewComboBoxColumn)
dc.DataSource = dtFlagColors
dc.DisplayMember = "info"
dc.ValueMember = "id"
dgvCF.DataSource = dtSample
The combo column has its own datasource of course, in order to display one thing and use another for as the Value to give back to you. Result (the values are silly):
It is not exactly what you want, but comes close and is much simpler. It also requires almost no code for driving the associative entity. Another alternative would be to use a DGV as the second picker so you can show the extended data and manually add rows to a DGV:
If you set the dropdown style to Nothing, it looks like a text column.

Returning multiple records using Recordset MS Access

I have an MS Access form that has a combo box (which acts as a primary key) called cboProjectID. I am trying to create the form in a way so that once a selection is made in the cboProjectID combo box, it triggers another combo box called cboErrCod1 to display error codes based on those available to the project_id selected in the cboProjectID combo box.
Only certain Error Codes are available to certain Project ID's and those are defined in another table. However, i have created a query called HDR_ERRCODES that relates Project_ID, Project_Code and the possible Error_Reason_Code 's available to a specific project_Id.
For example, a project code (which is selected from the cboProjectID combo box) could look something like these: "FI-01-05", "FI-01-01", "SY-02-02" etc). Once selected, the Project_ID is stored in the table Project_DTA_REV_T (this is the table that the form stores its information) and the Project_ID is simply a next up number (1,2,3,4 etc. etc.).
I understand that you need to use a Recordset to return multiple values when doing VBA code. This is the code I have started off with but it doesnt seem to be working in the "on change" command for the cboProjectID combo box field:
Private Sub cboProjectID_Change()
Dim VarComboKey As Integer
Dim dbs As DAO.Database
Dim Err1 As DAO.Recordset
VarComboKey = Me.cboProjectID.Value
Set dbs = CurrentDb
Set Err1 = dbs.OpenRecordset("SELECT DISTINCT [Error_Reason_Code] FROM [HDR_ErrCodes] WHERE [project_ID] = " & VarComboKey)
Do While Not Err1.EOF
Me!cboErrCod1 = Me!cboErrCod1 & Err1!Error_reason_code & " "
Err1.MoveNext
Loop
Err1.Close
Set Err1 = Nothing
End Sub
I am no VBA expert and have been trying to follow the coding methodology i have read on this website for Recordset's. Access is displaying every type of Error_Reason_Code, not the specific Error_Reason_Code 's pertaining to the project ID selected in the cboProjectID combo box.
Can someone point me in the right direction in getting this code to display on the Error_Reason_Code 's in the cboErrCod1 combo box, only pertaining to the Project_ID selected in the cboProjectID combo box??
To change the list of items displayed in a combo box you need to modify its Row Source. There are 3 options for the Row Source Type: Table/Query, Value List and Field List.
Value list will take a semi colon separated list of values and display them for selection. For example if you entered a Row Source of "Cat;Dog;Mouse" you would get the following when selecting your drop down.
cat
dog
mouse
Field List will display the field names of whatever query or table you select as the row source.
Table Query is what you want. This will populate the combo box with the values from the table or query you provide. What you are doing with your code now and the line Me!cboErrCod1 = Me!cboErrCod1 & Err1!Error_reason_code & " " is changing the currently displayed value in the Combo Box but not affecting the values that will be available when you expand it. To do this you have to change the Row Source of your combo box with the query that you have already created.
Me!cboErrCod1.RowSource = "SELECT DISTINCT [Error_Reason_Code] FROM [HDR_ErrCodes] WHERE [project_ID] = " & VarComboKey
So your method would change to be what is below since you do not need to modify a record set.
Private Sub cboProjectID_Change()
Dim VarComboKey As Integer
VarComboKey = Me.cboProjectID.Value
Me!cboErrCod1.RowSource = "SELECT DISTINCT [Error_Reason_Code] FROM [HDR_ErrCodes] WHERE [project_ID] = " & VarComboKey
End Sub
EDIT: I have tried to duplicate your setup to see why the results would return nothing. Please correct this if I have made any errors in my assumptions.
I made a form with a combo called cboProjectID that has a source that gets all of the project IDs from the project table(Select * from Projects;) and displays the Project Code in the drop down. I made another one called cboErrCod1.
I made two tables one called Projects and the other called ErrorCodes with the following set ups based on your description.
Projects HDR_ErrCodes
ProjectID ProjectCode(text) Error_Reason_Code(text) project_ID(integer)
1 FI-01-05 Failure 1
2 FI-01-01 Testing 1
3 SY-02-02 Manual 1
4 SY-01-01 Failure 2
5 SY-01-02
With the above set up and the on change event my combo box cboErrCod1 gets populated with the correct error codes. Is there something wrong with that setup?

Microsoft Access, auto generate columns in DataSheet subform

I want to create a form in MS Access 2003 that lets the user pick from any existing query, and then have it display the results inside the form (as a sub-form in DataSheet view). The user will then be able to select one or more records and click a button on the parent form to do certain actions based on the selection. I want it to be able to work with any query, with very few limits, and display the full results of the query (all columns). The only requirement I might have is that it include certain fields for certain actions. For example, if I have a "send email" action, the query will require a field named "email", or maybe "to" and "subject".
Changing the DataSource of the DataSheet sub-form at run-time isn't a problem, I've done that before using VBA. Getting the columns displayed to change is the problem.
In a .NET WinForms app this could be done with the "auto generate columns" on a GridView control, or using the GridView.Columns collection directly in code. In VBA I don't see a way to add/remove columns from a DataSheet view. I also don't see a way to auto generate them based on the query. It appears the columns are controlled by the controls placed on the form (in form view), and while it is possible to add/remove controls using VBA, the form would have to be placed in Design View and require exclusive access to the database -- sounds very messy and I would like to avoid the exclusive access part.
Am I missing something? Is there an easy way to do this?
Here's how I would go about it. Create a blank subForm control on your main form. To change the source and the columns just leave the source object blank, then when you set it with code, the columns will reset to whatever source you use. So set it like so:
Private Sub setSource()
Me.subForm.SourceObject = "Query.myQuery"
End Sub
Then to get the selected items, assuming you know what column you want, you would do something like this:
Private Sub getSelected()
Dim rs As Recordset
Dim f As Form
Set f = Me.subForm.Form
Set rs = f.RecordsetClone
Debug.Print f.SelTop
rs.MoveLast
rs.MoveFirst
rs.Move f.SelTop - 1
Debug.Print rs!ID
End Sub
If you don't know the column explicitly you can use this to loop through the columns of the selected item and run some analysis on each name until you determine it's the column you want.
Dim i as Integer
For i = 0 To rs.Fields.Count - 1
Debug.Print rs.Fields(i).Name
Next