Select combobox intem after bounded with SQL Query in Access - vba

I have the following pair of combobox that are used for two inserts in two different access tables from a single form.
The problem I have is that I am not able to make anything else load the form is selected both in Name_OT and in Year the first value that contains corresponding combobox.
I think the solution is with:
Combobox1.Selected (0) = True 'First value
But the combobox goes blank, no text or anything appears.

Solved with this
Cuadro_combinado79 = Cuadro_combinado79.ItemData(0)
Cuadro_combinado85 = Cuadro_combinado85.ItemData(0)

Related

access a specific field within a query that is part of a subform

I am trying to get an understanding of what the syntax would be to access a specific field within a query that makes up a subform within another form.
Form > SubForm/Query > Field
The field I am trying to get access to a field called Berthed that operates as a Boolean field. Either the vessel is present or it is absent.
This is what I was trying and it does not work.
Me.Boat_Move_Prior_Week.SourceObject.berthed
In this case here is the breakdown
Me = current form
Boat_Move_Prior_Week = query
Berthed = field I am trying to access through the VBA
My final goal will be to create an If statement, that upon requery of the Form, will look at the Berthed field, and if the field is checked (vessel is present) do nothing, but if the field is unchecked (vessel is not present) then remove the vessel from the query.
To reach the field (on the subform) bound to the field berthed, this is the syntax (assuming the textbox on the subform also is named berthed):
IsBerthed = Me!NameOfSubformControl.Form!berthed.Value
Here an amazing guide to know how to deal with controls in main form and subforms:
Link
Aside from my syntax issues I should have been using False for the value. Thanks to the two posters above for getting me in the right direction.
If Me.Boat_Move_Prior_Week.Form!BERTHED.value = False Then
Me.Boat_Move_Prior_Week.SourceObject = ""
End If

How do I fix MS access option group problem?

I created many option groups in MS Access 2013 and I am trying to populate my table according to what is selected in the option group. So, if the user selects option 1, I want "the text" not its value ex: "1" stored in my table. I tried the following code in AfterUpdate() event and it works fine:
Private Sub Frame49_AfterUpdate()
Dim D As Integer
Select Case Me![Frame49]
Case 1
Me![Name] = "text"
D = 1
Case 2
Me![Name] = "text1"
D = 2
Case 3
Me![Name] = "text2"
D = 3
Case 4
Me![Name] = "text3"
D = 4
Case 5
Me![Name] = "text4"
D = 5
End Select
DoCmd.RunCommand acCmdSaveRecord
Rem D = Frame49.Value
End Sub
but when the end user answers the first question and tries to answer the next question, all options of the previous question get selected. How do I fix this?
Here is the file to see what I mean:
https://drive.google.com/open?id=1WjrAhXCnk961mxBuxS3RYqOUpPA_GsyL
Thanks in advance.
even though the option group only takes numeric values, you can achieve what you want by hard coding the values using if statements e.g
If Frame5 = 1 Then orukolook = "okay"
If Frame5 = 2 Then orukolook = "right"
If Frame5 = 3 Then orukolook = "fine"
orukolook is the textbox control that you want the texts to be inserted, so if the first option of the option group is selected,the text "okay" will be inserted into the textbox control, if second option then the text "right" will be inserted.
The values hard coded in the place holder oruko look,e.g okay,right, fine are the labels associated to each value in the option group.
OptionGroup frame and associated buttons/checkboxes must have a number value. Therefore OptionGroup frame must be bound to a number type field. If you want controls to reflect selection in a text field, then need code to set UNBOUND OptionGroup frame with corresponding number value. In other words, convert saved text back to number value. Code would most likely need to be in form Current event. Something like:
Me.Frame49 = Switch([Name]="text",1, [Name]="text1",2, [Name]="text2",3, [Name]="text3",4, [Name]="text4",5)
Alternatively, save number value to number fields. Text equivalent is provided by labels on form. Use lookup tables to provide text equivalent on reports or calculate the equivalents with expressions in query or textboxes. An expression like:
Choose([Name], "text", "text1", "text2", "text3", "text4")
BTY, Choose() expression can be used in place of Case structure in your original code.
Me![Name] = Choose(Me.Frame49, "text", "text1", "text2", "text3", "text4")
Also recommend using radio (option) buttons instead of checkboxes.
Other alternatives are comboboxes and listboxes instead of option groups.
Advise not to use reserved words as names for anything. Name is a reserved word. Also, avoid spaces and punctuation/special characters in naming convention.
Frame49 is bound to a database field.
When the user clicks a checkbox, the field's value (along with Frame49's value) is set to an integer.
You then change the database field's value to a string.
This causes Frame49's value to be set to that string.
Since that is an invalid value for an Option Group, all the related checkboxes show as a solid black square, representing an indeterminate state. That is not the same as a checkmark, so your observation "all options of the previous question get selected" is incorrect.
The simplest way to do what you want is to use a 1-column ListBox instead of an Option Group. You can size each ListBox so that it shows all the options as text strings.
When the user clicks an "option" to select it, the corresponding text string will be written to the database, with no VBA code involved.
When the user goes back to a previous record, the ListBoxes will all show the proper selections.
If you don't want to change how your form looks, then you must do as others suggested, and make Frame49 unbound, i.e. set its Control Source to blank.
Then when you set the database field's value to a text string, Frame49's value will remain as an integer.
If you want the ability to go back and edit earlier records, you can do it but it is beyond what I can answer here.

pass value within one form in ms-access

hey i am a total noob and only found ways to pass a value between two forms.
this is the form i am working with: form.
in this form (event.form) there is a subform (Fundort_kurz-Subform) which lists data. when i click one row inside the subform the value for "objid" appears in textbox "text501" by "=[Fundort_kurz-Subform].[Form]![Objid]". I automatically want the value from field "text501" to be passed to field "Objid_3" which is meant to write the value into the table.
Not sure what you are doing, but use the OnCurrent event of the subform:
Me.Parent!Objid_3.Value = Me!text501.Value
And do rename your controls to something meaningful.

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.