+ Reply to Thread
Results 1 to 1 of 1

Need Help: Transfer data from Excel to SQL Table date issue

  1. #1
    Registered User
    Join Date
    01-12-2017
    Location
    dublin, ireland
    MS-Off Ver
    2010
    Posts
    1

    Need Help: Transfer data from Excel to SQL Table date issue

    Hi All,

    I need some help from you guys to sort this issue out:
    I'm trying to transfer data from my excel sheet to my SQL table using VBA with the loop in it which gets triggered by hitting SAVE button, my sheet looks as follows:

    Capture1.PNG

    Idea is to consolidate the following under just 3 Columns in my SQL table: fact.Holiday which consists of following columns: DATE, DAY and NAME

    In order to transfer the data I have added a VBA code with the loop as I don't know how many names will be there so it has to loop until the last row in a **** and then copy everything from the next column until it's blank the following code been added under ThisWorkbook:
    ************************************************************
    Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim conn As New ADODB.Connection
    Dim iRowNo, ColNo As Integer
    'Dim sDate As Date, sDay As String, sName As String

    With Sheets("Schedule")

    'Open a connection to SQL Server
    conn.Open "Provider=SQLOLEDB;Data Source=myTable;Initial Catalog=Harmony;Integrated Security=SSPI;"

    conn.Execute "DELETE FROM fact.holiday"
    'Skip the header row
    iColNo = 2

    'Loop until empty cell in CustomerId
    Do Until .Cells(1, iColNo) = ""
    iRowNo = 2
    Do Until .Cells(iRowNo, 1) = ""
    sDate = .Cells(iRowNo, 1)
    sDay = .Cells(iRowNo, iColNo)
    sName = .Cells(iRowNo, iColNo + 1)

    'Generate and execute sql statement to import the excel rows to SQL Server table
    conn.Execute "insert into harmony.fact.holiday ([Date],[Day],[Name]) values ('" & sDate & "', '" & sDay & "', '" & sName & "')"
    iRowNo = iRowNo + 1

    Loop
    iColNo = iColNo + 2
    'MsgBox iColNo
    'MsgBox .Cells(iRowNo, 1)

    Loop
    MsgBox "Transfer completed"
    conn.Close
    Set conn = Nothing

    End With

    End Sub
    *****************************************************************

    The problem that I have is that it does not transfer beyond 12 January 2017 ( seems like VBA treating 12 as last month of the year and just stops there, but if I change my dates and instead of 13 January I will have any date bellow 12 it will work just fine so don't know where i'm getting it wrong =o? as it stands it transfers only the following:
    Capture2.PNG

    I'm also getting an error in excel when I'm executing the following code

    Capture.PNG


    Any help or advice very much appreciated.


    Thank you


    Ali
    Last edited by abduvali2017; 08-21-2017 at 11:50 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] data transfer code issue
    By mojobaabby in forum Excel General
    Replies: 4
    Last Post: 09-23-2016, 07:46 PM
  2. insert data into oracle table date format issue
    By Khaled Diab in forum Access Tables & Databases
    Replies: 4
    Last Post: 02-03-2015, 05:44 AM
  3. Replies: 0
    Last Post: 05-16-2014, 11:07 AM
  4. Pivot table date format issue when drop down run on VBA - Excel 2003
    By NotBright in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-14-2014, 06:28 AM
  5. VBA to transfer Excel data to an Access table
    By beckajayne in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-18-2011, 03:00 PM
  6. excel transfer data to access table
    By nominoo in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-26-2010, 02:30 PM
  7. Replies: 0
    Last Post: 07-07-2005, 12:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1