Colin Stadig
Colin Stadig

Reputation: 245

MySQL ODBC Connector w/ Excel VBA - Can't DELETE Records

Why would one be able to connect to a MySQL database with excel VBA (via ODBC connector as seen in the public function i have shown below) to INSERT records as well as for SELECT statements but receive an error statement when I try and DELETE records? When I connect with Root account I still have the same issues. I am new to MySQL and am using the Sakila database that comes when you install the program.

What could I possibly be missing?

See below for my functions that I use to connect to the database as well as a sample of code that works for INSERT and SELECT statements and then the code that is having an error with my DELETE statement.

REVISION...

Turns out this was just embarrassingly an error in my MySQL statement as pointed out in the accepted answer. However, for those who stumble across this in the future who want to connect to MySQL through Excel-VBA, you may find merit in the connectDatabase() and closeDatabase() functions as most code I've stumbled upon with the ODBC Connector has users creating their connection in a less clean format.

Public DBCONT As Object

Public Function connectDatabase()
    Set DBCONT = CreateObject("ADODB.Connection")   
    Dim Server_Name As String
    Dim Database_Name As String
    Dim User_ID As String
    Dim Password As String
    Dim sConn As String

    Server_Name = "localhost"
    Database_Name = "sakila"
    User_ID = "admin"
    Password = "password"

    sConn = "Driver={MySQL ODBC 5.1 Driver};Server=" & _
            Server_Name & ";Database=" & Database_Name & _
            ";UID=" & User_ID & ";PWD=" & Password & ";Option=3;"

    DBCONT.Open sConn
    DBCONT.cursorlocation = 3

End Function



Public Function closeDatabase()
    On Error Resume Next 
    DBCONT.Close
    Set DBCONT = Nothing
    On Error GoTo 0
End Function

The following code works:

Private Sub Workbook_Open()

    Dim rs As Object
    Set rs = CreateObject("ADODB.Recordset")    
    Dim sqlstr As String
    sqlstr = "SELECT * FROM Actor"

    Call connectDatabase
    rs.Open sqlstr, DBCONT

    Dim actorfirstname As String
    Dim actorlastname As String
    Dim mysqlinsert As String
    actorfirstname = InputBox("Enter first name")
    actorlastname = InputBox("Enter last name")
    mysqlinsert = "INSERT INTO Actor(first_name,last_name) VALUES ('" & actorfirstname & "','" & actorlastname & "')"
    DBCONT.Execute mysqlinsert

    If rs.RecordCount > 0 Then
      For i = 1 To rs.RecordCount
        For x = 1 To rs.Fields.Count
            If i = 1 Then ThisWorkbook.Sheets("Sheet1").Cells(1, x).Value = rs.Fields(x - 1).Name
            With ThisWorkbook.Sheets("Sheet1")
                .Cells(i + 1, x).Value = rs(x - 1)
            End With
        Next x
        rs.movenext
      Next i
    Else
      MsgBox "No Records Found"
    End If
    rs.Close
    Set rs = Nothing
    Call closeDatabase

End Sub    

The following codes does not work... it errors out on the line DBCONT.Execute sqlstrDelete with a Run-time error '-2147217900 (80040e14)': Automation Error

Sub deleteRecord()
    Dim sqlstrDelete As String   
    sqlstrDelete = "DELETE * FROM Actor WHERE last_name='TEST'" 

    Call connectDatabase
    DBCONT.Execute sqlstrDelete
    Call closeDatabase
 End Sub

Upvotes: 0

Views: 587

Answers (1)

whytheq
whytheq

Reputation: 35605

Do you need a star? *

DELETE * FROM Actor WHERE last_name='TEST'

Will this execute?

DELETE FROM Actor WHERE last_name='TEST'

Upvotes: 1

Related Questions