一个有关ADO的简单代码,请大家帮我修改一下,谢谢!
option explicit
dim conn as new adodb.connection
dim rs as new adodb.recordset
dim sql as string
private sub form_load()
dim connstr as string
connstr = "provider=microsoft.jet.oledb.4.0;data source=d:\mis\data.mdb"
conn.open connstr
rs.cursorlocation = aduseclient
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd"
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
private sub cmdfind_click()
if txtname.text="" then
msgbox "请输入所要查询的名字。",16,"提示"
exit sub
else
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd where 姓名=" & txtname.text & ""
end if
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
当我运行后点击cmdfind按钮时,系统提示
----------------------
实时错误3750:
对象打开时操作不被允许。
----------------------
应当是最后两行有问题,请教如何进行修改呢?
推荐阅读
private sub cmdfind_click()
if txtname.text="" then
msgbox "请输入所要查询的名字。",16,"提示"
exit sub
else
set rs=nothing
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd where 姓名=" & txtname.text & ""
end if
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
在每个rs.open .....这句之前加一句:if rs.state=adstateopen then rs.close
你在form_load已经打开过rs,在cmdfind_click中没关闭又再打开rs.你应该在再次打开之前调用rs.close一次
locktype
recordset对象open方法的locktype参数表示要采用的lock类型,如果忽略这个参数,那么系统会以recordset对象的locktype属性为预设值。locktype参数包含adlockreadonly、adlockprssimistic、adlockoptimistic及adlockbatchoptimistic等,分述如下:
常数 常数值 说明
adlockreadonly 1 缺省值,recordset对象以只读方式启动,无法运行addnew、update及delete等方法
adlockprssimistic 2 当数据源正在更新时,系统会暂时锁住其他用户的动作,以保持数据一致性。
adlockoptimistic 3 当数据源正在更新时,系统并不会锁住其他用户的动作,其他用户可以对数据进行增、删、改的操作。
adlockbatchoptimistic 4 当数据源正在更新时,其他用户必须将cursorlocation属性改为adudeclientbatch才能对数据进行增、
删、改的操作。
因为你在form_load里已经open了一次(但没有关闭,且用的参数会锁住其他用户的操作),所以,你第二次open的时候就出错了.
请将adlockpessimistic参数改为adlockoptimistic,并且养成好的习惯,及时关闭(释放)无用的资源.
private sub cmdfind_click()
if txtname.text="" then
msgbox "请输入所要查询的名字。",16,"提示"
exit sub
else
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd where 姓名=" & txtname.text & ""
end if
改动过的地方
set rs=new adodb.recordset
改动过的地方
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
不要set rs=nothing 要close
可以这样
private sub cmdfind_click()
if txtname.text="" then
msgbox "请输入所要查询的名字。",16,"提示"
exit sub
else
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd where 姓名=" & txtname.text & ""
end if
‘-------------增加一句
if rs.state<>0 then rs.close
’-----------------
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
option explicit
dim conn as new adodb.connection
dim rs as new adodb.recordset
dim sql as string
private sub form_load()
dim connstr as string
connstr = "provider=microsoft.jet.oledb.4.0;data source=d:\mis\data.mdb"
conn.open connstr
rs.cursorlocation = aduseclient
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd"
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
private sub cmdfind_click()
if txtname.text="" then
msgbox "请输入所要查询的名字。",16,"提示"
exit sub
else
sql = "select 工号,姓名,性别,身份证号码,学历,籍贯,单位编号,职别编号,入厂日期 from rszd where 姓名=" & txtname.text & ""
end if
rs.close
rs.open sql, conn, adopenkeyset, adlockpessimistic
set datagrid1.datasource = rs
在每次打开记录集时,加一句 if rs.state then rs.close
with rs
if .state = adstateopen then .close
.open sql, conn, ....
end with
如上做查询试试用rs.filter


讨论区