data from Excel into SQL Server 2008 via VBA

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I currently have some code in an Excel 2007 workbook which takes data
out of a worksheet and creates records in two different Access 2007
tables.

I would like to place the data into two SQL Server 2008 tables instead
and am unsure on how to change the code.

SQL Server name is: MFGDATA/SCADASQL
Database name is: "Batch Raw Log"
Two table names are: "rawlog" and "mdiadd"


Current Code:

Dim db As Database, rs As Recordset, r As Long, s As Long
Set db = OpenDatabase("\\Dcc-1\dcc scada\Databases\Batch Raw
Log.accdb")
' open the database
Set rs = db.OpenRecordset("rawlog", dbOpenTable)
' all records in a table
s = 5
Do While Len(Range("C" & s).Formula) > 0
r = s ' the start row in the given raw material
Do While Len(Range("C" & r).Formula) > 0
' repeat until first empty cell in column C5+x
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("batdate") = Range("B5").Value
.Fields("prod") = Range("E2").Value
.Fields("prodlot") = Range("H2").Value
.Fields("reactor") = Range("N2").Value
.Fields("rawnum") = Range("C" & r).Value
.Fields("begin") = Range("E" & r).Value
.Fields("end") = Range("F" & r).Value
.Fields("rawlot") = Range("G" & r).Value
.Fields("rawwt") = Range("H" & r).Value
.Fields("oper1") = Range("K" & r).Value
.Fields("oper2") = Range("L" & r).Value
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
s = s + 4 ' next raw
Loop
rs.Close
Set rs = Nothing

Set rs = db.OpenRecordset("mdiadd", dbOpenTable)
' all records in a table
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("batdate") = Range("B5").Value
.Fields("prod") = Range("E2").Value
.Fields("prodlot") = Range("H2").Value
.Fields("reactor") = Range("N2").Value
.Fields("mdidate") = Range("B60").Value
.Fields("mditime") = Range("C60").Value
.Fields("mdisec") = Range("E60").Value
.Update ' stores the new record
End With
rs.Close
Set rs = Nothing
db.Close


Any help would be appreciated.
.



Relevant Pages

  • Re: Update to Access Multiple tables via VBA
    ... Dim db As Database, rs As Recordset, r As Long, ur As Long ... ..AddNew ' create a new record ... .Update ' stores the new record ... tbl_One is one Many with tbl_Three via Foreign Key ...
    (microsoft.public.excel.programming)
  • Re: data from Excel into SQL Server 2008 via VBA
    ... SQL Server name is: MFGDATA/SCADASQL ... Database name is: "Batch Raw Log" ... .Update ' stores the new record ...
    (microsoft.public.sqlserver.programming)
  • Re: Need Advice on Linux vs. Windows for a Database-Driven Web Application
    ... We recently hired sales reps in several cities in the US ... > and Canada to maintain our displays in stores and to take orders from ... > in a database that we could access from the Head Office. ... No question - the Linux based one. ...
    (comp.programming)
  • Re: Max Exchage DB size in SBS
    ... private) rather than total. ... The stores grow as necessary to house their content, ... the database will grow by 100mb. ... becomes "slack" in the database. ...
    (microsoft.public.backoffice.smallbiz2000)
  • Re: AW: mklvcopy 0516-622
    ... before rebuilding and re-importing the database I blasted /dev/zero all over the raw volumes (everything continued to work fine after recreating the ... I have now added 2 new D40 drawers to the system, and built RAID 0 arrays within each drawer. ...
    (AIX-L)