救命!!用db连excel的语句救命!!
急啊,弄了一个下午也没弄出来
小弟只有求助各位高手大哥了
Option Explicit
Dim Rs As ADODB.Recordset
Dim conn1 As ADODB.Connection
'保存文档
Private Sub Command1_Click()
' 设置“CancelError”为 True
dlgFileOpen.CancelError = True
' On Error GoTo ErrHandler
Dim mFileName As String
dlgFileOpen.Filter = "文件(*.xls)|*.xls"
dlgFileOpen.ShowOpen
If dlgFileOpen.FileName = "" Then Exit Sub
mFileName = Trim(dlgFileOpen.FileName)
Call InSert(mFileName)
'ErrHandler:
' ' 用户按了“取消”按钮
' Exit Sub
End Sub
'保存文件到数据库中
Private Sub InSert(cFilePath As String)
Set conn1 = New ADODB.Connection
Set conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Dim cFileName As String '要保存的文档名称
Dim Sql As String
Dim SqlInsert As String
Sql = "Select * From Sheet1$" 'Sheet1$必须是固定的
Set conn1 = New ADODB.Connection
conn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source='" + cFileName + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"
conn1.CursorLocation = adUseClient
conn1.Open
Set Rs = conn1.Execute(Sql)
If Rs.EOF And Rs.BOF Then
MsgBox "选择的Excel表中没有数据!", vbOKOnly + vbExclamation, "系统提示"
Exit Sub
Else
conn.Open publicstr, 1, 3
Do While Not Rs.EOF
'此处插入的值根据实际数据库结构进行调整
SqlInsert = "Insert Into cellinfo (CellName,Area1,Area2) Values ('" & Trim(Rs("客户姓名")) & "','" & Trim(Rs("原面积")) & "','" & Trim(Rs("测绘面积")) & "')"
conn.Execute (SqlInsert) '插入Excel表格内容
Rs.MoveNext
Loop
MsgBox "导入数据成功!", vbOKOnly + vbExclamation, "系统提示"
End If
Rs.Close
conn1.Close
conn.Close
Set Rs = Nothing
End Sub
错误提示:
指定的初始化字符串不符合oledb规定
在线等!!!!!!!!!!