Using Dataset and table adapters for Service Based Database in Visual Studio 2017 and VB.net - vb.net

I am trying to create a Windows form with a self contained DB. I added a Service Based DataBase. I then created a Dataset. My table is simple:
Example:
columns are: Model (PK), Part1, Part2, Part3............
I want a combobox on my for to be able to select the model, and another combobox to select the desired part then return the number. You pick Model 740, and Part1 and the return is part1 part number.
The issue is that I need the select Statement in the tableadapter query builder to show something like this.
SELECT *
FROM Table
WHERE Model = ? (Paramter from ComboBox1) AND parameter (Part1 From Combobox2 Will be a Column Name)
I can't figure out how or if it is even possible to have a parameter in after the select.
Table is built like this:
[enter image description here][1]
[1]: https://i.stack.imgur.com/bf4kv.png

I got it figured out
'TODO: This line of code loads data into the 'CSDataset.CryoSeries' table. You can move, or remove it, as needed.
Me.CryoSeriesTableAdapter.Fill(Me.CSDataset.CryoSeries)
'Pulling Data From Table Adapter
Dim db As New DataTables.CSDatasetTableAdapters.CryoSeriesTableAdapter
Dim result = db.GetData
Dim Model As String = CBmodel.Text.Trim
Dim Part As String = CBPart.Text.Trim
TBresult.Text = result.Rows(0).Item(Part)

Related

MDX Query returns value in report but not in Visual Basic code

