Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Keeping correct time format when uploading Excel data to Mysql database

I have the excel data in the following format:
Excel Data Laout

When I click the UploadData button this data is uploaded into my connecting SQL database. This works when only uploading integers and strings, when I try to push dates to a database I get the following error:
Error

When running the VBA code it appears to be ignoring the custom date format ("yyyy-mm-dd") that I apply the Date column in the Excel sheet so that it is in the correct format. How can I edit my VBA code so that the Date column data is in the "yyyy-mm-dd" format, it should have to do with the "query" variable in the For loop in my code I think, so that when it runs the query it sets the value of Date1 to the proper time format. NOTE: Under the query variable I included a formula I used in the excel sheet which allowed me to pull the Date value in the correct format, although I couldn’t declare this properly in the VBA without getting errors

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sub UploadGsaData()
' Create the required variables
Dim query As String 'Variable for SQL query
Dim Date1 As String
Dim Address1 As String
Dim Mobile1 As Integer
Dim Salary1 As Integer
Dim TRow As Long
Dim CRow As Long



        ' Do not update the cells on the current sheet, this is performed after the SQL query
         Application.ScreenUpdating = False

         ' Unprotect sheet to allow SQL changes to be made
         ActiveSheet.Unprotect
         
         ' Send the ending row value
         TRow = 7

         ' Inititate For Loop to iterate table of data
         For CRow = 3 To TRow
                
                 Date1 = Sheets("Sheet1").Range("B" & CRow).Value
                 Address1 = Sheets("Sheet1").Range("C" & CRow).Value
                 Mobile1 = Sheets("Sheet1").Range("D" & CRow).Value
                 Salary1 = Sheets("Sheet1").Range("E" & CRow).Value

                 query = "REPLACE INTO testdata(Date, Address, Mobile, Salary) VALUES('" & Date1 & "', '" & Address1 & "', '" & Mobile1 & "', '" & Salary1 & "')"
                 '="REPLACE INTO operations_logbook(date_time, category, description, logged_by) VALUES('"&TEXT(B3,"yyyy-mm-dd")&"', '"&D6&"', '"&D10&"', '"&D8&"')"
         
                 ' Call mysql query function to run query to upload log to database ( called a different name as different driver)
                 Call server_2_query_mysql_database(server2, database1, query, ActiveSheet.Range("A1"))
         
          Next
          
          

         ' Reapply the Sheet protection now that the query is complete
         ActiveSheet.Protect

         ' Update the Excel sheet so that any new data is updated
         Application.ScreenUpdating = True


End Sub

>Solution :

Change this …

Date1 = Sheets("Sheet1").Range("B" & CRow).Value

… to this …

Date1 = Sheets("Sheet1").Range("B" & CRow).Text
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading