fill textbox based on combobox selection vb.net *data not bound [duplicate] - vb.net

I got a bounded combobox to a group name in vb.net. i can view in the drop down items of the combobox the set of GROUP NAMES. but when i do an insert or update query, i need to make the selected group name refers to the GROUP NUMBER. I don't want to store letters in the database, instead i prefer numbers. how can i do that?!
Here is my code so far :
cmd1.Parameters.AddWithValue("#group", DirectCast(Additemcombobox.SelectedItem,
DataRowView).Item("GroupName"))
Storing the group name in database is currently working well.
My question might not be well explained. Please ask me in case...
any help would be appreciated

You can show one element to the user such as the name, but use a different one for the code to identify the item using DisplayMember and ValueMember
Dim SQL = "SELECT Id, Name FROM GroupCode ORDER BY Name"
...
GrpDT = New DataTable
GrpDT.Load(cmd.ExecuteReader)
cboGroup.DataSource = GrpDT
cboGroup.DisplayMember = "Name"
cboGroup.ValueMember = "Id"
The user will only see the names, while the code can use ValueMember:
Private Sub cboGroup_SelectedValueChanged(...etc
Console.WriteLine(cboGroup.SelectedValue)
End Sub
It prints the Group ID not the name. Depending on the ORDER BY clause in the SQL and the ID, the SelectedIndex may or may not match, so respond to SelectedValueChanged event. If you use SelectedValue instead of SelectedItem you wont have to thrash about with a DataRowView item.
Note that SelectedValue is Object so you will have to cast to integer or whatever for use elsewhere.

Related

Access report : Set unbound textbox value depending on other textbox value

I have report in Access 2016 with two columns (Name and Group) from a query.
I want to fill an unbound textbox in my report depending on the value in Group.
The value I'm getting is the last group value. I want it to look for each row value.
Please help me.
I've tried get the value from Group to display in textbox.
if me.group.value = "1" then me.textbox = "blablabla" end if
it only works if I use the last group.value ("4"), but every row will get "blablabla"
I have tried with _load and _open , _activate. Don't know the difference though.
Please help
sincerely
Dan
Don't use VBA. Put expression in textbox ControlSource or do calc in query used as report RecordSource and bind textbox to that field.
IIf([Group] = 1, "blablabla", Null)

Capture the text of a LABEL to be used in QUERY

I have reorganized everything then.
I want to capture the text that is contained in a label I have in a form in order
to be used as the input for the WHERE criteria.
The query is the next called qry_A:
SELECT
tbl_R.ID_R,
tbl_C.Cuenta,
tbl_C.Nombre,
IIf(tbl_RD.Deb Is Null,0,tbl_RD.Deb) AS Deb,
IIf(tbl_RD.Cre Is Null,0,tbl_RD.Cre) AS Cre,
tbl_R.NIT,
tbl_R.Fecha,
tbl_R.Com,
tbl_F.ID_F,
tbl_R.ID_U
FROM
(tbl_F INNER JOIN tbl_R ON tbl_F.ID_F = tbl_R.Fideicomiso)
INNER JOIN (tbl_C RIGHT JOIN tbl_RD ON tbl_C.Cuenta = tbl_RD.Cuenta) ON tbl_R.ID_R = tbl_RD.ID_R
WHERE ((tbl_R.ID_U)=[Forms]![frm_qry_A]![lbl_Usuario]);
I've checked the name of the label and the name of the form. Not sure if this is possible to do with a label, I've checked and tried with a Text Field in the form and it works, I mean, if I capture the text of a Text Field of another form that I have it goes, this is the line added to the query in order to illustrate what I did:
WHERE ((tbl_R.ID_U)=[Forms]![frm_B]![txt_Usuario]);
With that I capture the criteria or text declared in the Form called form_B as this:
Private Sub Form_Load()
Me.lbl_Usuario.Caption = LCase(UsuarioLogeado)
Me.txt_Usuario = UsuarioLogeado
End Sub
You forget to type Parameters collection name:
'Associate parameter
tomarConsulta.Parameters!capturarUsuario = txt_Usuario
or better:
'Associate parameter
tomarConsulta.Parameters("capturarUsuario").Value = txt_Usuario
The label's Caption property contains the text which the label displays. So reference Caption explicitly when you reference the label in the SQL statement.
WHERE tbl_R.ID_U=[Forms]![frm_B]![lbl_Usuario].Caption

Vba code to show selected data from combobox

I have an Access 2010 form which has a combobox listing three columns:
ID
first name
last name
The combo box is bound to a table containing this data. The ID column in the combo box is hidden, it only shows the first and last name.
When the user selects a row only the the first name is shown. in the property section, I chose:
Column Count: 3
Column widths:0;3,3
Bound Column: 1
I made another text field and in the combobox I wrote the following vbcode:
text=combo.value
that shows in the text field the chosen ID.
I want to show in another field (text\combo?) the last name.
How can I do that?
well I did it with recordset.
if someone know a simpler solution, I will be glad to lrn.
this is my code:
Dim dbs As ADODB.Connection
Dim id As String
Set dbs = CurrentProject.AccessConnection
Set rsRep = New ADODB.Recordset
Set rsRep.ActiveConnection = dbs
rsRep.Open "tblRep"
id = Me.cbPrvFirstName
rsRep.MoveFirst
rsRep.Find "RepId=" & id
Me.txtPrvLastName = rsRep.Fields(2)
rsRep.Close
You can pull in the value from another column within the combo box using the below code. By default combo.value will always give the value from the first column even if hidden.
try
text=combo.column(x)
in your case to retrieve last name it would be
text=combo.column(2)
where x is a NUMBER of the column you want to retrieve 0 being the first column
You could also try an alternative - This works like a VLookup in excel if you are familiar with that.
(Not as simple as the first option ;) )
text=Dlookup("[last name]","tblRep","RepId=" & combo.value)

