i am try to upload Data from excel to ,Mysql but it is not uploading

user231

New Member
Joined
Jan 8, 2015
Messages
7
hey i am trying to upload data from excel to mysql

instead cells issues i have 32 columns in excel sheet and database

i have columns which are starting from 2 row and 1st column




Code:
    Function esc(txt)        'Will replace the 's with MySQL-friendly characters
        esc = Trim(Replace(txt, "'", "\'"))
    End Function
Sub upload_to_database()


'====================================
'= Code made by :                   =
'= Michael Clermont & The Interwebs =
'= 04/04/2011 - Version 1.00        =
'====================================
'INSTRUCTIONS                       =
'==================================================================================================
' 1. Make sure you have Microsoft ActiveX Data Objects Library enabled (Tool > References).       =
' 2. Download the latest MySQL ODBC Driver (Google search).                                       =
' 3. Change the settings below and select the proper driver in this case "MySQL ODBC 5.1 Driver". =
' 4. Copy the required data into your form and press the button.                                  =
'==================================================================================================


'------------------------------------------------------------------------------
'Variable declarations
'------------------------------------------------------------------------------


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 database_table As String
Dim count As Integer
Dim cell As String
Dim cell_2 As String
Dim count_2 As Integer
Dim count_3 As Integer


'------------------------------------------------------------------------------
'Settings
'------------------------------------------------------------------------------


server_name = "192.168.1.200" 'Enter your server IP here - if running from a local computer use 127.0.0.1
database_name = "projects" 'Enter your database name here
user_id = "root" 'Enter your database user here
password = "bmx1" 'Enter your database user password here
database_table = "tblpro" 'Enter database table name


'------------------------------------------------------------------------------
'Initiate database connection
'------------------------------------------------------------------------------


'Specify your driver below


Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 5.2 Unicode Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427" ' Option 16427 = Convert LongLong to Int: This just helps makes sure that large numeric results get properly interpreted
' & ";DSN=" & DSN_
'MsgBox "Server connection OK" 'Debug






'------------------------------------------------------------------------------
'Assign values to global variables
'------------------------------------------------------------------------------


aWidth = WorksheetFunction.CountA(Range("A1:FA1")) 'Finds the width of the table - if you have more columns than that, just extend the range
aHeight = WorksheetFunction.CountA(Range("A1:A65536")) - 1 'Finds the height of the table, minus the field names - if you have more rows than that, just extend the range
count = 0 'Will be used throughout the macro as a counter
count_2 = 0 'Will be used throughout the macro as a counter
count_3 = 0 'Will be used throughout the macro as a counter


'------------------------------------------------------------------------------
'Populate the table row
'------------------------------------------------------------------------------
ReDim array_fields(aWidth)


'This will populate INTO what the VALUES will go for the whole upload
Do Until count = aWidth
    count = count + 1 'Set the count to be used in the array and increment it for the the Do
    cell = Worksheets("Upload").Cells(1, count).Value
    array_fields(count) = cell
Loop


'This is not necessary, but is done for better code comprehension (and to avoid any problems later on)
count = 0 'reset the counter


'------------------------------------------------------------------------------
'Get the data and store it within an array
'------------------------------------------------------------------------------
ReDim array_values(aHeight, aWidth)




count = 2 'set the values
count_2 = 1 'set the values




'This will populate VALUES for the whole upload
Do Until (count - 2) = aHeight


    Do Until (count_2 - 1) = aWidth
        cell = Worksheets("Upload").Cells(count, count_2).Value
        array_values((count - 1), count_2) = cell
        count_2 = count_2 + 1
    Loop
    
    count_2 = 1
    count = count + 1


Loop


'This is not necessary, but is done for better code comprehension (and to avoid any problems later on)
count = 0 'reset the counter
count_2 = 0 'reset the counter
cell = "" 'reset the cell value


'------------------------------------------------------------------------------
'Upload the data
'------------------------------------------------------------------------------


count = 0 'set the values
count_2 = 2 'set the values
count_3 = 1 'set the values
cell = esc(array_fields(1)) 'set the first value of the cell output


'Return the value in fields
Do Until (count + 1) = aWidth
    cell = cell & ", " & array_fields(count_2)
    count = count + 1
    count_2 = count_2 + 1
Loop


'Prevent the whole thing to crash because of '
cell = esc(cell)


'Reset the variables for the next Do
count = 0
count_2 = 1




Do Until count = aHeight


'repopulate the first cell
cell_2 = "'" & esc(array_values(count_2, 1))


    'Populate the cell value
    Do Until count_3 = aWidth
        cell_2 = cell_2 & "'" & ", " & "'" & esc(array_values(count_2, (count_3 + 1)))
        count_3 = count_3 + 1
    Loop
    
    cell_2 = cell_2 & "'" 'add the last ' to the name
    
    'MsgBox (cell) 'debug
    'MsgBox (cell_2) 'debug


    'Run the query
    strSQL = "INSERT INTO " & database_table & " (" & cell & ") VALUES (" & cell_2 & ")"
    'MsgBox (strSQL) 'debug
    conn.Execute (strSQL)  'execute the above query


    'Reset the variables for the loop
    count_2 = count_2 + 1
    count = count + 1
    count_3 = 1
    cell_2 = ""
Loop


'Close the DB connection
conn.Close
Set conn = Nothing


MsgBox ("Upload finished")


'This is not necessary, but is done for better code comprehension (and to avoid any problems later on)
count = 0 'clean up the variables, just in case
count_2 = 0 'clean up the variables, just in case


End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,214,832
Messages
6,121,845
Members
449,051
Latest member
excelquestion515

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