VB.net SQL output loop rename file error on next loop - sql

new to the stack here. I am developing some tools for work that allow us to pull information from a database and then operate on it with additional custom code. Since I need the output to not have a header, I want to have one file (as defined by schema.ini) that I will temporarily write the data into then copy and rename it before starting again. Using VS2017, when I debug it spits out the error that it cannot find RawC.txt after the first iteration. I cannot seem to figure out why. It is probably something simple but I have been unable to locate online. Can anyone help me out here? Below is the block I am having trouble with:
For each of the three files, output the sorted list. Schema has the correct format
For index = 0 To 2
If index = 0 Then
whicharr = arrSt(1)
ElseIf index = 1 Then
whicharr = arrSe(1)
ElseIf index = 2 Then
whicharr = arrVi(1)
End If
stopFile = "SELECT " & complist(index) & " INTO [Text;Database=" & TMPath & "].[RawC.txt] FROM [" & whicharr & "] ORDER BY " & complist(index)
cmd = New OleDbCommand(stopFile, conn)
cmd.ExecuteNonQuery()
'Now Copy and rename this file
My.Computer.FileSystem.CopyFile(TMPath & "\RawC.txt", TMPath & "\" & whicharr & ".txt")
'And delete the old RawC.txt file
My.Computer.FileSystem.DeleteFile(TMPath & "\RawC.txt")
Next
The final output from this loop should be three unique files that I will pass to another code that will perform some math on it.

Related

Open a file in a new instance of program

All;
I have a bit of code I've written that opens a design blueprint when I scan a bar code. It works well enough, but I'd like to open a new instance of the design software (Solidworks) and have the print display in the new instance. Right now, no matter how many Solidworks instances I have open, the print will only open in the first instance started.
The line commented out below is the line that works, just not in the right instance. The line below that is what I'd expect to work, but it returns a 'file not found' even though the path to solidworks and the print path are both correct.
Any explanation as to why this isn't working would be much appreciated as I'm obviously very new at this...and have no idea what I'm doing.
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim barcode As String = tb_barcode.Text
Dim filename As String = tb_barcode.Text
'Add File Extension to end of path
Dim ext As String = ".SLDDRW"
'Split job number from detail number in barcode textbox
barcode = Split(tb_barcode.Text, ".")(0)
filename = Split(tb_barcode.Text, ".")(1)
'- This works, just in primary instance
'System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)
'- This does not work
System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Catch
MessageBox.Show("File Not Found")
End Try
End Sub
Sorry for naive approach but shouldn't there be a comma in Process.Start between 2 arguments?
Start(String, String)
Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component. docs
Why don't you use the Application.ExecutablePath.That returns the Application's path with its full name. Then your code should be
System.Diagnostics.Process.Start(Application.Executablepath, "G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Also make sure that the second string argument is a valid path.

Write a program to create a file that consists of only 2 fields from another text file with 6? (VB2012)

I'm using
Dim sw As IO.StreamWriter = IO.File.CreateText("Justices1.txt")
to create the file but I'm having trouble writing the code that will take only specific parts of the original "Justices.txt" file and put it into "Justices1.txt"
The first line of the original Justices file looks like this:
Henry,Baldwin,Andrew Jackson,PA,1830,1844
And I'm trying to get it to this in the file I'm creating (Justices1):
Henry Baldwin,PA
Sorry if this is a stupid question- I'm new to this.
I'd normally suggest using a StringBuilder but I figure your app won't be burning too many CPU cycles with something simple like this:
Private Sub FormatJusticesFile(filePath As String, formattedFilePath As String)
IO.File.WriteAllLines(formattedFilePath,
From line In IO.File.ReadAllLines(filePath)
Let terms = line.Split(","c)
Select "(" & IO.Path.GetFileNameWithoutExtension(filePath) & "): " & terms(0) & " " & terms(1) & "," & terms(3))
End Sub
Usage:
FormatJusticesFile("c:\Justices1.txt", "c:\Justices2.txt")

Try-catch issue

I am running across an issue in my program. I am reading a folder containing csv files and entering the data into a list of arrays. I have data enclosed within quotes.
However, the data is not pristine, and there are fields that have spare quotes that are causing issues.
I have set up a try/catch function that shunts the bad data into a manual verification routine. However, today the try/catch has started clearing the stack and reprocessing all the files from the top of the folder.
I have been using the messageboxes to trace exactly when the stack is getting cleared. It's happening at the start of the try/catch statement. It's not making it to the first messagebox.
Does anyone have any tips for continuing the program without losing the stack data?
files_ = Directory.GetFiles(path_, "*.ldf") 'picks out only the .ldf files
For i = 0 To files_.Length - 1 'i is the index for the number of ldf files in the folder. Number is i+1
i_string = CStr(i)
Using filereader1 As New Microsoft.VisualBasic.FileIO.TextFieldParser(files_(i), System.Text.Encoding.Default)
filereader1.TextFieldType = FieldType.Delimited
filereader1.Delimiters = New String() {","}
filereader1.HasFieldsEnclosedInQuotes = True
While Not filereader1.EndOfData
Try
split_string = filereader1.ReadFields()
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MessageBox.Show(ex.Message & " : " & FileName & " ; " & filereader1.ErrorLine & " LIST")
error_throw = filereader1.ErrorLine
MsgBox("LIST error throw")
Throw_Error = True
End Try

How can I remove the initial return when the multiple texts are added to the field?

I am asking the user to select a txt file from a specified folder on a server [This is in PowerPoint 2007], but I need to give them the option of selecting more than one, so I have a bit of conditional code to determine this.
One file selected uses this code:
oShape.TextFrame.TextRange.Text = Text
More than one file selected currently uses this:
oShape.TextFrame.TextRange.Text = oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf & Text
…but this results in an extra return space above it all in the field, which is a bit untidy.
Could anyone advise me on how I can modify this to only get the returns in between the two texts, but not at the beginning?
I am not entirely sure I got the problem, but I believe this will do:
oShape.TextFrame.TextRange.Text = iif(oShape.TextFrame.TextRange.Text <> "", oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf, "") & Text

AppendText won't append to the next line in a text file / vb

I'm making a program that lets you add student info to an existing CSV text file but whenever I use append text it adds the new info to part of the last line and then a new line.
I want it to do this:
John Doe,29,Male
John Doe,29,Male
It does this instead:
John Doe,29,MaleJo
hn Doe,29,Male
Note: there isn't actually an empty line between each set of info, I just wanted it to be easy to read when I posted it here.
Here is the code for that section:
Dim swVar As IO.StreamWriter = IO.File.AppendText(frmMain.fileName)
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()
Any help would be appreciated, thanks!
A handy tool in VS2010 (and others) is snippets - right click Insert Snippets... - lots of code patterns for typical tasks. In your case here is a modified snippet:
Sub AddToFile(textToAdd As String, filePath As String)
My.Computer.FileSystem.WriteAllText(filePath, textToAdd, True)
End Sub
You may want to check/add a vbNewLine to the text being added since one is not automatically added as with WriteLine.
Not a direct reply to your stated problem, but an alternate method.
See if something like this works better:
IO.File.AppendText(frmMain.fileName, vbNewLine & txtName.Text & "," & txtAge.Text & "," & gender & vbNewLine)
AppendText will open write and close all in one operation so there's no need for a separate streamwriter. It also doesn't add newlines so those must be added separately
Unless of course you are doing a series of writes to the same file then something like this would probably be more appropriate:
Dim swVar As New IO.StreamWriter(frmMain.fileName, True)
'write your lines here
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()