Linking result of a query that related to a Listbox value to textboxes?

I have a problem in MS Access and I don't know how can I solve this problem.
I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:
The user select 'A' value From the list box then this query should be run:
SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID
then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).
How can I do that? How can I Link the list box and the query to the textboxes?
thank you so much
Every ListBox has a ListBox_Click() procedure, which is run when an element of the listbox is clicked.
You can get, what you want, if you do something like this:
Sub ListBox1_Click()
Dim valueOfListbox As String
valueOfListBox = ListBox1.Value
' **** here comes your code ****
' get the actual value from the listbox and do the query
' change the values of the textboxes to the result of the query
End Sub
If your listbox is named different than "ListBox1" you have to change the name of the procedure. For example if the listbox is named "blaBox" it has to be blaBox_Click().
Depending on the structure of your data, I would do something like this. Set the table/query of your listbox to have all the data you want to display.
SELECT ID, Name, XValue, YValue, Wert FROM Mytable
In the Format tab of the property sheet of the listbox, set the column count to 2, and the column widths to 0";2" or your preference. This will hide the other values in the listbox rows.
In the three textboxes, set the control source like so.
Textbox1.ControlSource: =[Listbox1].[Columns](3)
Textbox2.ControlSource: =[Listbox1].[Columns](4)
Textbox3.ControlSource: =[Listbox1].[Columns](5)
You may need to adjust the numbers. A listbox has a property called Columns that enable you to access the values at different columns of the listbox. If you don't want to or cannot have all the data in the listbox, then you can use the DLookUp function in each textbox.
Textbox1.ControlSource: =DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")
The reference to [Listbox1] will pull the value of the bound column. If the bound column is not the data you are looking up by then you will need to reference a column (i.e. [Listbox1].[Columns](2)).

Display ValueMember when selection is changed

I have searched all day for what I would think would be a simple concept, but that is what I get for thinking.
I have a DataGridView control with a ComboBox column that uses a DataTable as its DataSource as follows:
With CType(dgvICS213.Columns(GridCol("CostBasis").Name), DataGridViewComboBoxColumn)
.DataSource = Common.ExecuteQuery("SELECT ID, CostBasis FROM ICS213CostBasis", CommandType.Text)
.DisplayMember = "CostBasis"
.ValueMember = "ID"
End With
The database table stores the value (ID) from the combo box.
I use the DefaultValuesNeeded event to set new values to the DisplayMember (CostBasis) value:
Dim dgv As DataGridView = sender
CType(e.Row.Cells(GridCol("CostBasis").Index), DataGridViewComboBoxCell).Value = CType(dgv.Columns(GridCol("CostBasis").Index), DataGridViewComboBoxColumn).Items(0).Item(1)
I also use the DisplayMember value to fill existing rows from a database query (handling the DataError event)
So far, so good but when I select a an entry from the combox, the ValueMember is displayed in the combo instead of the DisplayMember. Can someone explain where I am going wrong.
I faced the same problem and solved it by changing the field in the database from byte to int16.