DAO dsnless connection to mysql database opens as readonly

Hi,
I'm trying to add a record with vba to a remote mysql database table using a dsnless dao connection.

Unfortunately I keep getting the "Cannot Update. Database or object is read-only".

I am able to add records using a DSN connection so I think the problem is somewhere in the code I'm using.

Any ideas?
Public Sub testDsnless()
    Dim dbMySQL As Database
    Dim cnMySQL As DAO.Connection
    Dim rsMySQL As DAO.Recordset
    Dim sDSN As String, qrMySQL As String
    
    sDSN = "ODBC;DRIVER={MySQL ODBC 3.51 Driver};" _ 
& " SERVER=www.myserver.com;DATABASE=mydatabase;" _
& " UID=myusername;PWD=mypassword;"
    Set dbMySQL = OpenDatabase("", False, False, sDSN)
    
    qrMySQL = "SELECT * FROM tableName;"
    Set rsMySQL = dbMySQL.OpenRecordset(qrMySQL)
    
    rsMySQL.AddNew
    rsMySQL!ID = 1000
    rsMySQL.Update
End Sub

Open in new window

aragorn_grAsked:
Who is Participating?
 
TextReportConnect With a Mentor Commented:
Do you have a primary key or unique key on the table?
Cheers, Andrew
0
 
TextReportCommented:
Pass through queries are read only, you need to either attache the table to use the AddNew or do an INSERT in the passthrough query.
Cheers, Andrew
0
 
TextReportCommented:
Sorry mis read your question, your not using passthrough's.

Can you confirm if you set sDSN to use the DSN this then works for you.

Cheers, Andrew
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
aragorn_grAuthor Commented:
> Can you confirm if you set sDSN to use the DSN this then works for you.

If I understand correctly, you want me to change line 10 as follows (remove the False at options parameter)

Set dbMySQL = OpenDatabase("", , False, sDSN)

and get the SELECT Data Source message window when i run the code. I get the same error again...

0
 
TextReportCommented:
I am refering to the ODBC connect string, the one you are using is a DSN Less connection as you are specifying the driver to use. A DSN Connection has DSN= in the connect string. Pleas try this instead of the DSN Less option you are using.
Cheers, Andrew
0
 
aragorn_grAuthor Commented:
OK, I've modified sDSN to be:
sDSN = "ODBC;DSN=dsn_name;DRIVER={MySQL ODBC 3.51 Driver};"
and still the same error. The connection is made successfully but I have read only access.
0
 
TextReportCommented:
OK and if you do a File Attach is it read-write?
Cheers, Andrew
0
 
aragorn_grAuthor Commented:
I dont know what a File Attach is or how am I supposed to do one...

I can tell you that if I link to the mysql database tables in an mdb using the same dsn, I have read write access.
0
 
TextReportCommented:
Probably talking about the same thing, File Menu, Get External Data, Link Tables.
This would imply that the attached tables is correctly identifying the Primary Key whereas the OpenDatabase is not.
Not sure what we can do now excepty use the linked table.
Cheers, Andrew
0
 
TextReportCommented:
OK tested with MS SQL Server and worked Ok with a slight modification

    Set rsMySQL = dbMySQL.OpenRecordset(qrMySQL, dbOpenDynaset, dbSeeChanges)

Cheers, Andrew
0
 
aragorn_grAuthor Commented:
Unfortunately it does not work with mysql :-(
0
 
aragorn_grAuthor Commented:
No. I thought i did because the table was exported but seems like it didn't retain the Primary Key... I've set it and everything is fine! I'm accepting your answer with id 20824180 as a solution

Thanks a lot Andrew!
0
 
TextReportCommented:
No problem
0
All Courses

From novice to tech pro — start learning today.