How to print N number of primes in Visual basic (forms)? - vb.net

I have this visual basic code to receive user input and print out that many primes. For instance if a user inputs 5, the output should be: 1, 3, 5, 7, 11. But I found difficulty with it.Here is my code:
Dim i, n, input, currentPrime As Integer
Dim Wrap As String = Chr(10) & Chr(13)
input = txtInput.Text
currentPrime = 1
txtAns.Text = "Prime Numbers are : " & Wrap
Do While (currentPrime <= input)
For i = currentPrime To input
For j = 2 To Fix(i / 2) + 1
If i Mod j = 0 Then
n = 1
End If
Next
If n = 1 Then
n = 0
Else
txtAns.Text = txtAns.Text & Wrap & i & " is a prime number " & Wrap
End If
Next
currentPrime += 1
Loop

Try this...
Dim i, n, input As Integer
Dim Wrap As String = Chr(10) & Chr(13)
input = txtInput.Text
Dim found = 0
Dim output = "Prime Numbers are : " & Wrap
While found < input
i = i + 1
For j = 2 To Fix(i / 2) + 1
If i Mod j = 0 Then
n = 1
End If
Next
If n = 1 Then
n = 0
Else
output = output & Wrap & i & " is a prime number " & Wrap
found = found + 1
End If
End While
txtAns.Text = output

Related

Error 1004 Macro Excel

I'm currently having some issues with VBA and the error 1004. I checked on the Internet but, unfortunately, couldn't find the solution.
I'm working on data transfer between Excel and an other base. I'm currently on the first part of the work : the shape of the new document before the transfer. In fact, I can have text with more than 250 characters on my first database while the other one wants me to split all of the documents each 250 characters (as you can check on the program) and add a number associated to help to gather the information.
It works very well until I reach the line Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("B" & ligneNC), 250 * (j - 1) + 1, 250) with j = 2, LigneLib = 3899 and a text with 257 characters. This code already worked for texts with more than 500 characters that is why I don't understand the issue.
Moreover, When I delete all of the lines to start the macro again, I still have the error on the same line on the first loop. However, it works again only when I restart Excel.
Please find below the details of the macro :
Option Explicit
Sub Libelle()
Dim ligneLib As Integer
Dim ligneNC As Integer
Dim endLoop As Integer
Dim i As Integer
Dim j As Integer
ligneNC = 3
ligneLib = 1
For i = 1 To 3003
endLoop = Round_Up(Len(Sheets("NCXL").Range("B" & ligneNC)) / 250)
For j = 1 To endLoop 'Texte description
Sheets("LibImport").Range("A" & ligneLib) = "100"
Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-DESC"
Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO"
Sheets("LibImport").Range("D" & ligneLib) = j
Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("B" & ligneNC), 250 * (j - 1) + 1, 250)
ligneLib = ligneLib + 1
Next
endLoop = Round_Up(Len(Sheets("NCXL").Range("C" & ligneNC)) / 250)
For j = 1 To endLoop 'Texte cause
Sheets("LibImport").Range("A" & ligneLib) = "100"
Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-CAUSE"
Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO"
Sheets("LibImport").Range("D" & ligneLib) = j
Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("C" & ligneNC), 250 * (j - 1) + 1, 250)
ligneLib = ligneLib + 1
Next
endLoop = Round_Up(Len(Sheets("NCXL").Range("E" & ligneNC)) / 250)
For j = 1 To endLoop 'Texte action corrective
Sheets("LibImport").Range("A" & ligneLib) = "100"
Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-DSCCOR"
Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO"
Sheets("LibImport").Range("D" & ligneLib) = j
Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("E" & ligneNC), 250 * (j - 1) + 1, 250)
ligneLib = ligneLib + 1
Next
endLoop = Round_Up(Len(Sheets("NCXL").Range("D" & ligneNC)) / 250)
For j = 1 To endLoop 'Texte action curative
Sheets("LibImport").Range("A" & ligneLib) = "100"
Sheets("LibImport").Range("B" & ligneLib) = Sheets("NCXL").Range("A" & ligneNC) & "-DECIS"
Sheets("LibImport").Range("C" & ligneLib) = "NONCONFO"
Sheets("LibImport").Range("D" & ligneLib) = j
Sheets("LibImport").Range("F" & ligneLib) = Mid(Sheets("NCXL").Range("D" & ligneNC), 250 * (j - 1) + 1, 250)
ligneLib = ligneLib + 1
Next
ligneNC = ligneNC + 1
Next
End Sub
Function Round_Up(ByVal val As Double) As Integer
Dim result As Integer
result = Round(val)
If result >= val Then
Round_Up = result
Else
Round_Up = result + 1
End If
End Function
Thanks,
Cédric.
The 251st character of your example text is an = symbol. When you write that to a cell, Excel assumes you're entering a formula.
To work around this issue, precede the text with a ' as you would if you were keying it manually:
Sheets("LibImport").Range("F" & ligneLib) = "'" & Mid(Sheets("NCXL").Range("B" & ligneNC), 250 * (j - 1) + 1, 250)
They won't be visible in Excel, but you might need to account for them if you're doing other things later with the contents of that cell.

