简体   繁体   中英

Update Access database using Visual Studio 2015 - VB.net

I am trying to do a simple update to an Access 2016 database. I am using Visual Studio/VB.net. I have been able to do this already on a different form with no issues using the same type of coding (it's pretty basic, it was for a school project but not anymore). I have tried two different ways to do this...using the update table adapter, for example:

            MediatorsListTableAdapter.UpdateMediators(MediatorIDTextBox.Text, MediatorNameTextBox.Text, MaskedTextBox1.Text, MaskedTextBox2.Text, DateTimePicker1.Value,
            AvailabilityTextBox.Text, EmailTextBox.Text)

Using that method I always get a notImplemented exception thrown even though I have used a similar type of adapter elsewhere. Also I tried using a strung method (I know, not ideal):

saveInfo = "UPDATE mediatorsList(mediatorName, email, mediatorPrimaryPhone, mediatorSecondaryPhone, lastMediationDate, availability)
              VALUES('" & MediatorNameTextBox.Text & "','" & EmailTextBox.Text & "','" & MaskedTextBox1.Text & "','" & MaskedTextBox2.Text & "',
            '" & DateTimePicker1.Value & "','" & AvailabilityTextBox.Text & "', WHERE mediatorID = '" & MediatorIDTextBox.Text & "') "

But this method gives me the error of Syntax Error in UPDATE statement . Again I have used this method elsewhere with no problems. Below I will post all the code for this form.

Imports System.Data
Imports System.Data.Odbc ' Import ODBC class
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class editMediators

Dim NewData As Boolean
Dim objConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ECRDatabase.accdb")


' create functions for save or update 
Private Sub runAccessSQL(ByVal sql As String)
    Dim cmd As New OleDbCommand
    connect() ' open our connection
    Try
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = sql
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        conn.Close()
        MsgBox("Data Has Been Saved !", vbInformation)
    Catch ex As Exception
        MsgBox("Error when saving data: " & ex.Message)
    End Try
End Sub

Private Sub editMediators_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Me.MediatorsListTableAdapter.Fill(Me.ECRDatabaseDataSet.mediatorsList) 'loads current mediator information
    DateTimePicker1.Value = Today()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'update button
    NewData = True
    alertMsgBox2()

End Sub

Private Sub alertMsgBox2()

    Select Case MsgBox("Yes: Saves Changes," & vbNewLine &
           "No: Exits the mediator update window without saving," & vbNewLine &
           "Cancel: Returns to the mediator update window.", MsgBoxStyle.YesNoCancel, "Update Mediator Information")
        Case MsgBoxResult.Yes

            MediatorsListBindingSource.EndEdit()
            updateMediator()

         'intentionally commented out                
         'MediatorsListTableAdapter.UpdateMediators(MediatorIDTextBox.Text,    MediatorNameTextBox.Text, MaskedTextBox1.Text, MaskedTextBox2.Text, DateTimePicker1.Value,
            'AvailabilityTextBox.Text, EmailTextBox.Text)

           ' Me.Close()

        Case MsgBoxResult.No
            MediatorsListBindingSource.CancelEdit()
            Me.Close()
    End Select

End Sub

Private Sub updateMediator()
    Dim saveInfo As String
    If NewData Then
        Dim Message = MsgBox("Are you sure you want to update mediator information? ", vbYesNo + vbInformation, "Information")
        If Message = vbNo Then
            Exit Sub
        End If
        Try
            'Update mediator information
            saveInfo = "UPDATE mediatorsList(mediatorName, email, mediatorPrimaryPhone, mediatorSecondaryPhone, lastMediationDate, availability)
              VALUES('" & MediatorNameTextBox.Text & "','" & EmailTextBox.Text & "','" & MaskedTextBox1.Text & "','" & MaskedTextBox2.Text & "',
            '" & DateTimePicker1.Value & "','" & AvailabilityTextBox.Text & "', WHERE mediatorID = '" & MediatorIDTextBox.Text & "') "
        Catch ex As Exception
        End Try

    Else
        Exit Sub

    End If
    runAccessSQL(saveInfo)

End Sub

There is obviously something I am missing, though I am not sure it is missing from the code. I checked my database fields and set them to string/text fields just to see if I could get it working. At one time, I had two 2 phone number fields that were set to to the wrong data type so you could only enter a number per int32 requirements. I actually had one of these methods working/updating the db several months ago but I can't figure out what happened since. I do know Visual Studio gave me some problems which probably contributed but it's been too long to remember what happened.

I am rather lost on what else to try as this seems like it should work one way or another. Any ideas what to look at and/or try?? Hopefully I can be pointed in the right direction.

Thanks :)

Your update statement is incorrect, the WHERE clause is inside the VALUES() segment, and should be after it.

Try this instead: (Edited)

saveInfo = "UPDATE mediatorsList SET mediatorName='" & _
    MediatorNameTextBox.Text & "', email='" & EmailTextBox.Text & "', .... WHERE " & _
    mediatorID = '" & MediatorIDTextBox.Text & "'"

Also be sure to handle the date correctly. I usually force formatting in yyyy/mmm/dd format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM