With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("datapath") = dpath
.Fields("analysistime") = "atime"
.Fields("reporttime") = "rtime"
.Fields("lastcalib") = "lcalib"
.Fields("analystname") = "aname"
.Fields("reportname") = "rname"
.Fields("batchstate") = "bstate"
.Fields("instrument") = "NA"
.Update ' stores the new record
End With
当我检查数据库时,看起来它只是插入了最后一个字段!
有没有人遇到过这个问题?
答案 0 :(得分:3)
编辑:以下代码有效,但可以在此处找到相关解决方案:Connecting Excel to Mysql Update PROBLEM
它涉及使用文本数据类型的困难。
这似乎有效,但不要问我为什么:
dpath = "a" ''Trim(Range("B2").Text)
atime = "a" ''Trim(Range("B3").Text)
rtime = "a" ''Trim(Range("B4").Text)
lcalib = "a" ''Trim(Range("B5").Text)
aname = "a" ''Trim(Range("B6").Text)
rname = "a" ''Trim(Range("B7").Text)
bstate = "a" ''Trim(Range("B8").Text)
cn.Open strCon
cmd.ActiveConnection = cn
cmd.CommandText = _
"INSERT INTO `batchinfo` (`datapath`, `analysistime`," _
& "`reporttime`,`lastcalib`,`analystname`,`reportname`," _
& "`batchstate`,`instrument`) " _
& "VALUES (?,?,?,?,?,?,?,?);"
cmd.Parameters.Append _
cmd.CreateParameter("datapath", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("analysistime", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("reporttime", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("lastcalib", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("analystname", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("reportname", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("batchstate", adChar, adParamInput, 100)
cmd.Parameters.Append _
cmd.CreateParameter("instrument", adChar, adParamInput, 100)
cmd.Parameters("datapath").Value = "abc"
cmd.Parameters("analysistime").Value = atime
cmd.Parameters("reporttime").Value = rtime
cmd.Parameters("lastcalib").Value = lcalib
cmd.Parameters("analystname").Value = aname
cmd.Parameters("reportname").Value = rname
cmd.Parameters("batchstate").Value = "bstate"
cmd.Parameters("instrument").Value = "NA"
cmd.Execute