How can I properly use the for...next statement? - vb.net

I'm new to vb.net and English is not my first language, Please note this :)
I've try nested for...next loop but there's a problem which that value does not increase :(
Please help me!
Dim h As Integer = 8
Dim i As Integer = 5
Dim time As String = Format(Now, "yyyy-MM-dd HH:mm:ss")
Dim pno As String = PatNo.Text
Dim itname() As String =
{"<R>SPH", "<R>CYL", "<R>AX", "<R>AVE", "<L>SPH", "<L>CYL", "<L>AX", "<L>AVE"}
Dim list As List(Of String) = New List(Of String)()
For a As Integer = 8 To 18
list.Add(ReceiveBox.Lines(a))
Next
Dim arrlist() As String = list.ToArray()
For linerepeat As Integer = 1 To 3
If linerepeat = 3 Then
i -= 3
Else i = 5
End If
For g As Integer = 0 To 10
For f As Integer = 0 To 7
For number As Integer = 1 To 3
cmd.Connection = conn
Dim itvalue As String = Mid(arrlist(g), h, i)
Dim query As String = "INSERT INTO EYE_TEST VALUES ('" & time & "','" & pno & "','" & txt.Text & "','" & itname(f) & number & "','" & itvalue & "')"
cmd.CommandText = query
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
h += 7
Next
If h = 29 Then h = 8
Next
'If g = 3 And 5 And 9 Then Exit For
Next
Next

To reproduce your error I copied your code block and commented out lines of code that I cannot reproduce (for instance, your SQL command).
Dim h As Integer = 8
Dim i As Integer = 5
' Dim time As String = Format(Now, "yyyy-MM-dd HH:mm:ss")
' Dim pno As String = PatNo.Text
' Dim itname() As String =
'{"<R>SPH", "<R>CYL", "<R>AX", "<R>AVE", "<L>SPH", "<L>CYL", "<L>AX", "<L>AVE"}
' Dim list As List(Of String) = New List(Of String)()
' For a As Integer = 8 To 18
' list.Add(ReceiveBox.Lines(a))
' Next
' Dim arrlist() As String = list.ToArray()
For linerepeat As Integer = 1 To 3
If linerepeat = 3 Then
i -= 3
Else : i = 5
End If
For g As Integer = 0 To 10
For f As Integer = 0 To 7
For number As Integer = 1 To 3
'cmd.Connection = conn
'Dim itvalue As String = Mid(arrlist(g), h, i)
'Dim query As String = "INSERT INTO EYE_TEST VALUES ('" & time & "','" & pno & "','" & txt.Text & "','" & itname(f) & number & "','" & itvalue & "')"
'cmd.CommandText = query
'cmd.CommandType = CommandType.Text
'cmd.ExecuteNonQuery()
h += 7
Next
If h = 29 Then h = 8
Next
'If g = 3 And 5 And 9 Then Exit For
Debug.Print(" g:" & g)
Next
Next
I then added Debug.Print(" g:" & g) at the end of your "g" for loop to see if it was iterating. When I debug this I get the following output:
g:0
g:1
g:2
g:3
g:4
g:5
g:6
g:7
g:8
g:9
g:10
g:0
g:1
g:2
g:3
g:4
g:5
g:6
g:7
g:8
g:9
g:10
g:0
g:1
g:2
g:3
g:4
g:5
g:6
g:7
g:8
g:9
g:10
This shows that the "g" for loop is iterating.
Try adding Debug.Print(" g:" & g) at the end of your "g" for loop. When you run the program the results will show in the Output window (if you do not see this window, click View -> Output). If you do not get the same results as me then I would expect the problem to be caused by your SQLConnection object or the query you try to execute. Maybe during one of the iterations a value does not get set properly and an error is caused which might end your for loop unexpectedly.

Related

Displays the result many times in Mulitselect Listbox Search in VB6

I am creating a program in vb6 with ms access. while i am searching the database from multi select list box in vb it displays the results wrongly.
if i click the first item it shows one time
if i click second item it shows that item two times
it i click third item it shows that item three times.
how to solve this
i tried the below code
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
If str <> "" Then str = str & ""
If Val(List1.SelCount) = 1 Then
str = List1.List(List1.ListIndex)
Else
str = str & " or name= " & List1.List(List1.ListIndex)
End If
End If
Next i
If str <> "" Then
Set rs = db.OpenRecordset("select * from Customers where name= '" & str & "'")
display
End If
result
Kumar vasanth vasanth kannan kannan kannan
Try this:
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim str As String
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then str = str & " or name = '" & List1.List(i) & "'"
Next i
str = Mid(str, 4)
If str <> "" Then
Set rs = db.OpenRecordset("select * from Customers where " & str)
display
End If
End Sub

Visual Basic: how can I display certain values from a group of characters

Here we are finding the eight adjacent numbers that have the highest sum and displaying that sum. We also need to have it display the eight adjacent numbers that add up to this value. I am stuck on how to display these values. My code for what I have so far is below:
Dim chars As Char() = "73167176531330624919225119674426574742355349194934" &
"96983520312774506326239578318016984801869478851843" &
"85861560789112949495459501737958331952853208805511" &
"12540698747158523863050715693290963295227443043557" &
"66896648950445244523161731856403098711121722383113" &
"62229893423380308135336276614282806444486645238749" &
"30358907296290491560440772390713810515859307960866" &
"70172427121883998797908792274921901699720888093776" &
"65727333001053367881220235421809751254540594752243" &
"52584907711670556013604839586446706324415722155397" &
"53697817977846174064955149290862569321978468622482" &
"83972241375657056057490261407972968652414535100474" &
"82166370484403199890008895243450658541227588666881" &
"16427171479924442928230863465674813919123162824586" &
"17866458359124566529476545682848912883142607690042" &
"24219022671055626321111109370544217506941658960408" &
"07198403850962455444362981230987879927244284909188" &
"84580156166097919133875499200524063689912560717606" &
"05886116467109405077541002256983155200055935729725" &
"71636269561882670428252483600823257530420752963450"
Dim index As String = 0
Dim x = 0
Dim values = Array.ConvertAll(chars, Function(c) CInt(c.ToString()))
Dim maxSum = 0
For i = 0 To values.Length - 8
Dim sum = values(i)
For x = i + 1 To i + 7
sum += values(x)
index = i
Next
If sum > maxSum Then
maxSum = sum
End If
Next
Console.WriteLine(index)
Console.WriteLine(maxSum)
Console.Read()
End Sub
Here's my take on it using two different approaches. The first is a more traditional approach, while the second utilizes LINQ:
Sub Main()
Dim chunkSize As Integer = 8
Dim source As String =
"73167176531330624919225119674426574742355349194934" &
"96983520312774506326239578318016984801869478851843" &
"85861560789112949495459501737958331952853208805511" &
"12540698747158523863050715693290963295227443043557" &
"66896648950445244523161731856403098711121722383113" &
"62229893423380308135336276614282806444486645238749" &
"30358907296290491560440772390713810515859307960866" &
"70172427121883998797908792274921901699720888093776" &
"65727333001053367881220235421809751254540594752243" &
"52584907711670556013604839586446706324415722155397" &
"53697817977846174064955149290862569321978468622482" &
"83972241375657056057490261407972968652414535100474" &
"82166370484403199890008895243450658541227588666881" &
"16427171479924442928230863465674813919123162824586" &
"17866458359124566529476545682848912883142607690042" &
"24219022671055626321111109370544217506941658960408" &
"07198403850962455444362981230987879927244284909188" &
"84580156166097919133875499200524063689912560717606" &
"05886116467109405077541002256983155200055935729725" &
"71636269561882670428252483600823257530420752963450"
Dim strChunk As String
Dim strMaxChunk As String = ""
Dim curSum, MaxSum As Integer
Dim values() As Integer
For i As Integer = 0 To source.Length - chunkSize
strChunk = source.Substring(i, chunkSize)
values = Array.ConvertAll(strChunk.ToCharArray, Function(c) CInt(c.ToString()))
curSum = values.Sum
If curSum > MaxSum Then
MaxSum = curSum
strMaxChunk = strChunk
End If
Next
Console.WriteLine("Traditional")
Console.WriteLine("Max Sum = " & MaxSum & " from " & strMaxChunk)
Dim sums = From chunk In Enumerable.Range(0, source.Length - chunkSize).Select(Function(x) source.Substring(x, chunkSize))
Select chunk, sum = Array.ConvertAll(chunk.ToCharArray, Function(y) CInt(CStr(y))).Sum
Order By sum Descending
Dim linqResult = sums.First
Console.WriteLine("Linq")
Console.WriteLine("Max Sum = " & linqResult.sum & " from " & linqResult.chunk)
Console.ReadLine()
End Sub

Function to return info

I changed the above to use string builder but some reason its not comming through on the loop its returning ok through the OrdersLine variable but not to the stream . Below is the loop im declaring it in
Dim OrdersLine As String
For Each item As String In split
For Each thisEntry As DataRow In orderHeaderInformation.Rows
orderLineInformation = connection.SqlSelectToDataTable(scriptBuilder.GetOrderLineInformation(item, thisEntry.Item("location")))
Dim orderNumber = From row In newEntries.AsEnumerable()
Select row.Field(Of String)("ordernumber") Distinct
For Each c In IO.Path.GetInvalidFileNameChars
filename = thisEntry.Item("orderNumber").ToString().Replace(c, "")
Next
ediExportPath = configuration.EditExport
filename = ediExportPath & "\" & filename & "_" & thisEntry.Item("location") & ".edi"
Dim streamWriter As New IO.StreamWriter(filename)
OrdersLine = ExportOrdersLine(orderLineInformation).ToString()
streamWriter.WriteLine(OrdersLine)
streamWriter.Close()
streamWriter.Dispose()
Next
Next
Public Function ExportOrdersLine(editProductLine As DataTable) As String
Dim retVal As String
Dim newRecord As infoEDILine
Dim filenameWithoutExtensions As String
Dim i As Integer = 1
Dim edilIneOrder As New StringBuilder
For Each thisentry In editProductLine.Rows
edilIneOrder.AppendLine("LIN+" & i & thisentry.Item("TagBcode") & ":EN'")
edilIneOrder.AppendLine("PIA+1" & thisentry.Item("PLU") & ":SA'")
edilIneOrder.AppendLine("IMD+C++CU'")
edilIneOrder.AppendLine("IMD+F++:::" & thisentry.Item("Style.Description") & "'")
edilIneOrder.AppendLine("QTY+" & thisentry.Item("PLU") & ":1'")
edilIneOrder.AppendLine("QTY+" & thisentry.Item("OnOrder") & ":1'")
edilIneOrder.AppendLine("TAX+7+VAT+++:::00" & thisentry.item("VatRate") & "'")
' if the vat rate is zero add three zeros to above line
' if the vat rate is not zero add only two 00 lke above line
' if no decimal places add one decimal place of zero
edilIneOrder.AppendLine("MOA+203:" & thisentry.item("LineNetCost") & "'")
edilIneOrder.AppendLine("PRI++AAA:" & thisentry.Item("GrossCost") & "'")
edilIneOrder.AppendLine("PRI++AAB:" & thisentry.Item("WholeSaleCost") & "'")
edilIneOrder.AppendLine("UNS+S'")
i = i + 1
Next
Return edilIneOrder.ToString()
End Function
Turns Out I was missing
streamWriter.AutoFlush = True

string format using with increment

i have a ticket_no field which has a format of "storecode - datetoday - n" what i'm trying to is when as long as the date is today the "n" will just increment but if the date changes the "n" will reset to 1.
TMP_SQL = "select max(ticket_no) from tbl_main where store_id = '" + frm_store.store_code + "'"
Dim OBJCMD As New SqlCommand(TMP_SQL, OBJCON)
OBJREADER = OBJCMD.ExecuteReader()
Dim ydate As String
ydate = Now.ToString("MMddyy")
With OBJREADER
.Read()
Dim x As string
Dim str As String
If IsDBNull(OBJREADER(0)) = False Then
str = OBJREADER(0)
x = Int32.Parse(OBJREADER(0).ToString().Split("-")(1))
If x <> ydate Then
tmp = 0
tmp = Int32.Parse(OBJREADER(0).ToString().Split("-")(2)) + 1
Else
tmp = Int32.Parse(OBJREADER(0).ToString().Split("-")(2)) + 1
End If
End If
End With
txtTicketno.Text = frm_store.store_code & "-" & Now.ToString("MMddyy") & "-" & tmp

VBA - Loop through x items in blocks of 100 x times

I've been struggling with this for a day now.
I have a list box that is populated with x items. Could be 1 - x
I need to take all the items in the list box and format them into a string
which I submit into an oracle database. I'm using INLIST on the SQL side and because of that I can only have a maximum of 100 items in my string.
So for example if I was to have 547 items in the listbox, I would iterate through all 547 items, but at every 100 I would submit into the database, returning my result into my collection class, finishing with the last 47.
here's what i have so far. There is some attempts to solve my problem in the code so if it's confusing i'll try to explain.
Public Function SearchBMS()
On Error GoTo HandleError
Dim rst As ADODB.Recordset
Dim sESN As String
Dim i As Integer
Dim x As Integer
Dim maxrec As Integer
Dim itemcnt As Integer
Dim iBlockCount As Integer
With frmEngineCampaignSearch.lstbxESNNumbers
itemcnt = .ListCount - 2
'iBlockCount = GetBlockCount(itemcnt)
x = 0
maxrec = 100
Debug.Assert itemcnt = 200
For i = 0 To itemcnt
For x = i To maxrec
MsgBox "test", vbOKOnly
i = i + 100
Next x
If i = itemcnt Then ' if I = last item than we put the closing parenthesis on our string
sESN = sESN & "'" & .list(i) & "'"
Else
sESN = sESN & "'" & .list(i) & "' , " ' otherwise there are more items so we seperate by comma
End If
If itemcnt <= 100 Then
Set rst = Nothing
'Set rst = rstGetCustomerInfo(sESN)
'LoadRSTToCollection rst
elseif
While x = maxrec
MsgBox "submit first 100", vbOKOnly
'Set rst = Nothing
'Set rst = rstGetCustomerInfo(sESN)
'LoadRSTToCollection rst
sESN = gC_sEMPTY_STRING
maxrec = maxrec + 100
Wend
x = x + 1
Next i
End With
HandleError:
If Err.Number > 0 Then
MsgBox Err.Number & ": " & Err.Description
End If
This function is to get the number of times I would have to perform the submission but i hit a road block on how to use it within the for loop
Public Function GetBlockCount(ByRef lItemCnt As Long) As Integer
Dim x As Double
If lItemCnt <= 100 Then
GetBlockCount = 1
Exit Function
ElseIf lItemCnt > 100 Then
x = Round(lItemCnt / 100)
If lItemCnt Mod 100 > 0 Then
x = x + 1
Else
GetBlockCount = x
Exit Function
End If
End If
End Function
Any help would be much appreciated.
I think you need to clean it out and make it more readable. Then look at it and the solution will be much clearer.
Here is a simple skeleton of what it should look like:
I = 100
Txt = Get100Requests(I)
Do While Txt <> ""
'use txt
I = I + 100
Txt = Get100Requests(I)
Loop
Function Get100Requests(FromItem As Integer) As String
If FromItem => frmEngineCampaignSearch.lstbxESNNumbers.ListCount Then Exit Function
Dim I As Integer
I + FromItem
Do While I < FromItem + 99 And I < frmEngineCampaignSearch.lstbxESNNumbers.ListCount
Get100Requests = Get100Requests & "'" & frmEngineCampaignSearch.lstbxESNNumbers.list(i) & "', "
I = I + 1
Loop
Get100Requests = Left(Get100Requests, Len(Get100Requests)-2)
Exit Function