Getting Column Index from SSDBGrid in VB6 - datagrid

I'm producing a report in Crystal Reports where the column order must match the order that they're displayed in in the SSDBGrid in VB6.
To do this, I decided it was best to loop over each column in the grid, and call a function to set the parameter field values to the correct heading (so, column 0's caption would be the value for parameter field #Col1, etc)
Anyway, the code I have for this is
Dim c As Column
Dim cName As String
For Each c In FShow_All_Accounts.grd_accounts.Columns
cName = "#Col" & c.ListIndex
Call setColumnHeaders(c.Index)
crxReport.ParameterFields.GetItemByName(cName).AddCurrentValue ("f")
Next
The problem is that first of all, setting the type of c to Column and looping over each c in grd_accounts seems to be incorrect - grd_accounts is an SSDBGrid, and secondly, it errors when trying to return the index.
So, my question(s):
What is the correct way to loop over each column in an SSDBGrid?
Secondly, how would I then get the column index for the correct column, to pass into the function?

The answer is actually more simple than you're imagining it to be. Your code is very nearly there.
Dim c As Column
Dim i As String
Dim cname As String
i = 0
For Each c in grd_accounts.Columns
i = i + 1
cName = "#Col" & i
crxReport.ParameterFields.GetItemByName(cName).AddCurrentValue (c.Caption)
Next
No need for the helper function. This will iterate over each column, set cName to be "#Col" and the value of i (Also the column number), and therefore that parameter field in the report will be captioned with that columns caption.

Related

How to have a SQLite query with a variable part?

Hi I am trying to create a query for SQLite which has a variable part in it. By variable I mean that a certain part within the string can possibly contain a variable but also an empy value
I tried this but I am not sure whether this works.
SELECT * FROM table WHERE attr LIKE 'ABC% %DEF'
Adding onto my comment, check the below code to test your values.
SELECT CASE WHEN 'ABC G DEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END as test_space,
CASE WHEN 'ABCGGGDEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END AS test_all

SQLite count number of occurence of word in a string

I want to count number of occurrences of a word in a string for example ,
[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]
in above string i want to count no of occurrences of a word 'DELIVERED' here it is 2.
i want to get result 2. pls help me on this. i should have to use only sql query to achieve this.
thanks in advance.
If your table's name is tablea and the column's name is col:
SELECT
(LENGTH(col) - LENGTH(REPLACE(col, '"DELIVERED"', '')))
/
LENGTH('"DELIVERED"') as counter
from tablea
remove every occurrence of "DELIVERED" and subtract the length of the string from the original string and finally divide the result with the length of "DELIVERED"
Assuming your data is in a table something like:
CREATE TABLE example(json TEXT);
INSERT INTO example VALUES('[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]');
and your instance of sqlite has the JSON1 extension enabled:
SELECT count(*) AS "Number Delivered"
FROM example AS e
JOIN json_each(e.json) AS j
WHERE json_extract(j.value, '$.messageStatus') = 'DELIVERED';
gives you:
Number Delivered
----------------
2
This will return the total number of matching entries from all rows in the table as a single value. If you want one result per row instead, it's an easy change but the exact details depend on your table definition. Adding GROUP BY e.rowid to the end of the query will work in most cases, though.
In the long run it's probably a better idea to store each object in the array as a single row in a table, broken up into the appropriate columns.

Merging two tables and returning value through r script

I am attempting to add a dynamic column to a table in spotfire that is updated using r-script/data functions in order to handle different variable types. When you just insert columns, it does not allow you to change the column from a text value to a string value.
The basic code structure is create a new table by merging the base table with the information table, select a column header to populate the new column from, and return the calculated column values to the base table. Parameters are as follows:
Input Parameters:
Name Type
columnMatch Value
baseTable Table
infoTable Table
Output Parameters (to be added to baseTable)
Name Type
outputColumn Column
Script
newTable <- merge(baseTable,infoTable, by = "uniqueIdentifier")
cnames <- colnames(newTable)
outputColumn <- newTable[,match(colorSelection, cnames, nomatch=1)]
outputColumn
The issue thatI am having is as follows:
The code is not returning the correct value for the correct uniqueIdentifier. Is there a way that I can make the values line up, or sort the table in order to return the correct value for the correct uniqueIdentifier?
Thanks!
Jordan
EDIT: found out how to dynamically refer to column number using match function.

How to concat string with For Varible Vb

I have 16 items named q1,q2,q3,...,q16 and txt_q1,txt_q2,...,txt_q16
And I need to check each one, so I have a for and inside I have the If like this:
For Cnt AS Integer = 1 To 16 Step 1
If("q"&cnt = "") Then
"txt_q"&cnt.Style.Add("color","blue")
....
..
End If
End For
The idea is check al items with a For to avoid the 16 Ifs, but I have a syntax error.
What can I do?
the txt_q1,txt_q2... are IDs for asp:labels, and I have a radiobuttonList, so I get the radiobutton text and I set q1, q2, q3... to the corresponding radiobuttonlist. So I want to check if there is a radiobuttonlist that is not checked, if not I change the color to blue to the asp:label. So I want to avoid making 16 ifs for each radiobuttonlist and make it with a For, because the only thing that change in the variables is the number is the same "q" and the same "txt_q" so I want to add the number to "q" or "txt_q" to make it one variable called q1 or txt_q1 that already exist and that way acces to the txt_q1.Style.Add() and change the color of that label.
Thank You
Just for your understanding:
When you write "txt_q"&cnt, you are creating a new string literal which holds e.g. "txt_q1", "txt_q2", etc. This resembles the name of the variables, but is really something completely different. The name of the variable is just for you to write in code. The value of the variable is a reference to the control object. What you need is to obtain the reference to the controls. This can be done on several ways, one is to use the FindControl() method.
For Cnt AS Integer = 1 To 16 Step 1
Dim c = FindControl("q" & cnt) 'pass in the ID here, you will get a reference
'you can use c to access the properties e.g. c.Style
End For
Again, just to clarify
c.Style 'c is a variable holding a reference to the control
"c".Style ' "c" is a simple string

Method to remove empty rows from DataTable?

I parsed through an Excel spreadsheet and returned the entire result as a DataTable. However, this Excel spreadsheet has several empty rows that I would like to eliminate from the resulting DataTable. In particular, each empty row begins with an empty cell. So, I know that the entire row is empty if the value of the cell at the first index is empty. Note that I cannot simply modify the Excel spreadsheet because I have to work with exactly what the client has sent to me. Based on this information, I assumed that I could perform the following function to remove empty rows:
' DataTable dt has already been populated with the data
For Each row As DataRow In dt.Rows
If dt.Rows.Item(0).ToString = "" Then
dt.Rows.Remove(row)
ElseIf dt.Rows.Item(0) Is Nothing Then
dt.Rows.Remove(row)
End If
Next
However, after crafting this solution, I am greeted with the following error:
Collection was modified; enumeration operation might not execute.
I now realize that I cannot alter the collection as I access it. How can I get around this? I'm wondering if I should create a new DataTable with the rows that aren't empty. Is this the best approach or are there better options?
EDIT: I have also tried iterating over the rows backwards:
For i = dt.Rows.Count() - 1 To 0
If dt.Rows.Item(i).Item(0).ToString = "" Then
dt.Rows.RemoveAt(i)
End If
Next
You can't modify the collection while you're enumerating it with For Each, but a simple For loop will work. You'll need to loop backwards to avoid skipping the row after a removed row.
You've also got your tests the wrong way round; if Item(0) returns Nothing, then Item(0).ToString will throw a NullReferenceException.
I'm assuming the dt.Rows.Item(0) is a typo, and should read row.Item(0) instead.
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
Dim row As DataRow = dt.Rows(i)
If row.Item(0) Is Nothing Then
dt.Rows.Remove(row)
ElseIf row.Item(0).ToString = "" Then
dt.Rows.Remove(row)
End If
Next
Vb.Net using linq
Dtset.Tables(0).AsEnumerable().Where(Function(row) row.ItemArray.All(Function(field) field Is Nothing Or field Is DBNull.Value Or field.Equals(""))).ToList().ForEach(Sub(row) row.Delete())
Dtset.Tables(0).AcceptChanges()

Resources