This is for an application which dynamically sets data for and renders reports.
I have an MDX query for a report which relies on a parameter. The query is:
SELECT NULL ON COLUMNS, strtomember(#DateYear) ON ROWS FROM [MYDATACUBE]
When running this in the report query designer, it returns a value properly. However, when running this in visual basic code, it returns nothing. Here is the important part of my code:
Dim cn = New AdomdConnection(adomdparamconnectionstrings(countparamsadomd))
Dim da As AdomdDataAdapter = New AdomdDataAdapter()
Dim cmd = New AdomdCommand(paramcommands(countparamsadomd), cn)
Dim tbl = New DataTable
If (adomdparams) Then 'If there are parameters, adds them to the query
For l As Integer = 0 To (countparamsadomd - 1)
If (adomdparamconnectionstrings(l) = "NODATASET") Then
Dim p As New AdomdParameter(paramvaradomd(l), paramadomd(l))
cmd.Parameters.Add(p)
Else
Dim p As New AdomdParameter(paramvaradomd(l), adomdqueryvalues(l))
cmd.Parameters.Add(p)
End If
Next
End If
da.SelectCommand = cmd
cn.Open()
da.Fill(tbl)
cn.Close()
I know the connection string works because all the other datasets use the same one. I know the command is right using break points. I know the parameter's value is right also using break points. I know the code overall works because it works with every dataset I've tested it with except this one. Using break points, everything seems to work as it does for other datasets, but then it just doesn't return any values.
The table resulting from this does have a properly named column ([Date].[Year].[Year].[MEMBER_CAPTION]) but has no rows. The returned value should be a single row with the year in it.
I have done something similar in C#.
If you have access to the dimension and hierarchy name, then you can have a generic query like this:
WITH
MEMBER [Measures].[Member Caption] AS StrToMember(#Hierarchy).HIERARCHY.CURRENTMEMBER.MEMBER_CAPTION
SELECT
[Measures].[Member Caption] ON COLUMNS,
StrToMember(#DateYear) ON ROWS
FROM
[MYDATACUBE]
Note that you'll have to add the parameter, #Hierarchy, to the command before executing the query. There is a little trick in the value expression for the calculated member. When passing a hierarchy expression to StrToMember(), it returns the default member from that hierarchy. From the default member, you can use the HIERARCHY function to work backwards to get the hierarchy. From the hierarchy, you can get the CURRENTMEMBER and its MEMBER_CAPTION property.
If you want a query specific for the hierarchy in your example, you can use:
WITH
MEMBER [Measures].[Member Caption] AS [Date].[Year].CURRENTMEMBER.MEMBER_CAPTION
SELECT
[Measures].[Member Caption] ON COLUMNS,
StrToMember(#DateYear) ON ROWS
FROM
[MYDATACUBE]

Sorting Data on Form in Access 2010 by VBA

I have the following setup:
Table "Mitarbeiter" (Users) with fields: "UNummer" / "Sortierung" /....
Table "Mo01" (a sheet for every month) with fields: "UNummer" / "01" / "02" / ....
The Field UNummer in Table Mo01 is a combination field that gets Mitarbeiter.UNummer and saves it as text
I call a Form "Monatsblatt" that is based on the table Mo01.
In that Form I have a Field "fldSort" that is calling "Sortierung" from table "Mitarbeiter". The Data in that field is based on "=DomWert("Sortierung";"Mitarbeiter";"UNummer = '" & [ID] & "'")"
This works and looks like this:
I am trying to sort the form by that "fldSort" in Form "Monatsblatt" by using this code:
Form_Monatsblatt.OrderBy = "fldSort"
Form_Monatsblatt.OrderByOn = True
When I start the form with that code running, Access asks for parameters:
I tried a lot of different ways of writing the code, referencing to the field in different ways. I do NOT want to base the form on anything other then the table.
Why not ask the wide world watch "Why Access asking me for Parameter"? That would have brought you to the clue I think. Debug.Print or MsgBox your .OrderBy and you see it's "fldSort", not a valid sort. Access is assuming you want to use a parameter called fldSort, but you want the string in the variable fldSort, but it's not recognized, because of the double quotes surrounding it. Everything between 2 double quotes is interpreted as a string, even it's a var name.
Delete the quotes and everything will work fine (if your sort string is sufficent)!
Form_Monatsblatt.OrderBy = fldSort
[Update]
Late, but now I see the clue. You added a calculated field to the form, but you can't sort or filter them.
Instead of appending this field to the table, create a query and add it there, then you bind the form to the query and add the field to the form. Now you can filter and sort as you like!
The query looks like this:
SELECT *,
Dlookup("Sortierung","Mitarbeiter","UNummer = '" & [ID] & "'") AS ldSort
FROM Mo01;
Or with a join:
SELECT
Mo01.*,
Mitarbeiter.Sortierung AS fldSort
FROM
Mo01
LEFT JOIN
Mitarbeiter
ON
Mo01.ID = Mitarbeiter.UNummer;
Now you can use
Form_Monatsblatt.OrderBy = "fldSort"
Form_Monatsblatt.OrderByOn = True
because you have a bound control called fldSort.
[/Update]

VB.NET tabledapter query insert into from another dataset

I have a situation where I want to Insert into access DB table from MS SQL table.
Same columns and everything.
I have both data sets and both table adapter. I can do what ever I want inside each dataset - any manipulation but I cannot insert from one table to another.
I tried creating an Insert query for destination tableadapter but I cannot get the from working. Tried linking, nothing works.
Searched for days, simply cannot find it.
Thank you for your answer. Can you help me on my example. I'm having trouble setting this up. This is what i got:
Dim myToTableTableAdapter As FirstDataSetTableAdapters.ToTableTableAdapter
myToTableTableAdapter = New FirstDataSetTableAdapters.ToTableTableAdapter()
Dim myFromTableTableAdapter As SecondDataSetTableAdapters.FromTableTableAdapter
myFromTableTableAdapter = New SecondDataSetTableAdapters.FromTableTableAdapter()
myFromTableTableAdapter = myToTableTableAdapter.Clone
'but it doesnt work from here`
What I wanted to do is:
For each drfrom As DataRow In myFromTableTableAdapter.GetData
myToTableTableAdapter.InsertInto(drfrom.item(column01), drfrom.item(column02), drfrom.item(andSoOn))
Next
But it seem to me that this would take so much longer then a "Insert Into From Select" script.
You cannot insert a row from one table into another table, but there are a couple of ways to do what you want. One way (a little verbose) is this:
' sets it up with same schema but empty rows
mOutTable = inTable.Clone
' Now insert the rows:
For Each rowIn In inTable.Rows
r = mOutTable.NewRow()
For Each col In inTable.Columns
r(col.ColumnName) = rowIn(col.ColumnName)
Next
mOutTable.Rows.Add(r)
Next
mOutTable.AcceptChanges
A second way, which is more concise, is this:
outTable = inTable.Clone
For Each inRow As DataRow In inTable.Rows
outTable.LoadDataRow(inRow.ItemArray, False)
End If
outTable.AcceptChanges
Note that both inTable and outTable are ADO.NET DataTable objects. You cannot implement my suggestion on the DataAdapter objects. You must use the DataTable objects. Each DataTable can be associated with a DataAdapter in the standard fashion for ADO.NET:
Dim t as New DataTable()
a.Fill(t);
where a is the ADO.NET DataAdapter. I hope this helps!
Jim

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)

How can i copy data table records of different field name based on mapping list evaluating condition on source data table?

I have a Source Data table of type System.Data.DataTable from which i have to generate Destination Data Table of mapped column(Consider same SqlDBType for mapped columns.)
I have a list of MappingInfo class in which each Source Datatable Column mapped with New Column Name which will be in destination data table.
Public Class MappingInfo
Public Property SourceFieldName As String
Public Property DestinationFieldName As String
End Class
I have to evaluate a condition in source datatable to allow row data copy in destination table.
I did this using following code snippet:
''Prepare destination table.
For Each oMapping In oMappingInfo
DestinationDataTable.Columns.Add( _
New DataColumn(oMapping.DestinationFieldName))
Next
For Each oRow In SourceDataTable.Rows ''Copy data.
If oRow("IsActive") Then
oDataRow = DestinationDataTable.NewRow
For Each oMapping In oMappingInfo
oDataRow(oMapping.DestinationFieldName) = _
oRow(oMapping.SourceFieldName)
Next
DestinationDataTable.Rows.Add(oDataRow)
End If
Next
The main drawback is that here i have minimum 40k records in source datatable and data is not possible to fetch from database as all changes with data committed only when user save his work. The generated destination table is been assigned as data source to grid control and to report for preview.
How can i achieve this efficiently using Linq or do anyone please suggest me best way to achieve this requirement.
I've not tried this, so I can't say for sure that it'll be faster, but it seems to me that you'd get much better speed using something like the following:
Dim l_destinationTable As DataTable
' This creates a copy of the structure and content
l_destinationTable = SourceTable.Copy()
For Each l_column As DataColumn in l_destinationTable.Columns
Dim l_columnMap = oMappingInfo.FirstOrDefault( _
Function (c) c.SourceFieldName = l_column.ColumnName )
If l_columnMap IsNot Nothing Then
' Rename the column if it is mapped
l_column.ColumnName = l_columnMap.DestinationFieldName
Else
' Drop the column if it is not mapped
l_destinationTable.Columns.Remove( l_column )
End If
Next
NOTE: This method will fail if an unmapped column is part of a relationship or another column's expression depends on this column. Also, if you are swapping the name of two columns (for example, A will be named B and B will be named A) then you will get an exception as two columns may not have the same name at the same time.