This is my code... I have errors on it...help!
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
Public Sub New()
MyBase.New()
If m_vb6FormDefInstance Is Nothing Then
If m_InitializingDefInstance Then
m_vb6FormDefInstance = Me
Else
Try
'For the start-up form, the first instance created is the default
instance.
If
System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is
Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
End If
End If
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents cboDelimiter As System.Windows.Forms.ComboBox
Public WithEvents txtTable As System.Windows.Forms.TextBox
Public WithEvents txtDatabaseFile As System.Windows.Forms.TextBox
Public WithEvents cmdImport As System.Windows.Forms.Button
Public WithEvents txtTextFile As System.Windows.Forms.TextBox
Public WithEvents _Label2_1 As System.Windows.Forms.Label
Public WithEvents _Label1_2 As System.Windows.Forms.Label
Public WithEvents _Label1_1 As System.Windows.Forms.Label
Public WithEvents _Label1_0 As System.Windows.Forms.Label
Public WithEvents Label1 As
Microsoft.VisualBasic.Compatibility.VB6.LabelArray
Public WithEvents Label2 As
Microsoft.VisualBasic.Compatibility.VB6.LabelArray
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Public WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.cboDelimiter = New System.Windows.Forms.ComboBox()
Me.txtTable = New System.Windows.Forms.TextBox()
Me.txtDatabaseFile = New System.Windows.Forms.TextBox()
Me.cmdImport = New System.Windows.Forms.Button()
Me.txtTextFile = New System.Windows.Forms.TextBox()
Me._Label2_1 = New System.Windows.Forms.Label()
Me._Label1_2 = New System.Windows.Forms.Label()
Me._Label1_1 = New System.Windows.Forms.Label()
Me._Label1_0 = New System.Windows.Forms.Label()
Me.Label1 = New
Microsoft.VisualBasic.Compatibility.VB6.LabelArray(Me.components)
Me.Label2 = New
Microsoft.VisualBasic.Compatibility.VB6.LabelArray(Me.components)
Me.Button1 = New System.Windows.Forms.Button()
Me.Label3 = New System.Windows.Forms.Label()
CType(Me.Label1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.Label2, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'cboDelimiter
'
Me.cboDelimiter.BackColor = System.Drawing.SystemColors.Window
Me.cboDelimiter.Cursor = System.Windows.Forms.Cursors.Default
Me.cboDelimiter.ForeColor = System.Drawing.SystemColors.WindowText
Me.cboDelimiter.Items.AddRange(New Object() {"*", ";", ",", "<tab>",
"<space>"})
Me.cboDelimiter.Location = New System.Drawing.Point(120, 144)
Me.cboDelimiter.Name = "cboDelimiter"
Me.cboDelimiter.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cboDelimiter.Size = New System.Drawing.Size(48, 21)
Me.cboDelimiter.TabIndex = 8
Me.cboDelimiter.Text = "*"
'
'txtTable
'
Me.txtTable.AcceptsReturn = True
Me.txtTable.AutoSize = False
Me.txtTable.BackColor = System.Drawing.SystemColors.Window
Me.txtTable.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtTable.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtTable.Location = New System.Drawing.Point(120, 120)
Me.txtTable.MaxLength = 0
Me.txtTable.Name = "txtTable"
Me.txtTable.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtTable.Size = New System.Drawing.Size(176, 19)
Me.txtTable.TabIndex = 5
Me.txtTable.Text = "DataValues"
'
'txtDatabaseFile
'
Me.txtDatabaseFile.AcceptsReturn = True
Me.txtDatabaseFile.AutoSize = False
Me.txtDatabaseFile.BackColor = System.Drawing.SystemColors.Window
Me.txtDatabaseFile.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtDatabaseFile.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtDatabaseFile.Location = New System.Drawing.Point(120, 96)
Me.txtDatabaseFile.MaxLength = 0
Me.txtDatabaseFile.Name = "txtDatabaseFile"
Me.txtDatabaseFile.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtDatabaseFile.Size = New System.Drawing.Size(176, 19)
Me.txtDatabaseFile.TabIndex = 3
Me.txtDatabaseFile.Text = "C:\Temp\test.mdb"
'
'cmdImport
'
Me.cmdImport.BackColor = System.Drawing.SystemColors.Control
Me.cmdImport.Cursor = System.Windows.Forms.Cursors.Default
Me.cmdImport.ForeColor = System.Drawing.SystemColors.ControlText
Me.cmdImport.Location = New System.Drawing.Point(120, 184)
Me.cmdImport.Name = "cmdImport"
Me.cmdImport.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cmdImport.Size = New System.Drawing.Size(81, 33)
Me.cmdImport.TabIndex = 2
Me.cmdImport.Text = "Import"
'
'txtTextFile
'
Me.txtTextFile.AcceptsReturn = True
Me.txtTextFile.AutoSize = False
Me.txtTextFile.BackColor = System.Drawing.SystemColors.Window
Me.txtTextFile.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtTextFile.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtTextFile.Location = New System.Drawing.Point(120, 72)
Me.txtTextFile.MaxLength = 0
Me.txtTextFile.Name = "txtTextFile"
Me.txtTextFile.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtTextFile.Size = New System.Drawing.Size(176, 19)
Me.txtTextFile.TabIndex = 0
Me.txtTextFile.Text = "C:\Temp\test.txt"
'
'_Label2_1
'
Me._Label2_1.BackColor = System.Drawing.SystemColors.Control
Me._Label2_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Label2_1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label2.SetIndex(Me._Label2_1, CType(1, Short))
Me._Label2_1.Location = New System.Drawing.Point(16, 144)
Me._Label2_1.Name = "_Label2_1"
Me._Label2_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label2_1.Size = New System.Drawing.Size(65, 17)
Me._Label2_1.TabIndex = 7
Me._Label2_1.Text = "Delimiter"
'
'_Label1_2
'
Me._Label1_2.BackColor = System.Drawing.SystemColors.Control
Me._Label1_2.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.SetIndex(Me._Label1_2, CType(2, Short))
Me._Label1_2.Location = New System.Drawing.Point(16, 120)
Me._Label1_2.Name = "_Label1_2"
Me._Label1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_2.Size = New System.Drawing.Size(73, 17)
Me._Label1_2.TabIndex = 6
Me._Label1_2.Text = "Table"
'
'_Label1_1
'
Me._Label1_1.BackColor = System.Drawing.SystemColors.Control
Me._Label1_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.SetIndex(Me._Label1_1, CType(1, Short))
Me._Label1_1.Location = New System.Drawing.Point(16, 96)
Me._Label1_1.Name = "_Label1_1"
Me._Label1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_1.Size = New System.Drawing.Size(96, 17)
Me._Label1_1.TabIndex = 4
Me._Label1_1.Text = "Database File"
'
'_Label1_0
'
Me._Label1_0.BackColor = System.Drawing.SystemColors.Control
Me._Label1_0.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_0.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.SetIndex(Me._Label1_0, CType(0, Short))
Me._Label1_0.Location = New System.Drawing.Point(16, 72)
Me._Label1_0.Name = "_Label1_0"
Me._Label1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_0.Size = New System.Drawing.Size(65, 17)
Me._Label1_0.TabIndex = 1
Me._Label1_0.Text = "Text File"
'
'Button1
'
Me.Button1.BackColor = System.Drawing.SystemColors.Control
Me.Button1.Cursor = System.Windows.Forms.Cursors.Default
Me.Button1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Button1.Location = New System.Drawing.Point(264, 184)
Me.Button1.Name = "Button1"
Me.Button1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button1.Size = New System.Drawing.Size(81, 33)
Me.Button1.TabIndex = 9
Me.Button1.Text = "Sort out data"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(8, 16)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(328, 32)
Me.Label3.TabIndex = 10
Me.Label3.Text = "Import Program from .txt to .mdb"
'
'Form1
'
Me.AcceptButton = Me.cmdImport
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(584, 294)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3,
Me.Button1, Me.cboDelimiter, Me.txtTable, Me.txtDatabaseFile, Me.cmdImport,
Me.txtTextFile, Me._Label2_1, Me._Label1_2, Me._Label1_1, Me._Label1_0})
Me.Location = New System.Drawing.Point(76, 101)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "Form1"
CType(Me.Label1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.Label2, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
#Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form1
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form1
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed
Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form1()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
Private Sub cmdImport_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdImport.Click
Dim delimiter As String
Dim contents As String
Dim lines() As String
Dim fields() As String
Dim wks As DAO.Workspace
Dim db As DAO.Database
Dim fnum As Short
Dim line_num As Short
Dim field_num As Short
Dim sql_statement As String
Dim num_records As Integer
delimiter = cboDelimiter.Text
If delimiter = "<space>" Then delimiter = " "
If delimiter = "<tab>" Then delimiter = vbTab
' Grab the file's contents.
fnum = FreeFile()
On Error GoTo NoTextFile
FileOpen(fnum, txtTextFile.Text, OpenMode.Input)
contents = InputString(fnum, LOF(fnum))
FileClose(fnum)
' Split the contents into lines.
lines = Split(contents, vbCrLf)
' Open the database.
On Error GoTo NoDatabase
wks = DAODBEngine_definst.Workspaces(0)
db = wks.OpenDatabase(txtDatabaseFile.Text)
On Error GoTo 0
' Process the lines and create records.
For line_num = LBound(lines) To UBound(lines)
' Read a text line.
If Len(lines(line_num)) > 0 Then
' Build an INSERT statement.
sql_statement = "INSERT INTO " & txtTable.Text & " VALUES ("
fields = Split(lines(line_num), delimiter)
For field_num = LBound(fields) To UBound(fields)
' Add the field to the statement.
sql_statement = sql_statement & "'" & fields(field_num)
& "', "
Next field_num
' Remove the last comma.
sql_statement = VB.Left(sql_statement, Len(sql_statement) -
2) & ")"
' Insert the record.
On Error GoTo SQLError
db.Execute(sql_statement)
On Error GoTo 0
num_records = num_records + 1
End If
Next line_num
' Close the database.
db.Close()
wks.Close()
MsgBox("Inserted " & VB6.Format(num_records) & " records")
Exit Sub
NoTextFile:
MsgBox("Error opening text file.")
Exit Sub
NoDatabase:
MsgBox("Error opening database.")
FileClose(fnum)
Exit Sub
SQLError:
MsgBox("Error executing SQL statement '" & sql_statement & "'")
FileClose(fnum)
db.Close()
wks.Close()
Exit Sub
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs
As System.EventArgs) Handles MyBase.Load
' Enter default file and database names.
txtTextFile.Text = VB6.GetPath & "\testdata.txt"
txtDatabaseFile.Text = VB6.GetPath & "\testdata.mdb"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim file As String = VB6.GetPath & "\testdata.txt"
Dim path As String = VB6.GetPath & "\Test1\"
Dim ds As New DataSet()
Try
Dim f As System.IO.File
If f.Exists(path & file) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " &
file, conn)
da.Fill(ds, "TextFile")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub
End Class