compile error: argument not optional

aysel1989

New Member
Joined
Oct 11, 2017
Messages
11
Hi guys, i may need some help here. I'm new to VBA excel and want to save the data directly into MYSQL database. But i have encounter some compile error with argument not optional. Here's my below codes:

Sub dbConnect() <--- error from here


Dim conn As New ADODB.Connection
Dim iRow As Integer


With Sheets("sheet1")
conn.Open "DRIVER={MySQL ODBC 5.3 ANSI DRIVER};" & _
"SERVER=myserver;" & _
"DATABASE=mydb;" & _
"USER=myuser;" & _
"PASSWORD=mypassword;" & _
"OPTION=3"


iRow = 5
Do Until .Cells(iRow, 1) = ""
Number = .Cells(iRow, 1)
country = .Cells(iRow, 2)
company = .Cells(iRow, 3)
brand = .Cells(iRow, 4)
Period = .Cells(iRow, 5)
Month = .Cells(iRow, 6)
Year = .Cells(iRow, 7)
conn.Execute "insert into table_1(number,country,company,brand,period,month,year)values('" & Number & "','" & country & "','" & company & "','" & brand & "','" & Period & "','" & Month & "','" & Year & "')"
iRow = iRow + 1
Loop
MsgBox "successfully saved into database."
conn.Close
Set conn = Nothing
End With
End Sub


Anyone can tell me what's wrong with my codes? Appreciated, thanks!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top