select certain words from list of string after comma - vb.net

What i want to do is take particular values from a list of strings and store them into variables for that particular index.
Com317,subject,1,20,M,year1
the example is what is contained in each index of the list of strings. I want to select the 1 and the 20 from this list and store them into 2 variables. Been trying to do this with a for each loop but I'm not sure how i can single out these 2 values
thanks

You can use the split function to accomplish this:
Dim s As String = "Com317,subject,1,20,M,year1"
Dim splitVals As String() = s.Split(",")
MessageBox.Show(splitVals(2)) ' 1
MessageBox.Show(splitVals(3)) ' 20

maybe something like that should works
This is pseudocode (I dont have VS installed on this computer, so check for bugs)
dim st as string
st = "Com317,subject,1,20,M,year1"
dim var1 as integer
dim var2 as integer
var1 = CInt(st.split(",")(2))
var2 = CInt(st.split(",")(3))

Related

Storing field names from a query table into a dynamic array MS Access

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

SQL statement that selects array values

I am working on a visual basic project. I have a mdb database connected to my project. I want to add a SELECT query that finds the results which are in array that i give it on my program
I have tried to write a statement like that:
SELECT kodu, adi_soyadi, sectigi_ders_say
FROM ogrenciler
WHERE kodu IN ?
But it does not work. In my page codes I have an array and I want to find results from "ogrenciler" table where the "kodu" is in my array.
Well, you could send that array to a temp table in Access, but that would prevent more then one user using the software at the same time. (or you could add some user name to the temp table. However, if the array of choices is small, say about max 50, then you can create the sql string.
eg:
Dim MySQL As String = "SELECT * from tblHotels WHERE ID IN("
Dim IdList(5) As Integer
Dim i As Integer
For i = 1 To 5
IdList(i) = i
Next
Dim MyList As String = ""
For i = 1 To 5
If MyList <> "" Then MyList = MyList & ","
MyList = MyList & IdList(i)
Next
MySQL = MySQL & MyList & ")"
Using MyCon2 As New OleDbConnection(My.Settings.OLESQL)
Dim da As New OleDbDataAdapter(MySQL, MyCon2)
Dim rstDat As New DataTable()
da.Fill(rstDat)
For i = 0 To rstDat.Rows.Count - 1
Debug.Print(rstDat.Rows(i).Item("HotelName"))
Next ' etc etc. etc.
End Using
So you can use the SQL format of:
SELECT * FROM tblHotels where ID IN (1,2,3)
And thus build up the "list". The only downside to this approach is that the sql string is limited to 2000 characters. So, if your list is larger then say 50 or so items, then you have to adopt a different approach.

How to split a String using multiple delimeters

I'm using a very particular program that doesn't have many pre loaded functions on it. I would like to split a Date and time into individual variables. 6 variables to be exact
I get the date and time like this 5/28/2014 15:34:40 so I have 3 characters "/" " " and ":" separating the 6 variables I want, but I can only get one to work at a time. I have tried many other ways but the x.Split() is the only syntax that this program seems to like (PARCView).
Dim x as String = "5/28/2014 15:34:40"
Dim y as String() = x.Split(New Char() {":"c})
So how can I add all three at once or use consecutive steps to store each number as a unique variable?
You actually have the Char Array that you need, though it only has one delineator... just fill it with the other delineators and it will split it for you.
Dim y As String() = x.Split(New Char() {":"c, "/"c, " "c})
example:
Another option is to parse it as a datetime object which will contain all the parts as properties:
Dim teststr = "5/28/2014 15:34:40"
Dim dt = DateTime.Parse(teststr)

splitting a string to access integer within it

i have a string "<PinX F='53mm'></PinX>", I want to access the 53 within the string and do some addition to it and then add the answer back into that string. I've been thinking about this and wasn't sure whether this can be done with regular expression or not? Can anybody help me out.
thanks
Yes, you can use a regular expression. This will get the digits, parse them to a number, add one to it, and put it back in the string (that is, the result is actually a new string as strings are immutable).
string s = Regex.Replace(
input,
#"(\d+)",
m => (Int32.Parse(m.Groups[1].Value) + 1).ToString()
);
Take a look at the HTML Agility Pack.
A regular expression looks like a good fit for this particular problem:
\d+
Will match one or more digits.
Int32.Parse(Regex.Match("<PinX F='53mm'></PinX>", #"\d+").Value)
Will return 53.
In this single case yes. "'(.*?)' then access the first group, but if this is part of a larger xml regular expressions should not be used. You should utilize the xml parser build into .net find the attribute with xsd and get the value.
Alternatively, here's a small routine...
' Set testing string
Dim s As String = "<PinX F='53mm'></PinX>"
' find first occurence of CHAR ( ' )
Dim a As Integer = s.IndexOf("'")
' find last occurence of CHAR ( ' )
Dim b As Integer = s.LastIndexOf("'")
' get substring "53mm" from string
Dim substring As String = s.Substring(a, b - a)
' get integer values from substring
Dim length As Integer = substring.Length
Dim c As Char = Nothing
Dim result As String = Nothing
For i = 1 To length - 1
c = substring.Chars(i)
If IsNumeric(c) Then
result = result & c
End If
Next
Console.WriteLine(Int32.Parse(result))
Console.ReadLine()

VB: Need help splitting a string into 3 variables

I need to split one variable into 3 variables. For example I have a variable called Data and it contains AAA00000001. I need to have them be split between the "AAA", "0000000", and "1". I looked at the Split() function but didn't get a clear example for this situation. The reason I need to do this is because I want to store these 3 variables into fields for a MySQL database.
Are the three subvariables always of the same length?
If so, you can use Substrings:
Dim substring1 As String = Data.Substring(0, 3)
Dim substring2 As String = Data.Substring(3, 7)
Dim substring3 As String = Data.Substring(10, 1)
Assuming the string is ALWAYS the EXACT same length and need to be split at the SAME place, you can use Substring().
dim s as String = "AAA00000001"
dim s1 as String = s.Substring(0, 3)
dim s2 as String = s.Substring(3, 7)
dim s3 as String = s.Substring(10)
If they're not always the same length, you're probably going to need to use Regular Expressions.
Split will break your string apart based on a character, or group of. It's not appropriate here, that is unless you're always splitting on 0000000, which I doubt you are.
If you know that the first 3 characters will always be your first group, second 7 your next, and last character, your final group, you could do something like this.
This uses the Substring function, e.g.
Dim yourString as String = "AAA00000001"
Dim c1 As String = yourString.Substring(0, 3)
Dim c2 As String = yourString.Substring(3, 7)
Dim c3 As String = yourString.Substring(10, 1)