求救,insert into 插入access数据库出错。
问题是如下程序在我电脑和公司内若干电脑运行没有任何错误,安装到客户机器上就出错,出现如下提示:更新失败,数据被锁定。而且每次出错被锁定的表都不一样。我曾怀疑是客户电脑上装了access2003,而我的是access2000,但是我在公司内的电脑上安装access2003,运行时没有出错。哎,水土不服。
因为表结构有改变,所以只能复制库内的数据。不可以直接复制数据库。
Count1 = InStrRev(App.Path, "\ ")
Str = Left(App.Path, Count1)
CD1.InitDir = Str
CD1.Filter = "mdb文件(*.mdb)|*.mdb "
CD1.ShowOpen
Str = CD1.filename
Cn1.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=jinke303;Data Source= " & App.Path & "\db.mdb; "
Cn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=jinke303;Data Source= " & Str & "; "
Set Rs = Cn.OpenSchema(adSchemaTables)
Do Until Rs.EOF
If Rs( "TABLE_TYPE ") = "TABLE " Then
'Debug.Print Rs(Count1).Name & "-> " & Rs.Fields(Count1).Value
Str = Rs( "TABLE_NAME ")
With Cmd1
.ActiveConnection = Cn1
.CommandType = adCmdText
.CommandTimeout = 60
.CommandText = "delete from " & Str
.Execute
End With
My_Delay (500)
Set Cmd1 = Nothing
My_Delay (500)
With Cmd
.ActiveConnection = Cn
.CommandType = adCmdText
.CommandTimeout = 0
.CommandText = "insert into " & " [;Database= " & App.Path & "\db.mdb;Pwd=jinke303;]. " & Str & " select * from " & Str
.Execute
End With
My_Delay (500)
Set Cmd = Nothing
End If
Rs.MoveNext
Loop
Rs.Close
Set Cmd = Nothing
Set Cmd1 = Nothing
Cn.Close
Set Cn = Nothing
Rs.Close
Set Rs = Nothing
Set Cmd1 = Nothing
Cn1.Close
Set Cn1 = Nothing
Screen.MousePointer = 1
MsgBox "导入数据成功! ", vbInformation, "提示 "
Exit Sub
Err:
MsgBox Str & " 表导入数据出错! ", vbInformation, "提示 "
If IsNull(Cn.Errors.Count) Then
Str = Cn1.Errors.Item(0)
Else
Str = Cn.Errors.Item(0)
End If
MsgBox "导入数据出错,请重试! " & Str, vbInformation, "提示 "
If Cn.State = 1 Then Cn.Close
If Cn1.State = 1 Then Cn1.Close
If Rs.State = 1 Then Rs.Close
Set Rs = Nothing
Set Cmd = Nothing
Set Cmd1 = Nothing
Screen.MousePointer = 1
[解决办法]
运行环境一样吗?很可能是文件数据文件属性是只读,或者所在的目录有权限锁定之类的
[解决办法]
数据库文件属性是只读?
[解决办法]
晕了,怎么会这样?安装之后数据库文件属性只读??表的权限只读??要么就是操作表时,有该表已经被另一个程序使用了??还能使其他的??不能吧。逐个检查一下。
[解决办法]
文件夹的权限?
[解决办法]
为什么不用代码直接修改表结构,反而用这样方式来导数据呢?