Read Each line not reading through entire file

I am using Vb to take a .txt file, parse it, and check for errors. My code works just fine, however, the code does not go through the entire file. It stops, on average, 20 lines shy of the EOF.
I am using the following
For Each lines As String In System.IO.File.ReadLines(myFile)
from here I parse the line and see if it needs any fixes.
Is there something that I'm missing or something that just cant be avoided.
The files that I'm reading in are about 150,000 KB to 230,000 KB and over 2 million lines.
As requested, the following is my code. Warning, I just started using Vb...
Module Module1
Sub Main()
Dim root As String = "C:\Users\mschramm\Documents\Agco\WindSensor\Data\filestobecleaned\"
Dim datafile As String = root & "ES.txt"
Dim outfile As String = root & "temptry.txt"
Dim output As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(outfile, False)
Dim k As UInteger = 0
Dim fixes As UInteger = 0
Dim time As ULong = 0
Dim count As UInteger = 0
Dim n As UInteger = 0
Dim LineCount As UInteger = 0
Dim TimeStep As ULong = 100
Dim Solar As UInteger = 0
For Each lines As String In System.IO.File.ReadLines(datafile)
LineCount = LineCount + 1
'Console.WriteLine(LineCount)
Dim parsedline As String() = Split(lines, ",")
If IsNumeric(parsedline(0)) = True And UBound(parsedline) = 8 Then
'TimeStep = parsedline(0) - time
Solar = parsedline(1)
time = parsedline(0)
output.WriteLine(lines & " Good Line")
count = count + 1
Else
Dim j As UInteger = 0
Dim ETX As Integer = 0
Dim STX As Integer = 0
Dim datacheck As Boolean = False
Dim fixedline As String = ""
Dim newtime As ULong = 0
For j = 0 To UBound(parsedline)
Dim a As Char = parsedline(j)
If a = (Chr(3)) Then ETX = j
If a = (Chr(2)) Then STX = j
Next
j = 0
If (STX < ETX) And (ETX - STX) = 6 And STX >= 2 Then
If Len(parsedline(STX + 1)) = 8 And Len(parsedline(STX + 2)) = 8 And Len(parsedline(STX + 3)) = 8 Then
Dim g = Len(parsedline(STX - 2))
While (j < g) And datacheck = False
If IsNumeric(parsedline(STX - 2)) Then
If parsedline(STX - 2) - time < 10000 And parsedline(STX - 2) - time > 0 Then
newtime = Right(parsedline(STX - 2), Len(parsedline(STX - 2)))
Solar = parsedline(STX - 1)
'TimeStep = newtime - time
fixedline = newtime & "," & parsedline(STX - 1) & "," & parsedline(STX) & "," & parsedline(STX + 1) & "," & parsedline(STX + 2) & "," & parsedline(STX + 3) & "," & parsedline(STX + 4) & "," & parsedline(STX + 5) & "," & parsedline(STX + 6) & " Fixed Line"
datacheck = True
Else
j = j + 1
parsedline(STX - 2) = Right(parsedline(STX - 2), Len(parsedline(STX - 2)) - 1).ToString
End If
Else
j = j + 1
parsedline(STX - 2) = Right(parsedline(STX - 2), Len(parsedline(STX - 2)) - 1).ToString
End If
End While
End If
End If
If (STX < ETX) And (ETX - STX) = 6 And STX = 0 Then
If Len(parsedline(1)) = 8 And Len(parsedline(2)) = 8 And Len(parsedline(3)) = 8 And Len(parsedline(4)) = 1 And Len(parsedline(5)) = 2 And Len(parsedline(6)) = 3 Then
newtime = time + TimeStep
fixedline = newtime & "," & Solar & "," & parsedline(STX) & "," & parsedline(STX + 1) & "," & parsedline(STX + 2) & "," & parsedline(STX + 3) & "," & parsedline(STX + 4) & "," & parsedline(STX + 5) & "," & parsedline(STX + 6) & " Fixed Line Gave Time and Solar"
datacheck = True
End If
End If
If newtime < time And newtime > 1000 Then
Dim badtime As ULong = newtime
Dim firstdig As ULong = time
Dim loopcount As UInteger = 0
While firstdig > 9
firstdig = firstdig / 10
loopcount = loopcount + 1
End While
firstdig = firstdig * (10 ^ loopcount)
If (firstdig + badtime) > time Then
newtime = firstdig + badtime
If (newtime - (10 ^ loopcount)) > time Then
newtime = newtime - (10 ^ loopcount)
End If
End If
End If
If datacheck = True Then
k = k + 1
If (newtime > 500) Then
output.WriteLine(fixedline)
'count = count + 1
time = newtime
End If
End If
If datacheck = False Then
n = n + 1
If STX >= 0 And ETX > 0 And ETX - STX < 9 Then
Console.WriteLine(LineCount)
'n = n + 1
End If
End If
End If
Next
Console.WriteLine(count & " Good lines")
Console.WriteLine(k & " Lines Corrected")
Console.WriteLine(LineCount & " Total Lines")
Console.WriteLine(n & " Lines were thrown out")
Console.WriteLine(n / LineCount * 100 & "% thrown out")
End Sub
End Module
and here is a sample of the data
Time: 16:52:18.0
Date: 11/6/2014
Time,Sensor1,U,V,W
544161,219,Q,-001.341,+000.947,+000.140,M,00,17
544284,218,Q,-001.207,+001.074,+000.225,M,00,1C
544361,220,Q,-000.935,+000.898,+000.187,M,00,17
544460,220,Q,-001.299,+001.151,-000.009,M,00,17
This is what the last 10 lines look like
Q,+001.681,-003.510,-0356154697,236,Q,+000.826,-002.744,-000.559,M,00,19
Q,+000.474,-002.789,-0356155062,234,Q,+000.400,-002.975,+000.438,M,00,1D
Q,+000.813,-002.934,-0356155297,236,Q,+000.146,-002.129,-000.235,M,00,16
Q,+000.494,-002.234,+0356155497,236,Q,+000.681,-001.996,-000.248,M,00,1F
Q,+000.800,-001.999,-0356155697,236,Q,+001.181,-002.883,-000.795,M,00,1A
356156060,233,Q,+000.400,-002.106,+000.251,M,00,18
356156296,235,Q,+000.888,-001.026,+000.442,M,00,10
356156495,236,Q,+000.570,-001.694,+000.589,M,00,13
356156695,236,Q,+001.495,-002.177,-000.035,M,00,15
356157060,234,Q,+000.770,-003.484,-000.161,M,00,14
for this file, the code makes it to the 6th to last line.
Thanks to mafafu for pointing out the solution.
I never closed the file, so the addition of output.Close() fixed everything.
Once again, thank you mafafu.

