Solved

Macro to insert in SQL 2008

Posted on 2016-07-14
11
32 Views
Last Modified: 2016-07-16
Hello,
can you please help.
Attached is a sample data.
I need to insert a note to each order into my SQL database.
need to find the highest NoteNumber, then add 1.

Option Explicit
Sub Insert_Notes()
Dim sh As Worksheet
Dim cnn As adodb.Connection
Set sh = Sheets("Import")
Dim rs As New adodb.Recordset
Dim sql As String
Set cnn = New adodb.Connection

cnn.Open "Provider=SQLOLEDB.1;User ID=sa; password=xxxx;Initial Catalog=xxxx;Data Source=111.111.111.111;"

SET IDENTITY_INSERT ActiverNotes ON
INSERT INTO ActiveNotes (NoteNumber,OrderNo,NoteText,EnteredBy,EnteredOnDate,IsPublicNote,OrderNoteTypeID) VALUES (MAX(NoteNumber)+1,Order Numbers from Column "AL",'Tracking from Column "AK"','System','Date from Column "AM"',4)
SET IDENTITY_INSERT ActiveNotes OFF
Commit
End Sub

Your help is very much Appreciated.
Book1.xls
0
Comment
Question by:Wass_QA
  • 6
  • 5
11 Comments
 
LVL 42

Expert Comment

by:EugeneZ
ID: 41711483
if your NoteNumber is auto-incremental - it will be populated for each insert

if it is not you need - please clarify


Sub Insert_Notes()
Dim sh As Worksheet
Dim cnn As adodb.Connection
Set sh = Sheets("Import")
Dim rs As New adodb.Recordset
Dim sql As String
Set cnn = New adodb.Connection

cnn.Open "Provider=SQLOLEDB.1;User ID=sa; password=xxxx;Initial Catalog=xxxx;Data Source=111.111.111.111;"


INSERT INTO ActiveNotes (OrderNo,NoteText,EnteredBy,EnteredOnDate,IsPublicNote,OrderNoteTypeID) 
VALUES ( 'Order Numbers', 'from Column "AL"','Tracking from Column "AK"','System','Date from Column "AM"',4)

Commit
End Sub

Open in new window

0
 

Author Comment

by:Wass_QA
ID: 41711487
Hello,
I need to insert the next note Number.
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 41711499
if your ActiveNotes  table's column NoteNumber is set as the identity column <=> auto incremental
you'll get the next number

---

if it is not  or you have some specific  logic (it is just an idea)
you can try this not

SET IDENTITY_INSERT ActiverNotes ON
INSERT INTO ActiveNotes (NoteNumber,OrderNo,NoteText,EnteredBy,EnteredOnDate,IsPublicNote,OrderNoteTypeID) 
Select (select MAX(NoteNumber)+1 from ActiveNotes ) as MAX_NoteNumber,'Order Numbers ','from Column "AL"','Tracking from Column "AK"','System','Date from Column "AM"',4
SET IDENTITY_INSERT ActiveNotes OFF

Open in new window

0
 

Author Comment

by:Wass_QA
ID: 41711506
Ho w do I insert the orders one after another?
Dim i as Integer,
next i,...

I wanted it to go through the orders one after another till last raw in column?
Thanks
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 41711512
did you review the posted above code?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Wass_QA
ID: 41711520
HI EugeneZ
What I meant
'Order Numbers ','from Column "AL"',
'Tracking from Column "AK"','
'Date from Column "AM"'

How do I get these into my Insert?
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 41711536
so far
we know from you code

that table  ActiveNotes
has columns
NoteNumber,OrderNo,NoteText,EnteredBy,EnteredOnDate,IsPublicNote,OrderNoteTypeID

can you tell more about
'Order Numbers ','from Column "AL"',
'Tracking from Column "AK"','
'Date from Column "AM"'?

for example from what table is AL column?

what does it mean 'Order Numbers ','from Column "AL"',?
can you illustrate?
0
 

Author Comment

by:Wass_QA
ID: 41711547
In My original question, I attached  a sample excel.
in Column AL , I have my order numbers
Column AK, I Have the NoteText
Column AM, I have the Date

I Get the excel sheet daily with Different order numbers (Column AL), notes (Column AK), dates (cOLUMN AM).

I need to insert into the database.

I'll  put the values into Column AL, AK, AM.

so,
Insert into My database the value for Note, date,.. for orderNumber in Raw 2
then, Raw 3, Then Raw 4,.... till last.

Thanks,
0
 
LVL 42

Accepted Solution

by:
EugeneZ earned 500 total points
ID: 41711553
I see
in order to import data from excel
using vbscript you can use this code as an example from http://tomaslind.net/2013/12/26/export-data-excel-to-sql-server/

Sub Button1_Click()
     
    Dim conn As New ADODB.Connection
    Dim iRowNo As Integer
    Dim sCustomerId, sFirstName, sLastName As String
  
    With Sheets("Sheet1")
            
        'Open a connection to SQL Server
        conn.Open "Provider=SQLOLEDB;Data Source=ASUSBOOK\SQL2012;Initial Catalog=ExcelDemo;Integrated Security=SSPI;"
            
        'Skip the header row
        iRowNo = 2
            
        'Loop until empty cell in CustomerId
        Do Until .Cells(iRowNo, 1) = ""
            sCustomerId = .Cells(iRowNo, 1)
            sFirstName = .Cells(iRowNo, 2)
            sLastName = .Cells(iRowNo, 3)
                
            'Generate and execute sql statement to import the excel rows to SQL Server table
            conn.Execute "insert into dbo.Customers (CustomerId, FirstName, LastName) values ('" & sCustomerId & "', '" & sFirstName & "', '" & sLastName & "')"
 
            iRowNo = iRowNo + 1
        Loop
            
        MsgBox "Customers imported."
            
        conn.Close
        Set conn = Nothing
             
    End With
 
End Sub

Open in new window

0
 

Author Comment

by:Wass_QA
ID: 41711612
EugeneZ,
Thank you very much,
This worked.
0
 

Author Closing Comment

by:Wass_QA
ID: 41714885
Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now