Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
dotinf
Starting Member
6 Posts |
Posted - 2008-02-22 : 20:02:23
|
Hello, I am haing a little trouble with send cell data from an Excel sheet to SQLEXPRESS and I'm sure that it is not that hard. Has anybody else come accross this. I have the code below, but it is not complete as I'm not sure what to do to complete it:Private Sub cmdSend_Click()Dim cn As New ADODB.ConnectionDim rs As New ADODB.RecordsetDim strConn As StringstrConn = "PROVIDER=SQLOLEDB;"strConn = strConn & "DATA SOURCE=(local)\SQLEXPRESS;INITIAL CATALOG=test;"strConn = strConn & " INTEGRATED SECURITY=sspi;"cn.ConnectionString = strConncn.Open Set rs = New RecordsetWith rs ' Assign the Connection object. .ActiveConnection = cn ' Extract the required records. .Open "UPDATE tblData Set ID = ID, Name = Name" ' Copy the records into cell A1 on Sheet1. Sheet1.Range("A2, B2").CopyFromRecordset rs ' Tidy up .CloseEnd With cn.Close End Sub I know that it is not correct, but I am trying to convert a recordset query from SQL to Excel to Update Sql from Excel.The cells from A2 down have an ID and cells from B2 down have a name.Thanks for your time |
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-02-23 : 14:23:43
|
You don't need to pull the data into excel that way...you can use MS Query..but if you instead on that method....you have to assign something to the recordset to begin with via a SQL StatementPrivate Sub cmdSend_Click()Dim cn As New ADODB.ConnectionDim rs As New ADODB.RecordsetDim SQL as stringDim strConn As StringDim x as integerDim ws as WorksheetSet ws = ActiveWorksheet 'or Sheets("Sheet1") x = 2 'set the initial row numberstrConn = "PROVIDER=SQLOLEDB;DATA SOURCE=(local)\SQLEXPRESS;INITIAL CATALOG=test;INTEGRATED SECURITY=sspi;"SQL = "Select ID, Name FROM Table order by ID"cn.ConnectionString = strConncn.OpenSet rs = New Recordsetrs.Open SQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdTextDo While not rs.EOF With ws .Range(cells(x,1),cells(x,2) = Array(rs![ID],rs![Name]) End with x = x+1Loopcn.CloseSet rs = NOTHINGset cn = NOTHINGEnd SubEDIT: Your last statement makes no sense...are you trying to get the data from SQL into excel, or from excel into SQL Poor planning on your part does not constitute an emergency on my part. |
|
|
|
|
|
|
|