WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Excel
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Update Excel Cells

Keywords: 	  update excel cells

Question:

i've update sqlexcel to update a cell in excel, but i received an error in sql EXECUTE statement.
file = "D:\TEMP\FICH.XLS" ; change to suit your environment
cConn = "Provider=MicroSoft.Jet.OLEDB.4.0; Data Source=%file%;Extended Properties=Excel 8.0"
DB = ObjectOpen("ADODB.Connection")
DB.Open(cConn)

cSql = "UPDATE [SH1$] SET COMENT='HELLO', WHERE CODE='0005';"
RS = DB.Execute(cSQL) 
DB.Close()
ObjectClose(DB)
Exit 

Answer:

If you want to perform UPDATES, you have to adjust the way the connection or Recordset is opened.

I am able to treat the Excel Sheet as a Recordset Object. This is very simple, for data retrieval only, but the syntax of using [TabName$] is what is required.

adOpenDynamic    = 2
adOpenKeySet     = 1
adLockOptimistic = 3
adLockBatchOptimistic = 4
adCmdTable       = 2
adCmdTableDirect = 512
adCmdText        = 1

file  = "E:\WBDEMO\RAO.XLS"  ; change to suit your environment
cConn = "Provider=MicroSoft.Jet.OLEDB.4.0; Data Source=%file%;Extended Properties=Excel 8.0"
DB    = ObjectOpen("ADODB.Connection")
RS    = ObjectOpen("ADODB.Recordset")
DB.Open(cConn)
RS.Open("RAO$",DB,adOpenKeySet,adLockOptimistic,adCmdTableDirect)

x = 0
RS.MoveFirst()
While ! RS.eof()
   fld  = RS.Fields("BRAO")
   fld1 = RS.Fields("OCN")
   If StrSub( fld.Value,1,1) == "B"
      fld1.Value = 9999
      x = x+1
   Endif
   RS.MoveNext()
Endwhile

DB.Close()
ObjectClose(DB)
ObjectClose(RS)
message("Number of OCN Values Changed",x)
Exit


Article ID:   W15261
File Created: 2002:09:05:13:50:52
Last Updated: 2002:09:05:13:50:52