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
Related
This should be simple, but it's escaping me right now as I have not written in VB in over a decade.
I have a stored procedure that gets a list of students and their assigned homeroom teacher.
Very basic - just the three columns: FirstName, LastName, Teacher.
What I need is to load the data into a DataSet where each Teacher has their own DataTable filled with the students assigned to them.
Sample data:
So in the example above, I would end up with 3 DataTables named Adams, Taggert and Dublin, each populated with the students associated with them all in a single DataSet in such a way that I could call the data with:
For Each dr As DataRow In ds.Tables("Taggert").Rows
' Do something with dr("FirstName").ToString
Next
Any help would be appreciated.
I ultimately used elements from both answers to find a solution. I ended up using DataViews to create the tables, and added them into the new DataSet. It works perfectly. It iterates through every record, but for the few hundred students it handles, it's actually quite
efficient. Thanks for the comments.
For Each row As DataRow In ds.Tables(0).Rows
Dim Teacher As String = row("Teacher").ToString()
If Not dsOutput.Tables.Contains(Teacher) Then
Dim dvStudents = ds.Tables(0).DefaultView
dvStudents.RowFilter = "Teacher = '" & Teacher & "'"
Dim subTable As New DataTable(Teacher)
subTable = dvStudents.ToTable(Teacher)
dsOutput.Tables.Add(subTable)
End If
Next
old_datatable with all teachers , i will declare this but you use your own
Dim old_datatable As New DataTable
Dim new_dataset As New DataSet
For Each row As DataRow In old_datatable.Rows
Dim teacher As String = row("Teacher")
If new_dataset.Tables.Contains(teacher) = False Then
Dim new_datatable As New DataTable
new_dataset.Tables.Add(new_datatable)
End If
new_dataset.Tables(teacher).Rows.Add(row)
Next
For Each dr As DataRow In new_dataset.Tables("Taggert").Rows
' Do something with dr("FirstName").ToString
Next
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 am working on a project and have hit a brick wall again. I have a listbox with some math operations in it (i.e. "1+2=3"). I am trying to save all items in the listbox to an access database, and I think I am getting close. The database has two entries I put in manually, but when I use the following code I only get -1's (plus the two manual entries) in the listbox (when I clear the listbox and load the database into the listbox). The other issue is I need to limit the database to 10 rows which I have no idea how to do. What I would like to do is check the database each time I save to it to see how many rows it has. If the number of listbox items I am saving will exceed 10 rows in the database then clear the database and start from 0 rows. Here is the code I have (which only attempts to save to database):
con.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = Mathops.accdb; Persist Security Info=False;"
Dim cmd As New OleDbCommand
Dim var1 As String
con.Open()
cmd.Connection = con
Try
For i As Integer = 0 To lstDisplay.Items.Count - 1
var1 = lstDisplay.Items.Item(i).ToString
cmd.CommandText = ("INSERT into Records (Operations) VALUES(" + var1 + ")")
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
Thank you for any assistance you can provide!
John
Oh...and the database has 2 fields; ID and Operations where ID is the Primary Key.
Try using listdisplay.items(i) to get the text from your listbox.
Then to sort out the 10 items issue:
If you do an SQL query:
SELECT Max(id) FROM Records that will give you the biggest id value in the table
Then check if the value you get back >= 10, and if it is use the SQL:
DELETE FROM Records to clear the table.
my question is, is there a way to filter records in the dataset and use that records to fill in the datagridview? for example, a datatable (with 3 columns: ID, StudentName, Gender) is filled with list of students. i have two datagrids in the form namely DatagridView1 and Datagridview2. DatagridView1 is where the the list of student where Gender is equal to M and DatagridView2 is where the the list of student where Gender is equal to F.
in my current solution, i am using a loop.
For each iStud as datarow in iDataset.Tables(0).Rows
IF iStud.Item("Gender").ToString = "M" Then
'add this record to DatagridView1
Else
'add this record to DatagridView2
End If
Next
is there a way without using a loop?
Yes, there is. All you need to do is to filter the dataset using SELECT.
For example,
DatagridView1.Datasource = xSet.Tables("StudentList").SELECT("Gender = 'M'")
DatagridView2.Datasource = xSet.Tables("StudentList").SELECT("Gender = 'F'")
Brief Explanation:
xSet is the name of the Dataset
StudentList is the name of the Datatable
Gender is the name of the Column where you want to filter
UPDATE
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