Unbound ComboBox populates another combobox - vba

How do I get a combobox to populate depending on what value you chose in another combobox? The first combobox values come from a table. The second combobox could either be a list or a date. Working on list part right now. If the value will give a list, the unbound combobox is visible and enabled. That works - I just can't get the second combobox to populate - should be values from another "code" table. Tried when exiting the first combobox OR when entering the second combobox. Neither works (used same code). Help!
Private Sub Combo746_Exit(Cancel As Integer)
Dim strsql As String
If (Me!Combo746.Value = "Category") Then
Me.Combo750.Enabled = True
Me.Combo750.visible = True
Me.Text748.Enabled = False
Me.Text748.visible = False
Me.Combo750.SetFocus
strsql = "Select [Category] FROM [dbo_CodesCategory]"
Me!Combo750.RowSource = strsql
Else
End If
End Sub

I got this to work. First combobox does not populate second combobox, but instead goes to the correct combobox with values. To explain - Department values are in the Department combobox - combodept. Category values are in a Category combobox - combocat. First combobox has values Category and Department. If the user picks Department, combodept for Department will be visible, combobox for Category- combocat - will be invisible. Here is the code:
If (Me!Combo746.Value = "Category") Then`
Me.ComboCat.Enabled = True
Me.ComboCat.visible = True
Me.ComboDept.Enabled = False
Me.ComboDept.visible = False
Me.ComboCat.SetFocus
Else
If (Me!Combo746.Value = "Department") Then
Me.ComboDept.Enabled = True
Me.ComboDept.visible = True
Me.ComboCat.Enabled = False
Me.ComboCat.visible = False
Me.ComboDept.SetFocus
End If
End If

Related

Filter record based on one subform to another

I have 3 forms:
frm_Main2
sfrm_ContractDetails (subform)
sfrm_ContractShop (subform)
The subforms are in a tab called NavigationSubform.
From frm_Main2 there is a combobox called cmb_Volume that when selected sfrm_ContractDetails is filtered by VolumeID. This subform has a value called ContractID which I want to use to filter sfrm_ContractShop.
This is what I wrote in the frm_Main2 cmb_Volume after update event:
Private Sub cmb_Volume_AfterUpdate()
[Forms]![frm_Main2]![sfrm_ContractDetails].Form.Filter = "[VolumeID] = " & Me.cmb_Volume
[Forms]![frm_Main2]![sfrm_ContractDetails].Form.FilterOn = True
[Forms]![frm_Main2]![sfrm_ContractShop].Form.Filter = "[ContractID] = " & [Forms]![frm_Main2]![sfrm_ContractDetails]!ContractID
[Forms]![frm_Main2]![sfrm_ContractShop].Form.FilterOn = True
End Sub
The first filter works well, but the second is resulting in a blank sfrm_ContractShop. What am I doing wrong pls?
You miss the Form property:
[Forms]![frm_Main2]![sfrm_ContractShop].Form.Filter = "[ContractID] = " & [Forms]![frm_Main2]![sfrm_ContractDetails].Form!ContractID

Access VBA Public Function - Variable for Control and Field?

I want to set up a public function like the following:
(A)
Public Function CheckYNField(chkControl As Control, chkField As String)
If chkField = "Y" Then
chkControl = -1
ElseIf chkField = "N" Then
chkControl = 0
Else: chkControl = 0
End If
End Function
(B)
Public Function CheckYNFlipper(chkControl As Control, chkField As String)
If chkField = "Y" Then
chkField = "N"
chkControl = 0
ElseIf chkField = "N" Then
chkField = "Y"
chkControl = -1
Else: chkField = "Y"
chkControl = -1
End If
End Function
The reason for this is, that I have a form, which has an underlying SQL table. I have no control over the data, but I must represent it, for the ability to maintain it. I have 10 fields, in the underlying table, which have a Y or N as their values. The data types are actually nvarchar(10). At the same time, I have to show these fields as checkbox controls, on the form, for ease of use.
The above code, is an attempt I am making to A - set the checkbox control to align with the current value in the table--> -1 = Y and 0 = N, and to B - update the table value, and switch the check box to checked or unchecked, from what it was, to the opposite, based on the onclick event of that control.
I want to make chkField and chkControl variables to the public function, that would be the table-field, and the form-control. I can't seem to get the right syntax, and was hoping someone might have clarification on how to do this.
for the form load and current, I tried this:
CheckYNField Forms("frmFormNameA").Controls(chkCheckNameA), tblTableName.FieldA
for the on click, I tried this:
CheckYNFlipper Forms("frmFormNameA").Controls(chkCheckNameA), tblTableName.FieldA
I've tried some other methods, but doesn't seem to be working. I'm doing something wrong, but I can't tell what. Appreciate any tips!
Edit/Update:
I tried Kostas K.'s solution, abandoning the idea of making a public functions with parameters for the fields and controls. I put the following, on load and on current:
With Me
If .txtfieldboundtablefieldA.Value = "Y" Then
.unboundchkA.Value = True
ElseIf .txtfieldboundtablefieldA.Value = "N" Then
.unboundchkA.Value = False
Else: .unboundchkA.Value = False
End If
End With
This is on a continuous form, so that it can show like a giant grid. There are the identifying bound field controls, and then a series of these checkboxes, to display the Y/N true/false status of each of these particular fields. I can't bound the checkboxes to the fields, because it changes the field value in the table to -1 or 0, and we need it to stay Y or N. I added a bound text field, to hold the table/field value for each row (hence the call to a text box control in the above revised code). The checkbox is unbound, and is there to display the field value, and allow the user to check and uncheck, so I can use on-click code to change the table field value between Y and N.
The above code is not seeming to show the correct checkbox value for each bound text field, based on each row. It shows based on the row that currently has focus. If I click on a row, where the table field is Y, all rows checkboxes on the form show true. If I move to a row, where the table field is N, all checkboxes for all rows change to false. I am struggling to just initially get 1 checkbox to show accurately, on every row of the continuous form, based on every record in the table. This is a small table, like 30 records. I really didn't think it would be so difficult to present this the way we need to.
Any ideas, how I could better do this?
Edit:
Set the Control Source of the checkbox to:
= IIf([YourYesNoField] = "Y", -1, 0)
In order to update when clicked:
Private Sub chkCheckNameA_Click()
Dim yesNo As String
yesNo = IIf(Me.chkCheckNameA.Value = 0, "Y", "N") 'reverse value
CurrentDb.Execute "Update [YourTable] SET [YourYesNoField]='" & yesNo & "' WHERE [ID]=" & Me![ID], dbFailOnError
Me.Requery
End Sub
You could try something this.
Check the Y/N field and assign the function's boolean return value to the checkbox (A).
On the checkbox click event, check its value and update the Y/N field (B).
'Form Load
Private Sub Form_Load()
With Me
.chkCheckNameA.Value = CheckYNField(![FieldA])
End With
End Sub
'Click
Private Sub chkCheckNameA_Click()
With Me
![FieldA] = IIf(.chkCheckNameA.Value = 0, "N", "Y")
End With
End Sub
'True returns -1, False returns 0
Private Function CheckYNField(ByVal chkField As String) As Boolean
CheckYNField = (chkField = "Y")
End Function
chkControl is a Control, so you need to access a property of that control. Try changing your code to:
Public Function CheckYNField(chkControl As Control, chkField As String)
If chkField = "Y" Then
chkControl.Value = -1
ElseIf chkField = "N" Then
chkControl.Value = 0
Else: chkControl.Value = 0
End If
End Function
and then the same idea in your other function.

DataGridView, Change cell of a typed Column to another type

I'm trying to mix a column on a datagridview with 2 control types (Checkbox & TextBox), the data is coming from a Stored Procedure that I'm also writing so I have a little flexibility.
in the stored procedure I'm returning a blank column to act as a Selection column in the GridView, but I am encountering problems when trying to convert cells to the other type based on criteria......
I keep the problem is with the datatypes when converting between the control types, I have tried all sorts of different ways to convert value first, controls first, etc but nothing is working 100%.....
currently, I have the SP returning the string False in a column then in using this with the criteria to create a checkbox..... it works fine but the Value remains a string even after converting it to a Boolean, the datatype is also a Boolean on the checkbox but the value is String..... this has gone over my head now and I'm at a loss......
NewCntrl2 = New DataGridViewCheckBoxCell
NewCntrl2.Value = Convert.ToBoolean(DGV.Cells(0).Value)
NewCntrl2.ValueType = GetType(System.Boolean)
DGV.Cells(0) = NewCntrl2
this is the code converting the textbox-column cell to a checkbox cell
any ideas why the value of the checkbox is still a string ('False')...
the problem with what it's doing now is when I handle the cell click event I cannot check or uncheck the box using the Not Value technique
----EDIT----
This is the sub I'm using, it creates several different types of controls.
Public Sub posted_CreateControls()
Dim Cell_0 As String
Dim Cell_1 As String
Dim Cell_2 As String
Dim NewCntrl As DataGridViewButtonCell
Dim NewCntrl2 As DataGridViewCheckBoxCell
For Each Rw As DataGridViewRow In dgvPosted.Rows
With Rw
Cell_0 = .Cells(1).Value.ToString
Cell_1 = .Cells(2).Value.ToString
Cell_2 = .Cells(3).Value.ToString
'this column starts with a value 'False' returned by the SP,
'we don't want checkboxes on all rows and using false string was the only method
'i could find to easily do this
'if both assign and unassign id are present then we need a checkbox
Select Case Cell_0
Case String.Empty
.Cells(0).Value = String.Empty
Case Else
If Not Cell_1 = String.Empty Then
NewCntrl2 = New DataGridViewCheckBoxCell
NewCntrl2.Value = Convert.ToBoolean(.Cells(0).Value)
NewCntrl2.ValueType = GetType(System.Boolean)
.Cells(0) = NewCntrl2
Else
.Cells(0).Value = String.Empty
End If
End Select
'Create an Assign button for each row in columnindex 0(Assign) if ColumnIndex(2)(Edit) contains M, L or I
Select Case Cell_2
Case "M", "L", "I", "P"
NewCntrl = New DataGridViewButtonCell
.Cells(1) = NewCntrl
NewCntrl.Tag = Cell_0
NewCntrl.Value = "Assign"
End Select
'Create an UnAssign button in columnindex 1(UnAssign) if the value of columnindex 1(Unassign) is not empty
If Not Cell_1 = vbNullString Then
NewCntrl = New DataGridViewButtonCell
.Cells(2) = NewCntrl
NewCntrl.Tag = Cell_1
NewCntrl.Value = "UnAssign"
End If
'Create an Edit button on columnindex 2(Edit) if ColumnIndex 2(Edit) is not M, L or empty string
Select Case Cell_2
Case "M", "L", "P", vbNullString
'Do Nothing
Case Else
NewCntrl = New DataGridViewButtonCell
.Cells(3) = NewCntrl
NewCntrl.Tag = Cell_2
NewCntrl.Value = "Edit"
End Select
End With
Next
NewCntrl = Nothing
End Sub
This creates the checkboxes how I want them but the values remain as a string that is causing the cell. Click event to fail because I'm trying to set the checkbox value to true and the .value = not .value part is failing because for some reason the value remains a string but the checkboxes value type is in fact Boolean.....
Instead of returning false try returning:
`select CAST(0 as bit) as somefield`
Which will return a boolean field. I'd also recommend adding a check box column instead of adding the individual cells.

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next

how to set focus to the new row in datagridview - vb.net

I don't know how to set focus point always to the new row in DataGridView in the beginning??
To focus on the newly added row :-
dataGridView1.Rows(dataGridView1.Rows.Count - 1).Selected = true;
or you can use this to focus on userdefine row
dataGridView1.Rows(Rowindex).Selected = true;
Ensure just the last full row is selected by using the following in your init code:
dataGriView1.MultiSelect = False
dataGriView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Keep Coding
dim NoRow As Integer = 2
me.gridTickets.CurrentCell = me.gridTickets.Rows(NoRow).Cells(0)
dgvSimpleReports.Rows(dgvSimpleReports.Rows.Count - 1).Selected = True
dgvSimpleReports.CurrentCell = dgvSimpleReports.Rows(dgvSimpleReports.Rows.Count - 1).Cells(0)
Selected is not enough, because selected row only selects the row but DataGridView is not focused automatically. You need to set current row , but current row is ReadOnly, so you need to use current cell, because current cell is not ReadOnly, the code stated below should fix this problem.
Have a look at the CurrentCell property.
If (DgViewCityMaster.Rows.Count > 0) Then
DgViewCityMaster.Rows(0).Selected = True
End If
' Here DGViewCityMaster is my Data Grid View
You want to handle the RowsAdded event of your DataGridView and just select the newly added row.
Private Sub MyDataGridView_RowsAdded(ByVal sender As Object, ByVal e As DataGridViewRowsAddedEventArgs) Handles MyDataGridView.RowsAdded
MyDataGridView.Rows(e.RowIndex).Selected = true;
End Sub