Got error of the multiplication table in visual basic, can anyone help to correct please?

lblTable.Text = ""
For i = 1 To 4
For j = 1 To 4
lblTable.Text = i & "×" & j & "=" & i * j
Next j
Next i
As the code shows above, I tried to make multiplication table from 1 to 4, but when I ran the program, the table would all show in one row, not like the sample. Is there anything miss in my code?
Not really sure how you're getting that "table" to display in the form (you must have omitted the code, because the code you provide simply replaces lblTable.Text in the inner loop each time. In any case, you can write each out to an array, and the drop the array to the worksheet like so:
Dim arr(1 to 4, 1 to 4) as String
lblTable.Text = ""
For i = 1 To 4
For j = 1 To 4
lblTable.Text = i & "×" & j & "=" & i * j
arr(i,j) = lblTable.Text
Next j
Next i
Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
Format your label with a non-proportional font so everything lines up.
Dim s as string, i, j
s = ""
For i = 1 To 4
For j = 1 To 4
s = s & Left(i & "×" & j & "=" & (i * j) & " ", 9)
if j = 4 Then s = s & chr(10)
Next j
Next i
lblTable.Text = s

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

Array not printing in 2D form inside a textBox in visualbasic

Private Sub Command4_Click()
Dim x As Integer
r = InputBox("Enter row size ")
c = InputBox("Enter column size ")
ReDim arr(r, c) As Integer
For i = 0 To r - 1 Step 1
For j = 0 To c - 1 Step 1
arr(i, j) = InputBox("Enter row : " & (i + 1) & "column size : " & (j + 1))
Next j
Next i
For i = 0 To r - 1
For j = 0 To c - 1
Text1.Text = Text1.Text & " " & arr(i, j)
Next j
Text1.Text = Text1.Text & vbNewLine & vbCr
Next i
End Sub
This is my code for taking inputs in an array. Here everything is working fine except this line "Text1.Text = Text1.Text & vbNewLine & vbCr" here I am trying to print the array in row-column in 2D form inside a text box but its not happening "vbNewLine or vbcr" both are not working and my array is getting printed in a single line.
I suggest vbCrLf instead of vbNewLine & vbCr, and you need to make sure you have your textbox set to Multiline in the properties editor.