Newby: Need help converting VBA script to VB 2005
Hello,
I am new to programming. The following script needs to be converted from VBA to Visual Basic 2005.
Dim fileName1 = InputBox("Enter filename for filtering")
fileName2 = InputBox("Enter filename for output")
Open fileName1 For Input As #1
Open fileName2 For Output As #2
Do
Input #1, Data1 ' Gets number
Input #1, Data2 ' Gets name
Input #1, Data3 ' Gets model
Input #1, Data4 ' Gets description
Input #1, Data5 ' Gets interval
If Data5 = "0" Then Data6 = "a"
If Data5 = "1" Then Data6 = "b"
If Data5 = "2" Then Data6 = "c"
If Data5 = "3" Then Data6 = "d"
If Data5 = "4" Then Data6 = "e"
If Data5 = "5" Then Data6 = "f"
Print #2, Data1 & "," & Data2 & "," & Data3 & "," & Data4 & "," & Data6
Loop While Not EOF(1)
Close #1
Close #2
Is there someone who can help me with this?
[923 byte] By [
E00Y] at [2007-12-16]
Most of the old style visual basic stuff is kept in Microsoft.VisualBasic namespace.
I didn't try this code, but used that namespace as example.
Please note however, that this way of doing it isn't really recomended, but since it's what you asked for...
| |
Private Sub DoFile() Dim fileName1 As String = InputBox("Enter filename for filtering") Dim fileName2 As String = InputBox("Enter filename for output") Dim Data1, Data2, Data3, Data4, Data5, Data6 As String Try Microsoft.VisualBasic.FileOpen(1, fileName1, OpenMode.Input) Microsoft.VisualBasic.FileOpen(2, fileName2, OpenMode.Output) Do Microsoft.VisualBasic.Input(1, Data1) Microsoft.VisualBasic.Input(1, Data2) Microsoft.VisualBasic.Input(1, Data3) Microsoft.VisualBasic.Input(1, Data4) Microsoft.VisualBasic.Input(1, Data6) Select Case Data5 Case "0" Data6 = "a" Case "1" Data6 = "b" Case "2" Data6 = "c" Case "3" Data6 = "d" Case "4" Data6 = "e" Case "5" Data6 = "f" Case Else MsgBox("Error reading file") Microsoft.VisualBasic.FileClose(1) Microsoft.VisualBasic.FileClose(2) Exit Sub End Select Microsoft.VisualBasic.Print(2, Data1 & "," & Data2 & "," & Data3 & "," & Data4 & "," & Data6) Loop While Not Microsoft.VisualBasic.EOF(1) Catch ex As IO.IOException MsgBox("Error:" & vbNewLine & ex.ToString) End Try Microsoft.VisualBasic.FileClose(1) Microsoft.VisualBasic.FileClose(2) End Sub
|
I tried to run the script but ditn't succeed in getting the right output. I receive errors about end of file and error reading file.
In what format must the input file be filled?
You said that this is not the right way to doing it, what is the right way?
There was a error in my code above, while getting/writing data, i skipped Data5 i think :), I didn't test the code first. My Bad.
anyways, i would do it like this....
The file being read would look like this....
00number|0name|0model|0description|0interval|0
11number|1name|1model|1description|1interval|1
22number|2name|2model|2description|2interval|2
33number|3name|3model|3description|3interval|3
44number|4name|4model|4description|4nterval|4
55number|5name|5model|5description|5nterval|5
My code as follows
| |
Dim sFileIn, sFileOut As String Dim sItems As String() Dim FileReader As IO.StreamReader = Nothing Dim FileWriter As IO.StreamWriter = Nothing sFileIn = InputBox("Enter File in") sFileOut = InputBox("Enter File in") Try FileReader = IO.File.OpenText(sFileIn) FileWriter = IO.File.CreateText(sFileOut) Do Until FileReader.EndOfStream sItems = FileReader.ReadLine().Split(New Char() {"|"}) Select Case sItems(sItems.GetUpperBound(0)) Case "0" sItems(sItems.GetUpperBound(0)) = "a" Case "1" sItems(sItems.GetUpperBound(0)) = "b" Case "2" sItems(sItems.GetUpperBound(0)) = "c" Case "3" sItems(sItems.GetUpperBound(0)) = "d" Case "4" sItems(sItems.GetUpperBound(0)) = "e" Case "5" sItems(sItems.GetUpperBound(0)) = "f" End Select FileWriter.Write(String.Join("|", sItems) & vbNewLine) Loop Catch ex As IO.IOException MsgBox("Error:" & vbNewLine & ex.ToString) End Try FileReader.Close() FileWriter.Close()
|
I ran this, and got the following output..
00number|0name|0model|0description|0interval|a
11number|1name|1model|1description|1interval|b
22number|2name|2model|2description|2interval|c
33number|3name|3model|3description|3interval|d
44number|4name|4model|4description|4nterval|e
55number|5name|5model|5description|5nterval|f
I used the Pipe character | as the delimter, but you can easily switch it to somthing else if you are using a different delimiter.
Dustin.
it's quite simple. Simply add a open dialog box control to your form.
Here's sample code to use it. You'll have to edit it to match your file extensions and such... This code assumes the control is called OpenFileDialog1, and that the code is being called from the form.
the section that says .ShowDialog(Me) the (ME) means that the dialog owner is the callign form. You could set it to another form by using .ShowDialog(frmParentForm)
| |
Dim sFile As String = "" With OpenFileDialog1 .Filter = "Project files|*.zzz" .FilterIndex = 1 .Title = "Open data file" .FileName = "" .ShowDialog(Me) If .FileName = "" Then Exit Sub sFile = .FileName End With
|