当前位置:编程学习 > VB >>

VBA向数据库中插入记录问题,高手进

代码如下 :

Dim i As Integer, j As Integer, sht As Worksheet 

Dim cn As New ADODB.Connection 

Dim rs As New ADODB.Recordset  

Dim strCn As String, strSQL As String 

strCn = "Provider=sqloledb;Server=49.140.25.168;Database=BUDGET;Uid=sa;Pwd=1qaz@WSX;"    

cn.Open strCn   

Set sht = ThisWorkbook.Worksheets("简历") 

    strSQL = "INSERT INTO [BUDGET].[dbo].[resumeinfo] ([id],[name],[location])  Values ('" + sht.Cells(2, 4) + "','" + sht.Cells(2, 2) + "','" + sht.Cells(3, 4)+ "')"

cn.Execute strSQL  
cn.Close  
MsgBox ("完成")
End Sub

运行后提示:运行时错误13  类型不匹配。
是strsql字符类型的问题,为什么会这样?如何解决? --------------------编程问答--------------------  strSQL = "INSERT INTO [BUDGET].[dbo].[resumeinfo] ([id],[name],[location])  Values (" + sht.Cells(2, 4) + ",'" + sht.Cells(2, 2) + "','" + sht.Cells(3, 4)+ "')"

第一个字段应该不需要单引号

--------------------编程问答-------------------- 检查sht.cells单元格是否有数值 --------------------编程问答-------------------- 学习, --------------------编程问答-------------------- 1 加一句 Debug.Print strSQL 输出 strSQL 的值,看看实际内容是什么。

2 最好用字符串连接符 & 取代 + 号。
补充:VB ,  VBA
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,