Question : How do I fix odbc call failed #1062 under access vba with a mysql backend?

I used the following code to programmatically set the record source for an access form.  The record source is an odbc linked table from a mysql backend.  I'm not changing any data at all on the form and when I try to go to the next record, I get "ODBC -- call failed. [MySQL][ODBC 5.1 Driver][mysqld-5.1-37-community] Duplicate entry '23' for key 'PRIMARY' (#1062).  I do not understand this because I'm just trying to go to the next record by clicking on the "Next Record" button, not insert a new record with the same primary key.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
If StaffID > #### Then
    Dim sqlCmd As String
  
    sqlCmd = "select * from notes inner join staff_team using (claim_number)"
    sqlCmd = sqlCmd & " where primarycm = " & StaffID
    sqlCmd = sqlCmd & " or secondarycm = " & StaffID
    sqlCmd = sqlCmd & " or tertiarycm = " & StaffID
    
    Me.RecordSource = "sqlCmd"
Else
    Me.RecordSource = "notes"
End If

Answer : How do I fix odbc call failed #1062 under access vba with a mysql backend?

The easiest way is to create a new query from the database window and set (Query | SQL Specific | Pass-Through). Set the ODBC connection string in the properties, and type the query using MySQL syntax. Such a query can be set as record source for a form, but will be read-only.

Based on your code, you can write any valid SQL into an existing query, including pass-through queries:

    CurrentDb.QueryDefs("MyQuery").SQL = strSQL

Then, set the record source to that query. This is what I meant by “rewrite the QueryDef object's SQL on the fly.”

You can also use DAO or ADO techniques to produce a recordset, using JetSQL or pass-through syntax. It's really just a matter of providing a valid connection string. Some recordsets can be set to the form's recordset property after they have been created.

You seem to prefer ADO; the required properties are

      .Properties("Jet OLEDB:ODBC Pass-Through Statement") = True
      .Properties("Jet OLEDB:Pass Through Query Connect String") = <string>

See also on MSDB: ADO Provider Properties and Settings
http://msdn.microsoft.com/en-us/library/aa140022(office.10).aspx
(search for "pass-through" in your browser, it's a long page)


You cannot directly write native (pass-through) SQL into a form's record source property, as there is no property to specify a connection string.

Good luck!
(°v°)
Random Solutions  
 
programming4us programming4us