使用VB.net创建新的Access记录时遇到问题

时间:2015-09-25 20:33:52

标签: vb.net oledb

关于我的技能的一点背景。 2006年,我教我自己使用VB6与Access数据库进行通信。我成功了,并通过教程和留言板为自己编写了几个方便的数据库。现在我想做另一个,但很多都改变了。毋庸置疑,我已经成为初学者了。

在表单加载时,我在datagridview中看到了我的数据。当我单击datagridview上的行标题时,它会将数据加载到文本框中。这一切都符合我的喜好。

我的问题是添加新记录。我搜索了这个网站,发现了一些我认为可行的建议,我实现了它们,无法让它们工作。提交按钮是我希望这种情况发生的地方。如果有人在那里可以帮助那将是太棒了。我的守则如下:

Public Class Form1
    Dim i As Integer
    Dim con As New OleDb.OleDbConnection        'THE CONNECTION OBJECT
    Dim dbProvider As String                    'HOLDS THE PROVIDER
    Dim dbSource As String                      'HOLDS THE DATA SOURCE
    Dim MyFolder As String                      'HOLDS THE DATABASE FOLDER
    Dim TheDatabase As String                   'HOLDS THE DATABASE NAME
    Dim FullDatabasePath As String              'HOLDS THE DATABASE PATH
    Dim ds As New DataSet                       'HOLDS THE DataSet OBJECT
    Dim da As OleDb.OleDbDataAdapter            'HOLDS THE DataAdapter OBJECT
    Dim sql As String                           'HOLDS THE SQL STRING


Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Me.PV_TableTableAdapter.Fill(Me.PVdbDataSet1.PV_Table)

    'SET UP THE PROVIDER
    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"

    'SET THE DATABASE AND WHERE THE DATABASE IS
    TheDatabase = "/PVdb.accdb"
    MyFolder = "C:\Pipe Vault"
    FullDatabasePath = MyFolder & TheDatabase

    'SET THE DATA SOURCE
    dbSource = "Data Source = " & FullDatabasePath

    'SET THE CONNECTION STRING
    con.ConnectionString = dbProvider & dbSource

    'OPEN THE DATABASE
    con.Open()

    'STORE THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
    sql = "SELECT * FROM PV_table"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "PV_Table")

    'Close THE DATABASE  '
    con.Close()
    'MessageBox.Show("Database is now closed")
End Sub

Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
    'LOADS TEXTBOXS WITH DATA FROM DATAGRID OBJECT
    i = DataGridView1.CurrentRow.Index
    txtLocation.Text = DataGridView1.Item(0, i).Value
    txtProjectName.Text = DataGridView1.Item(1, i).Value
    TxtPipeDate.Text = DataGridView1.Item(2, i).Value
    txtPipeNumber.Text = DataGridView1.Item(3, i).Value
    txtPipeSize.Text = DataGridView1.Item(4, i).Value
    txtAproxFootage.Text = DataGridView1.Item(5, i).Value
    txtInstallDate.Text = DataGridView1.Item(6, i).Value
End Sub

Private Sub TxtPipeDate_Click(sender As Object, e As EventArgs) Handles TxtPipeDate.Click
    MonthCalendar1.Visible = True
End Sub

Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) Handles MonthCalendar1.DateSelected
    TxtPipeDate.Text = MonthCalendar1.SelectionStart
    MonthCalendar1.Visible = False
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

    Dim cb As New OleDb.OleDbCommandBuilder(da)
    ds.Tables("PV_table").Rows(i).Item(1) = txtLocation.Text
    ds.Tables("PV_table").Rows(i).Item(2) = txtProjectName.Text
    ds.Tables("PV_table").Rows(i).Item(3) = TxtPipeDate.Text
    ds.Tables("PV_table").Rows(i).Item(4) = txtPipeNumber.Text
    ds.Tables("PV_table").Rows(i).Item(5) = txtPipeSize.Text
    ds.Tables("PV_table").Rows(i).Item(6) = txtAproxFootage.Text
    ds.Tables("PV_table").Rows(i).Item(7) = txtInstallDate.Text
    da.Update(ds, "PV_table")

    'MessageBox.Show("Data updated")
End Sub

Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click

    Dim a As Control

    'ENABLES TEXT BOXES
    For Each a In Me.Controls
        If TypeOf a Is TextBox Then
            a.Enabled = True
        End If
    Next

    'CLEARS CONTENTS OF TEXT BOXES
    txtLocation.Clear()
    txtProjectName.Clear()
    TxtPipeDate.Clear()
    txtPipeNumber.Clear()
    txtPipeSize.Clear()
    txtAproxFootage.Clear()
    txtInstallDate.Clear()

    'SETS STATES OF BUTTONS
    btnCommit.Enabled = True
    btnAddNew.Enabled = False
    btnUpdate.Enabled = False
    btnDelete.Enabled = False
End Sub

Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
    btnCommit.Enabled = False
    btnAddNew.Enabled = True
    btnUpdate.Enabled = True
    btnDelete.Enabled = True
End Sub

Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click


    Dim newRow As DataRow = ds.Tables("PV_Table").NewRow()
    Dim cb As New OleDb.OleDbCommandBuilder(da)

    newRow("Location") = txtLocation.Text
    newRow("Project Name") = txtProjectName.Text
    newRow("Pipe Date") = TxtPipeDate
    newRow("Pipe Number") = txtPipeNumber
    newRow("Pipe Size") = txtPipeSize.Text
    newRow("Aprox Footage") = txtAproxFootage.Text
    newRow("Install Date") = txtInstallDate.Text

    ds.Tables("PV_Table").Rows.Add(newRow)
    da.Update(ds, "PV_Table")

End Sub

0 个答案:

没有答案