I have 2 strings as:
STRING 1:
#serial1#code1#true#serial2#code2#false#serial3#code3#true
STRING 2:
#serial1#code1#false#serial2#code2#false#serial3#code3#false
what i need is information(serial1,code1,true/false) for the group for which it is changed. Like in above example i want data only for (serial1,code1,t/f) and (serial3,code3,t/f)
STRING1 is loaded on page_load and STRING2 will be loaded after click of save button. So i want to hit the database for only those values which are changed not for all the values.
Please suggest some suitable logic.
Thanks in Advance.
You can use String.Split() to turn your string into a String Array, and then evaluate the array items.
Example:
strOriginal() = string1.Split("#")
strChanged() = string2.Split("#")
' Assuming both strings had the same number of elements...
For i as Integer = 0 to strOriginal.Length - 1
If strOriginal(i) <> strChanged(i) Then
' Your DB write logic here...
End If
Next
Related
I have some information in a list (called listLines). Each line below is in a List(Of String).
1|This is just a header
3|This is just a footer
2|3456789|0000000|12312312313|BLUE|1|35.00
2|7891230|0000000|45645645655|BLUE|1|22.00
2|7891230|0000000|45645645658|RED|2|13.00
2|3456789|0000000|12312312316|RED|2|45.00
2|3456789|0000000|12312312317|YELLOW|5|-9.00
2|3456789|0000000|12312312315|ORANGE|3|15.00
2|7891230|0000000|45645645659|YELLOW|5|32.00
2|3456789|0000000|12312312314|GREEN|4|-20.00
2|7891230|0000000|45645645656|GREEN|4|39.00
2|7891230|0000000|45645645657|ORANGE|3|-18.50
I'm doing a listLines.sort() on the list to sort it alphabetically. Below is what I get after the .sort().
1|This is just a header
2|3456789|0000000|12312312313|BLUE|1|35.00
2|3456789|0000000|12312312314|GREEN|4|-20.00
2|3456789|0000000|12312312315|ORANGE|3|15.00
2|3456789|0000000|12312312316|RED|2|45.00
2|3456789|0000000|12312312317|YELLOW|5|-9.00
2|7891230|0000000|45645645655|BLUE|1|22.00
2|7891230|0000000|45645645656|GREEN|4|39.00
2|7891230|0000000|45645645657|ORANGE|3|-18.50
2|7891230|0000000|45645645658|RED|2|13.00
2|7891230|0000000|45645645659|YELLOW|5|32.00
3|This is just a footer
With that said, I need to output this information to a file. I'm able to do this ok. I still have a problem though. There is a sequence number in the above data at position 5 just after the listed colors (RED, BLUE, ETC..) that you can see. It's just before the last value which is a decimal type.
I need to further sort this list, keeping it in alphabetical order since position 2 is an account number and I want to keep the account numbers grouped together. I just want them to be resorted in sequential order based on the sequence number.
I was looking at another thread trying to figure out how I can do this. I found a piece of code like listLines.OrderBy(Function(q) q.Substring(35)).ToArray. I think this would probably help me if this was a fixed length file, it isn't however. I was thinking I can do some kind of .split() to get the 5th piece of information and sort it but then it's going to unalphabetize and mix the lines back up because I don't know how to specify to still keep it alphabetical.
Right now I'm outputting my alphabetical list like below so I can format it with commas and double quotes.
For Each listLine As String In listLines
strPosition = Split(listLine, "|")
Dim i As Integer = 1
Dim iBound As Integer = UBound(strPosition)
Do While (i <= iBound)
strOutputText = strOutputText & Chr(34) & strPosition(i) & Chr(34) & ","
i += 1
Loop
My main question is how do I re-sort after .sort() to then get each account (position1) in sequential order (position 5)? OR EVEN BETTER, how can I do both at the same time?
The List(Of T) class has an overload of the Sort method that takes a Comparison(Of T) delegate. I would suggest that you use that. It allows you to write a method or lambda expression that will take two items and compare them any way you want. In this case, you could do that like this:
Dim items = New List(Of String) From {"1|This Is just a header",
"3|This Is just a footer",
"2|3456789|0000000|12312312313|BLUE|1|35.00",
"2|7891230|0000000|45645645655|BLUE|1|22.00",
"2|7891230|0000000|45645645658|RED|2|13.00",
"2|3456789|0000000|12312312316|RED|2|45.00",
"2|3456789|0000000|12312312317|YELLOW|5|-9.00",
"2|3456789|0000000|12312312315|ORANGE|3|15.00",
"2|7891230|0000000|45645645659|YELLOW|5|32.00",
"2|3456789|0000000|12312312314|GREEN|4|-20.00",
"2|7891230|0000000|45645645656|GREEN|4|39.00",
"2|7891230|0000000|45645645657|ORANGE|3|-18.50"}
items.Sort(Function(x, y)
Dim xParts = x.Split("|"c)
Dim yParts = y.Split("|"c)
'Compare by the first column first.
Dim result = xParts(0).CompareTo(yParts(0))
If result = 0 Then
'Compare by the second column next.
result = xParts(1).CompareTo(yParts(1))
End If
If result = 0 Then
'Compare by the sixth column last.
result = xParts(5).CompareTo(yParts(5))
End If
Return result
End Function)
For Each item In items
Console.WriteLine(item)
Next
If you prefer a named method then do this:
Private Function CompareItems(x As String, y As String) As Integer
Dim xParts = x.Split("|"c)
Dim yParts = y.Split("|"c)
'Compare by the first column first.
Dim result = xParts(0).CompareTo(yParts(0))
If result = 0 Then
'Compare by the second column next.
result = xParts(1).CompareTo(yParts(1))
End If
If result = 0 Then
'Compare by the sixth column last.
result = xParts(5).CompareTo(yParts(5))
End If
Return result
End Function
and this:
items.Sort(AddressOf CompareItems)
Just note that this is rather inefficient because it splits both items on each comparison. That's not a big deal for a small list but, if there were a lot of items, it would be better to split each item once and then sort based on those results.
I have a Query qryRuleSets which outputs a table with 19 fields (that I do not want to save into an access table before that is suggested). I would like to get the field names and store them into an array so I can use that array in a for loop later on.
To find the number of fields that in the query result (to use in for loop later on) I have implemented the following, where the number of fields is stored in the variable numberfields -
numberfields = CurrentDb.QueryDefs("qryrulesets").Fields.Count
To actually get the name of these fields and store them in an array I am running into 2 problems:
1. Getting the field names from the query result
2. Setting up a dynamic array so that if the query ends up returning a table with more or less than 19 fields, it will still work
For my first problem:
I have tried to follow the steps in the following link: Get Column name from a query table but I can't figure it out.
To get the field names from the qry result I have tried the following but I'm not overly knowledgeable in vba/access so finding it hard to understand, even after a whole lot of googling:
Dim qry As QueryDef
Dim fieldNames As QueryDef
Dim firstcol As String
Set fieldNames = CurrentDb.CreateQueryDef(qry.qryrulesets)
firstcol = fieldNames.field(0).Name
For my second problem:
To store values in an array I have tried the following (as a test) and it works but I have to define the size of the array. Is there a way where it can be dynamic, i.e based on the value of the number of fields (found above stored in numberfields) :
Dim vardata(30) As Variant
For i = 1 To numberfields
vardata(i) = "hello"
Next i
I tried making the '30' above to a variable value but it didn't like that.
Any and all help will be appreciated. Thanks!
You can do like this:
Public Function GetFieldNames(ByVal QueryName As String) As String()
Dim Query As DAO.QueryDef
Dim FieldNames() As String
Dim Index As Integer
Set Query = CurrentDb.QueryDefs(QueryName)
ReDim FieldNames(0 To Query.Fields.Count - 1)
For Index = LBound(FieldNames) To UBound(FieldNames)
FieldNames(Index) = Query.Fields(Index).Name
Next
GetFieldNames = FieldNames()
End Function
I have a file array which carries strings in the following format:
"abc12345.xxx_xxx001.001045"
The string stored in above format helps me do a portion of my job
Now, having stored this, I want to extract a portion of this string and store it an array
How do I extract "abc12345" alone from this string and store it an array?
I tried using the split command. The problem am facing is:
PN(0) stores abc12345
PN(1) stores xxx_xx001
PN(2) stores 001045
I want abc12345 to be stored as PN(0) and next subsequent part number to be stored as PN(1)
Copy Code
Dim PN As New ArrayList()
For Each element In file1array
PN = element.split("."c)
Next
Assuming that every element contains a period and the first segment is the part number,
pn = file1Array.Select(
function(element as string)
return element.Split("."c)(0)
end function
).ToArray()
This would return pn as an array of string, not an ArrayList.
It's saying, for each element in file1Array, split element into an array and return the first element in that array (the part number.)
Then take all of those values and put them in an array.
You can use substring and indexOf:
Dim list As New ArrayList()
For Each Str As String In file1array
list.Add(Str.Substring(0, Str.IndexOf(".")))
Next
Why not just use the split function like you are doing and just concatenate index 1 and 2 to make the result you want?
Dim PN As New ArrayList()
For Each element In file1array
PN = element.split("."c)
PN(1) = PN(1) & PN(2)
Next
Not sure if I'm doing something wrong or not, basically my code starts at "111111111" and counts up by adding "1" to the original number every time the thread is able to. I want the method to skip 0's in the sequence though, instead of going to "111111120" after "111111119" I would like it to go straight to "111111121".
Private Sub IncreaseOne()
If count < 999999999 Then
count += 1
Else
done = True
End If
If CStr(count).Contains("0") Then
MsgBox("theres a 0 in that...darn.")
CStr(count).Replace("0", "1")
End If
End Sub
*note, my message box displays when it is suppose to but, 0s are not changed to 1s
Replace returns a string with the effects of the Replace, It doesn't work in place....
(Remember, in NET the strings are immutable objects)
Dim replaced = CStr(count).Replace("0", "1")
However you need to convert the string obtained to an integer and reassign to count.
count = Convert.ToInt32(replaced)
Replace is a function that returns a sting.
In other words, you need a variable to hold the result, like this:
Dim newValue = CStr(count).Replace("0", "1")
Dim strTest(recordSet.Count)() As String
While index < recordSet.Count
strTest(index)(recordset.item(index).getvalue(0).tostring,
recordset.item(index).getvalue(0).tostring,
recordset.item(index).getvalue(0).tostring)
index +=1
End While
Then once the array i populated above, i need to find the if another variable is contained in the second dimension of the second paren contains the value of my variable like this.
if strTest.Contains(someVariable) then
extract the index and get the other two values in the array
end if
so basically is how do i perform the second block of code here.
For Each set As String() in strtest
If set(1).Equals(someVariable) Then
'Get set(0) and set(2)
End If
Next
Loop through the array pulling the items as an array and check the middle item. I think that's what your question was saying.
Perhaps you can use strTest.IndexOf()?