JSON Result object dynamically written to Excel Worksheet VBA - json

I am attempting to write a tabular result (columns and rows of data) from an API call into an Excel range / table.
I managed to get it working, but I'd like for this to be dynamic across data sets (my data sets can be any shape with differing field names). As can be seen in the image, I don't necessarily know that the output is 3 columns of data, but I would want to get this from the JsonResult object (it forms part of the object, but I don't know how to access / reference it).
The object also contains the column names of the data which I would like written to Excel as well (as headings), but I don't know how.
Finally I'd like to write the row data to Excel as well, but I don't know how to access these values without specific reference to them (e.g. "company_code").
TO SUMMARISE: The items in yellow in the screenshot as well as the column / field names should be dynamically read from the JsonResult object.
Any assistance would be appreciated.
JsonResult in text: "[{"company_code":"ABC","employee_code":"5","is_exception":"0"},{"company_code":"ABC","employee_code":"8","is_exception":"1"}]"
My code snippet (if it helps):
Set JsonResult = ParseJson(ParseJson(req.responseText)("results")(1)("findings"))("results")
Dim Values As Variant
ReDim Values(JsonResult.Count, 3)
Dim Value As Dictionary
Dim i As Long
i = 0
For Each Value In JsonResult
Values(i, 0) = Value("company_code")
Values(i, 1) = Value("employee_code")
Values(i, 2) = Value("is_exception")
i = i + 1
Next Value
Sheets("Sheet1").Range(Cells(1, 1), Cells(JsonResult.Count, 3)) = Values

This is what ended up working for me:
Dim Item As Dictionary
Dim i As Long, size As Long
size = UBound(JsonResult(1).Keys) + 1
[a1].Resize(, size) = JsonResult(1).Keys 'Header rows
i = 1
For Each Item In JsonResult
i = i + 1
Range("A" & i).Resize(, size) = Item.Items 'Result rows
Next Item
The keys from the collection / dictionary's first Item is used for the Header rows and the items from each item in the collection / dictionary is written to Excel after that.
Works like an absolute charm.

Related

Obtain list of occurring field values in report footer - ssrs

I'm trying to obtain a list of the different field values within an SSRS Report, and then display the result set on the page header.
See link below for an example.
I know that usually this would be completed simply with groups but the user does not want the Tasks split out.
Thanks
Example Report:
To get the unique values into a single cell, you will need to utilize the code behind functionality in SSRS.
Click on the background of your report and go to report properties -> Code. Paste this function into the window:
Public Shared Function RemoveDuplicates(ByVal items As Object()) As String()
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As String = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
In the expression in your table where you want the unique values list, insert this expression:
=Join(Code.RemoveDuplicates(LookupSet(1,1, Fields!ID.Value, "DataSet1")), ",")
"Fields!ID.Value" is a reference to the field in your dataset that you want unique values from.
"DataSet1" is the name of the dataset that your field is located in.
What happens is that at runtime, the entire set of values from your query column are passed to the VB function which uses returns an array of unique values. The expression in the report Joins the unique array values back together with a comma deliminator.

How to add labels dynamically to html

I'm getting data into my code behind file in the format of array list. The sproc will return several records depending upon the input parameter value. Now i need to display all these values in HTML as labels dynamically.
For example if i got 2 records, i just need to display those 2 records, if 10 records are returned then display 10 records. I don't want keep 10 static labels to bind all the time. I just looking to place only one label in html and want to use that one to display all the records returned by query. Will it be possible??
Sample code:
//Client side
<asp:Label ID="lblresult" runat="server" CssClass="label" ></asp:Label>
//server side
Dim arraylist as arrayList = //result from sproc
Dim lbltext As String = ""
For Each item In arraylist
Do While item.value = lbltext
Me.lblresult.Text = item.value
lbltext = item.value
Loop
Next
Please let me how to solve this? Thanks in advance!
Kinda late for this, but is this what you are looking for?
For i = 0 To YourArrayList.Count - 1
lblresult.Text &= YourArrayList(i).ToString & " "
Next
Also this will cause you an error:
Dim arraylist as arrayList = //result from sproc
arraylist is a reserved keyword. You might want to use another name.
You can try following technique which i often use in C#,Php etc:
hope this will give you an idea for doing it in your programming Language.
string rows;
for(int cnt=0;cnt<array.count;cnt++) {
rows = rows +""+array[cnt].value;
}
yourLable.text = rows; //here you are binding all your rows to your lable

VBA Access - Array via textbox

At the moment all my arrays are implemented in the vba code. This works fine
Dim COST As String
Dim GAT As String
Dim OND As String
COST = "C:\Users\update\COST.xlsb"
GAT = "C:\Users\BACKUP\GAT.xlsb"
OND = "C:\Users\BACKUP\OND.xlsb"
MyArray = Array(COST, GAT, OND)
However I would like to select the above arrays from a textbox via a form
Below code works but I have to implement the file path
MyArray = Array(Forms![LAYOUT_F]![Update_F])
with file path
MyArray = Array("C:\Users\BACKUP\" & Forms![LAYOUT_F]![Update_F])
Using the above will require me to be file name specific and doesn't take into account arrays plus the file paths are different
Is there a way of selecting declared arrays from a textbox
I would use a multi-line textbox, where the user e.g. enters
C:\Users\update\COST.xlsb
C:\Users\BACKUP\GAT.xlsb
and then use the Split() function on the line breaks:
MyArray = Split(Me!myTextbox, vbCrLf)
But I my have misunderstood your question - not sure what you mean by "selecting declared arrays from a textbox".

excel vba: recordset joining and performance

The Context
I have an app in excel VBA for making read-only queries on a remote database.
Queries are executed from UDF's. My app passes an array of data from the recordset object to the function and Excel's fast process for writing an array to a cell range is invoked.
The Challenge
The app must be able to optionally return field names at the top of the dataset. This is presenting a huge performance challenge for me. The only way I know of to append or prepend to a 2D array in VBA is to loop through the entire array. Normally, I'm spared such a loop by passing the recordset.getRows() object directly to my UDF. However, when combining the list of fields and the result of the query with the looping method (the only method I'm aware of) I double or triple my calculation time for sizable queries.
I benchmarked this: for a query of 2k rows and 5 fields, average calc time without field names included is 4.3 seconds, vs. 9.8 seconds with field names
My first try was to combine the field names and recordset on the server using a UNION clause in my select statement (my server is MySQL). This does not work, however, since UNION forces data-type equality, implicitly converting my numerical data to strings. To convert them back I'd have to loop through the array, negating any efficiency gained.
My Question
Is there any object method of the recordset object or of VBA arrays that could be called upon to prepend a row to a large array without looping through the entire large array? The field names are all known before the MySQL query is executed.
My loop for joining the arrays is below. Define a new array arr of length of the recordset + 1, then loop through it, first adding the fields, then each row of the recordset array:
For r = LBound(arr, 1) To UBound(arr, 1)
If r = LBound(arr, 1) Then
arr(r) = fieldArray
Else
arr(r) = Application.Index(rs_array, r - 1, 0)
End If
Next
Using Application.Index is possibly the slowest way to combine your arrays: use a regular nested loop instead and you won't even notice any hit -
Sub TT()
Dim a(1 To 2000, 1 To 10)
Dim b(1 To 2000, 1 To 10)
Dim cc(1 To 2000)
Dim r, c, t
t = Timer
For r = 1 To 2000
For c = 1 To 10
b(r, c) = a(r, c)
Next c
Next r
Debug.Print "Loop", Timer - t '>> 0.015625 sec
t = Timer
For r = 1 To 2000
cc(r) = Application.Index(a, r, 0)
Next r
Debug.Print "Index", Timer - t '>> 4.195313 sec
End Sub

How to convert an Image into BLOB (Binary Large Object) format

I need a comparison between an input image with the
blob field in MySql table. so that i need to conver the image into BLOB format.
i had tried a lot in previous days but faild to achieve a good,i here by summerise the result
of my search for this comparison:
convert the image into byte array using the following code
Dim fileName As String
ListBox1.Items.Clear()
fileName = "D:\me.jpeg"
Dim fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)' Create a byte array of file stream length
Dim ImageData As Byte() = New Byte(fs.Length - 1) {}'Read block of bytes from stream into the byte array
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))'Close the File Stream
fs.Close()
For j As Integer = 0 To ImageData.Length - 1
ListBox1.Items.Add(ImageData(j).ToString)' list out the byte array
Next
Retrive the blob field carasponds to the same field
ListBox1.Items.Clear()
mystring = "select image from blob_table where id='28' "
openconnection() ' Methode to open a mysql connection
cmd = New Odbc.OdbcCommand(mystring, myconnection)
imagebyte = cmd.ExecuteScalar
For j As Integer = 0 To imagebyte.Length - 1
ListBox2.Items.Add(imagebyte(j).ToString) ' list out the BOLB array
Next
closeconnection() ' Close the connection
Both the list boxes are shown in the image below.
Both the lists are entirely different, byte array is comparitevely very large than blob it having more than 10K values so the comparison become harder and does not give the expected result.
Is their any possibility to convert an image into blob format? through vb.net. or any other
alternate solution for this comparison
Thanks in Advance for positive response....!