I'm not sure how to go about this. Currently, in the first gridview, is a list of groups a member has access to. The second gridview is bound to a table that contains a list of every type of group and has an Add button which adds it to the first gridview and updates a table adding that group to a member.
This is what I am trying to do:
When the Add button (in the second gridview) for a row is clicked, it will be added to the first one (like it currently does), but I would also like it to disappear from the second gridview. So basically, a group should only be visible in either the first gridview or the second, not both. The problem I have is I don't want to modify the table because obviously the table contains all possible groups. Any suggestions?
I would do all the work on the database side. Data source for first one is:
Select g.*
From membership m
inner join groups g on m.groupid=g.groupid
Where m.userid = #userid
Data source for the second one looks like
Select g.*
From groups g
Where not exists
(Select 1 from membership m
Where m.GroupID=g.GroupID and m.userid = #userid
Then your add button inserts the appropriate row in the table and requeries both grids.
You could simply make changes to the Dataset, then .AcceptChanges and re-bind. This would ensure the data would update on the screen without actually pushing it to the database.
Pseudo example:
Dim dt as Datatable = GetData1(strSQL.toString)
Dim dt2 as Datatable = GetData2(strSQL.toString)
Public Sub MoveRecord(ByVal dt1 as DataTable, ByVal dt2 as Datatable, ByVal index as Integer)
'Record being moved '
Dim dr as Datarow = dt.Rows(index)
dt2.Rows.Add(dr)
dt2.AcceptChanges
dt.Rows.RemoveAt(index)
dt.AcceptChanges
'Bind Gridview
Perhaps store new changes in Viewstate,
Cache, or Session for Paging, Sorting'
End Sub
Of course, this assumes the grids have the same fields. If they don't, you'd have to:
Dim drN as DataRow - dt2.Rows.NewRow
'Assign rows from moving datarow to new datarow'
dt2.Rows.Add(drN)
dt2.AcceptChanges
Related
I'm having an issue with creating a LINQ statement for a DGV control.
What it's supposed to do:
When I click on a country listed from a combobox I've placed on my Form, it is supposed to act as a filter.
Example: When Brazil is selected, the DGV will then display the CustomerID, Region, etc connected to Brazil.
The issue is, I'm definitely aware that this isn't correct. When I do click on a country from the combobox, the DGV will only display blank spaces, or "length."
Code:
Dim Q = From Query in DSname.Customers
Where Query.Country = ComboBox.Text
Select Query.CustomerID
DataGridView.DataSource = Q.ToList()
Any help will be appreciated, thank you!
This should pull each record that matches the criteria. Don't add the Select part at the end.
Dim query = (From cust in DSname.Customers
Where cust.Country = ComboBox.Text).ToList
DataGridView.DataSource = query
I'm a veteran Excel VBA coder who has been thrown into the deep end on this form <> Access database project. I'm working on one part of the form where users (employees) will select the site location (84 total, but not all will show based on what team they're on) and then select the department within that site. There are 2,189 total departments, so the department list should only show those for that site.
The database that we query from and update to is on a VERY slow server, so I want to pull a whole lookup table at the beginning (restricted down to those that team works on) into a datatable, then populate the first combo box with unique sites within that datatable, then populate the second combo box with all of the departments at that site.
The datatable is more or less just | ID | Site | Department |. I can fill that datatable just fine and leave it sitting in cache. And I could loop through the datatable to populate the combo boxes, but that's slower than it can be.
So is there a way to perhaps query from that datatable to either directly populate the combo boxes, or to fill a secondary datatable then to the combo boxes? For the first box, it's finding UNIQUE sites that is the challenge. Then for the second, it's departments where the site equals the one selected in the first box.
Thanks!
You can use Linq to Sql as mentioned by "Andrew Morton", i have made an exemple for you :
Dim da As SqlDataAdapter = New SqlDataAdapter("select * from YourDatatable, con)
Dim dt As New Data.DataTable()
da.Fill(dt)
'You can change here your condition as you need
Dim query = (From d1In dt.AsEnumerable() _
Where d1.Field(Of Integer)("ID") = 1 And d1.Field(Of Integer)
Select d1!ID, order!Site , d1!Department ,
If query.Count > 0 Then
comboBox1 .Items .Add (query(0).Site) 'Fill your Combobox1
end if
Dim query2 = (From d2 In dt.AsEnumerable() _
Where d2.Field(Of Integer)("ID") = 2 And d2.Field(Of Integer)
Select d2!ID, order!Site , d2!Department ,
If query2.Count > 0 Then
comboBox2 .Items .Add (query2(0).Department ) 'Fill your Combobox2
end if
I have an access form that contains two subforms. The main form has three dropdown fields that allow the user to pick what data is displayed in the two linked subforms. The dropdown fields are all unbound as they are referenced in the datasource of the main form.
The SQL of the datasource for the main form is as follows:
SELECT tbl_RptPeriod.RptPrdID, tbl_BusUnits.UnitID, tbl_Categories.CategoryID, tbl_Categories.CategoryTitle, tbl_Categories.CategoryGroup, tbl_Categories.Inactive, tbl_Categories.RptPrdID, tbl_Categories.UnitID
FROM tbl_RptPeriod INNER JOIN (tbl_BusUnits INNER JOIN tbl_Categories ON tbl_BusUnits.UnitID = tbl_Categories.UnitID) ON tbl_RptPeriod.RptPrdID = tbl_Categories.RptPrdID
WHERE (((tbl_RptPeriod.RptPrdID)=[Forms]![Compliance]![cmbReportPeriod]) AND ((tbl_BusUnits.UnitID)=[Forms]![Compliance]![cmbBusinessUnit]) AND ((tbl_Categories.CategoryID)=[Forms]![Compliance]![CategoryTitle]));
This works perfectly to look up records. However, I want the users to be able to add new records to the ones they are looking up. That doesn't work as it should.
I can add records, but then the form elements that I am using to look up the data, specifically RptPrdID,UnitID, and CategoryID are not being populated in the table, thus the new records are unassociated and don't show up if you go to look for them again in the main form after closing it or moving to another record.
This makes sense in so far as the form elements I am using in the "WHERE" criteria of the SQL are unbound, but of course, if I add a new record, I want it to be correctly matched. The user must be able to find the records he adds if he looks up the same criteria again.
Question:
How can I get new records entered in the subforms to have RptPrdID,UnitID, and CategoryID filled in?
UPDATE
SELECT tbl_Activity.CategoryID AS CategoryID_X, tbl_Activity.RptPrdID AS RptPrdID_X, tbl_Activity.UnitID AS UnitID_X, tbl_Categories.SortKey, tbl_Categories.CategoryTitle, tbl_Categories.CategoryGroup, tbl_Categories.Inactive, tbl_Categories.Inactive, tbl_BusUnits.Unit_Name, tbl_RptPeriod.Rep_Month_Nr, tbl_RptPeriod.Rep_Month_Name, tbl_RptPeriod.Rep_Year
FROM tbl_RptPeriod INNER JOIN (tbl_Categories INNER JOIN (tbl_BusUnits INNER JOIN tbl_Activity ON tbl_BusUnits.UnitID = tbl_Activity.UnitID) ON tbl_Categories.CategoryID = tbl_Activity.CategoryID) ON tbl_RptPeriod.RptPrdID = tbl_Activity.RptPrdID
WHERE (((tbl_Activity.CategoryID)=[Forms]![Compliance_EXPERIMENT]![cmbCategoryTitle]) AND ((tbl_Activity.RptPrdID)=[Forms]![Compliance_EXPERIMENT]![cmbReportPeriod]) AND ((tbl_Activity.UnitID)=[Forms]![Compliance_EXPERIMENT]![cmbBusinessUnit]))
ORDER BY tbl_Categories.SortKey;
I had to change the query somewhat, but the key fields are still the same ones, though they are now on a different table.
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.RptPrdID_X = Me.cmbReportPeriod
Me.UnitID_X = Me.cmbBusinessUnit
Me.CategoryID_X = Me.cmbCategoryTitle
End Sub
Add code for filling those 3 fields in Before Insert event of main form
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.RptPrdID = Me.cmbReportPeriod
Me.UnitID = Me.cmbBusinessUnit
Me.CategoryID = Me.CategoryTitle
End Sub
I'm using the below mentioned code to select a record ID from an Access Database that wasn't already selected in the last day and add it to an array.
The general goal is that a record that matches the initial "Difficulty" criteria will be retrieved so long as either the record was never selected before OR the record wasn't chosen in the last 2 days. After the loop is done, I should have x amount of unique record ID's and add those onto an array for processing elsewhere.
Private Function RetrieveQuestionID(questionCount As Integer)
' We're using this retrieve the question id's from the database that fit our arrangements.
Dim intQuestArray(0 To questionCount) As Integer
Dim QuestionConnection As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = |DataDirectory|\Database\MillionaireDB.accdb;")
QuestionConnection.Open()
For i As Integer = 1 To intNoOfQuestions
'TODO: If there are no valid questions, pull up any of them that meets the difficulty requirement....
Dim QuestionConnectionQuery As New OleDb.OleDbCommand("SELECT Questions.QuestionID FROM Questions WHERE (((Questions.QuestionDifficulty)=[?])) AND (((Questions.LastDateRevealed) Is Null)) OR (Questions.LastDateRevealed >= DateAdd('d',-2,Date())) ORDER BY Rnd((Questions.QuestionID) * Time());", QuestionConnection)
QuestionConnectionQuery.Parameters.AddWithValue("?", intQuestionDifficulty(i - 1).ToString)
Dim QuestionDataAdapter As New OleDb.OleDbDataAdapter(QuestionConnectionQuery)
Dim QuestionDataSet As New DataSet
QuestionDataAdapter.Fill(QuestionDataSet, "Questions")
intQuestArray(i - 1) = QuestionDataSet.Tables("Questions").Rows(0).Item(0)
Dim QuestionConnectionUpdateQuery As New OleDb.OleDbCommand("UPDATE Questions SET Questions.LastDateRevealed = NOW() WHERE Questions.QuestionID = [?]", QuestionConnection)
QuestionConnectionUpdateQuery.Parameters.AddWithValue("?", intQuestArray(i - 1).ToString)
QuestionConnectionUpdateQuery.ExecuteNonQuery()
Next
QuestionConnection.Close()
Return intQuestArray
End Function
However, looping through the array will show that there are records are somehow being repeated even though the record updates during the loop.
Is there another way to loop through the database and pull up these records? I even attempted to move the .Open() and .Close() statements to within the For...Next loop and I'm given worse results than before.
As Steve wrote, the >= should be a < .
In addition, your WHERE clause is missing parentheses around the OR part.
It should be (without all unnecessary parentheses):
SELECT Questions.QuestionID
FROM Questions
WHERE Questions.QuestionDifficulty=[?]
AND ( Questions.LastDateRevealed Is Null
OR Questions.LastDateRevealed < DateAdd('d',-2,Date()) )
ORDER BY Rnd(Questions.QuestionID * Time());
Also have a look at How to get random record from MS Access database - it is suggested to use a negative value as parameter for Rnd().
I am working on VS 2010 - Vb.Net, Having a syntax clarification on Filter & Merge DataSet
My dataset - ds has 23 tables. In which, i need to filter one Table with certain criteria.
tempDs.Merge(ds.Tables("p_tree_categories").Select( "GROUP_CODE <> 'PA.43.948'"))
On writing Merge Syntax,
I am able to see only selected table : p_tree_categories in the tempDs. We need the remaining 22 tables along with the filtered record of p_tree_categories.
How can i achieve this ?
It sounds like you only want the filtered rows of table p_tree_categories. This being the case, I would:
Generate a copy of the p_tree_categories which only contains the rows you are interested in.
Remove the existing p_tree_categories from tempDs.
Merge the copy into tempDs as p_tree_categories.
These steps could be implemented something like this:
Dim originalTable As DataTable = tempDs.Tables("p_tree_categories")
Dim filterView As DataView = New DataView(originalTable)
filterView.RowFilter = "GROUP_CODE <> 'PA.43.948'"
Dim filteredTable As DataTable = filterView.ToTable
filteredTable.TableName = "p_tree_categories"
' Remove old, add the new.
tempDs.Tables.Remove(originalTable)
tempDs.Tables.Add(filteredTable)