Link to home
Start Free TrialLog in
Avatar of aragorn_gr
aragorn_gr

asked on

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

Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

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
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
Avatar of aragorn_gr
aragorn_gr

ASKER

> 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...

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
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.
OK and if you do a File Attach is it read-write?
Cheers, Andrew
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.
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
OK tested with MS SQL Server and worked Ok with a slight modification

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

Cheers, Andrew
Unfortunately it does not work with mysql :-(
ASKER CERTIFIED SOLUTION
Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
No problem