-
Uploading Excel Cells/Ranges as data into a Database
Hi Meir,
Not sure if someone had already helped you, but I figured I'll add my 5 cents worth, since I am a regular user of this method on Excel 2003 and 2007:
PS: This is code copied from an Excel Workbook that I use daily to upload Currency Rates of Exchange into a SQL Server 2008 Database for myself, I will try indicate where you could change minor details to facilitate your own Excel File and Database situation...
[VBA]
Sub UploadROE()
' This function will create a Recordset from the Excel Sheet
' and then upload the data into the tblROE SQL Server 2008 Table
Dim Conn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Set Conn = New ADODB.Connection
Dim Tbl As New ADODB.Recordset
Dim Start, Finish As Long
Dim DT As Date ' Date extracted from Excel Cells on Column B
Dim Curr As String ' String[3] of Currency extracted from Column C
Dim Vall As Double ' ROE Value extracted from Column D
Dim intRow As Integer
' OPEN DATABASE - SQL SERVER 2008
Conn.Open "Provider=SQLNCLI10.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=SHIPPING004\SQLEXPRESS;Database=BrolazShipping"
' OPEN RATES OF EXCHANGE TABLE
Tbl.Open "Select * from tblROE WHERE ROE_DATE = '" & DateToday & "'", Conn, adOpenDynamic, adLockOptimistic
' Now we will loop through the Excel rows and individually assign the variable values
intRow = 2 ' Row 1 contains all the HEADER Column Names
Do Until (intRow = 45)
DT = Cells(intRow, "B")
Curr = Cells(intRow, "G") ' Column "G" has the actual values we use, not column "C" as previously used
Vall = Cells(intRow, "D")
' THEN we will insert those values into the SQL Server Table
Tbl.AddNew
Tbl.Fields(1).Value = DT
Tbl.Fields(2).Value = Curr
Tbl.Fields(3).Value = Vall
Tbl.Update
Cells(2, "I") = "Added Currency: " & Curr & " @ " & Vall
' increment Counter intRow
intRow = intRow + 1
Loop
' LOOP COMPLETE
' If we reached this section the Update was successfull
Cells(2, "I") = "Updated Rates of Exchange for " & DT & " successfully! ROE's affected = " & intRow
Conn.Close
Set Conn = Nothing
End Sub[/VBA]
Notes:
1. Simply replace the connection string/method with one that supports your Database platform (like Excel itself, MS Access or what you prefer)
2. Simply rename the Recordset(s) with names that make more sense for your own personal use
3. Important - If you are using a Microsoft Database, please go to (menu) Tools | References... and select the Microsoft ActiveX Data Object (version 2.8 in Office 2007) by checking the box left of it. This enables you to access and use ADODB Methods and Properties.
4. This post assumes some familiarity with SQL Syntax
5. My objects, such "tblROE" are specific table names within the scope of my Datbase Platform (SQL Server Express 2008) and need to be replaced with your own Database's table name(s)
Holler of you need more info/assistance.
Adios
Deyken
DeezineTek
South Africa

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules