1

I'm trying to insert snippets of a word document into a MySQL database. I did this in Java, but I lost all formatting of the Microsoft document, so I'm now trying to do the same in VBA (I'm hoping that this will keep the formatting?!). But to find out if it will, I need to try.. I've set up the connection and the table in MySQL but I can't insert any values. It throws up a syntax error on the "conn.Execute" line.

Sub ConnectToDataBase()

'
'
 Dim conn As New ADODB.Connection
 Dim Server_Name As String
 Dim Database_Name As String
 Dim User_ID As String
 Dim Password As String
 '
 Dim i As Long ' counter
 Dim SQLStr As String ' SQL to perform various actions
 Dim table1 As String, table2 As String
 Dim field1 As String, field2 As String
 Dim rs As ADODB.Recordset
 Dim vtype As Variant
 '


 Server_Name = "127.0.0.1" ' Enter your server name here -
 Database_Name = "rmp" ' Enter your database name here
 User_ID = "root" ' enter your user ID here
 Password = "Password1" ' Enter your password here

 Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
 & ";SERVER=" & Server_Name _
 & ";DATABASE=" & Database_Name _
 & ";UID=" & User_ID _
 & ";PWD=" & Password _
 & ";OPTION=16427" ' Option 16427 = Convert LongLong to Int: 

 strSQL = "INSERT INTO  parts(idParts, Part 1, Part 2, Part 3, Part 4, 
 Part 5) " & _
            "VALUES (' " & 2 & " ' , '" & one & "' , '" & two & "' , '" &  
  three & "' , '" & four & "' , '" & five & "' )"

conn.Execute strSQL


Close connections
On Error Resume Next
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
On Error GoTo 0

End Sub
4
  • How are you trying to preserve the formatting? Commented Aug 5, 2015 at 13:08
  • Well I need the text to be in the same format as it was in the Word Document (looking at Bold, Underlined, Tables etc) and I'm hoping VBA will do this automatically? Commented Aug 5, 2015 at 13:20
  • Not automatically. You would need to read the formatting codes/html and store them as part of the text. Commented Aug 5, 2015 at 13:31
  • Okay, I'll try that. Thank you :) Commented Aug 5, 2015 at 13:47

1 Answer 1

1

The .Net connector uses UID but the ODBC connector used by VBA uses User. I've only ever used Option=3 as well.

conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
    & ";SERVER=" & Server_Name _
    & ";DATABASE=" & Database_Name _
    & ";User=" & User_ID _
    & ";PWD=" & Password _
    & ";OPTION=16427" ' Option 16427 = Convert LongLong to Int:

You can test the connection by running a simple sql query.

strSQL = "SELECT * FROM parts;"
conn.Execute strSQL

If this throws an error, you still have an issue.

Also, in your code you have spaces either side of the 2. Is that correct?

"VALUES (' " & 2 & " ' ,

Sign up to request clarification or add additional context in comments.

3 Comments

I've taken it out and VBA still doesn't like it. Shouldn't it be easy to just send a statement into MySQL using ADODB?
What do you mean by "taken it out"? I've added code to my answer.
As in I removed the Spaces either side of 2 and 2 itself ( and the obviously the idparts part further up). And thank you for the code, and forgive me if I'm wrong, but it doesn't seem any different from mine? (Other than UID which I will try)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.