Skip to content

Instantly share code, notes, and snippets.

@cofearabi
Created June 23, 2013 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cofearabi/5845135 to your computer and use it in GitHub Desktop.
Save cofearabi/5845135 to your computer and use it in GitHub Desktop.
VBA Macro which gets the Excel data and put it into MySQL database.
Sub send_mysql()
Dim int_sheets_no
int_sheets_no = 1
int_line = 2
Do While Worksheets(int_sheets_no).Range("E" & int_line).Value <> ""
str_name = Worksheets(int_sheets_no).Range("B" & int_line).Value
str_zip = Worksheets(int_sheets_no).Range("C" & int_line).Value
str_address = Worksheets(int_sheets_no).Range("D" & int_line).Value
str_tel = Worksheets(int_sheets_no).Range("E" & int_line).Value
str_fax = Worksheets(int_sheets_no).Range("F" & int_line).Value
Call add_mysql(str_name, str_zip, str_address, str_tel, str_fax)
int_line = int_line + 1
Loop
Set xlbook = Nothing
Set xlApp = Nothing
End Sub
Function add_mysql(str_name, str_zip, str_address, str_tel, str_fax)
Set conn_1 = CreateObject("ADODB.Connection")
Set rs_1 = CreateObject("ADODB.Recordset")
conn_1.Open "Driver={MySQL ODBC 5.2w Driver};server=192.168.2.100;" & _
"database=user; uid=user; pwd=user;"
intCount = 0
rs_1.Open "select * from tel where tel like '" & str_tel & "'", conn_1, 3, 3
If rs_1.EOF <> True And rs_1.BOF <> True Then
'MsgBox "already exists " & vbCrLf & str_tel
Else
rs_1.addnew
rs_1.Fields("name") = str_name
rs_1.Fields("zip") = str_zip
rs_1.Fields("address") = str_address
rs_1.Fields("tel") = str_tel
rs_1.Fields("fax") = str_fax
rs_1.Update
rs_1.Close
MsgBox "add " & vbCrLf & str_tel
End If
conn_1.Close
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment