源程序如下(我想添加一个记录)
Private Sub menuadd_Click()
If txtid <> "" Then
sql = "select * from " & tname & " where id=" & "'" & txtid.Text & "'"
Set rec = db.OpenRecordset(sql)
If rec.EOF Then
rec.AddNew
rec.Fields(0).Value = txtid.Text
rec.Fields(1).Value = txtname.Text
rec.Fields(2).Value = txtsex.Text
If txtage.Text = "" Or txtsalary.Text = "" Then
MsgBox "年龄和工资为空或类型错误", vbCritical, "错误"
Exit Sub
Else
rec.Fields(3).Value = CInt(txtage.Text)
rec.Fields(4).Value = CCur(txtage.Text)
End If
Else
MsgBox ("记录已经存在")
End If
txtid.Text = ""
txtname.Text = ""
txtage.Text = ""
txtsex.Text = ""
txtsalary.Text = ""
txtjiguan.Text = ""
Else
MsgBox "id can not be null"
End Ifvb提示我在
sql = "select * from " & tname & " where id=" & "'" & txtid.Text & "'"
Set rec = db.OpenRecordset(sql)
有错误(标准表达式中数据类型不匹配),我不是很懂,希望给予指教,谢谢
Private Sub menuadd_Click()
If txtid <> "" Then
sql = "select * from " & tname & " where id=" & "'" & txtid.Text & "'"
Set rec = db.OpenRecordset(sql)
If rec.EOF Then
rec.AddNew
rec.Fields(0).Value = txtid.Text
rec.Fields(1).Value = txtname.Text
rec.Fields(2).Value = txtsex.Text
If txtage.Text = "" Or txtsalary.Text = "" Then
MsgBox "年龄和工资为空或类型错误", vbCritical, "错误"
Exit Sub
Else
rec.Fields(3).Value = CInt(txtage.Text)
rec.Fields(4).Value = CCur(txtage.Text)
End If
Else
MsgBox ("记录已经存在")
End If
txtid.Text = ""
txtname.Text = ""
txtage.Text = ""
txtsex.Text = ""
txtsalary.Text = ""
txtjiguan.Text = ""
Else
MsgBox "id can not be null"
End Ifvb提示我在
sql = "select * from " & tname & " where id=" & "'" & txtid.Text & "'"
Set rec = db.OpenRecordset(sql)
有错误(标准表达式中数据类型不匹配),我不是很懂,希望给予指教,谢谢
Set rec = db.OpenRecordset(sql)
你的tname是什么东东呀 , 如果是已知表, 你应该:
sql = "select * from tname where id='" & Trim(txtid.Text) & "'"
Set rec = db.OpenRecordset(sql)
试试看
看看数据库中的id字段是什么类型的,如果为自动编号或其它数值型,改
sql = "select * from " & tname & " where id=" & "'" & txtid.Text & "'"
为:
sql = "select * from " & tname & " where id=" & txtid.Text
Set rec = db.OpenRecordset(sql,dbOpenDynaset)改为sql = "select * from tname " where id=" & "'" & txtid.Text & "'"Set rec = db.OpenRecordset(sql,dbOpenDynaset)前提tname 是一个表
Set rec = db.OpenRecordset(sql)改为sql = "select * from tname " where id=" & "'" & txtid.Text & "'"Set rec = db.OpenRecordset(sql,dbOpenDynaset)前提tname 是一个表
If txtage.Text = "" Or txtsalary.Text = "" Then
MsgBox "年龄和工资为空或类型错误", vbCritical, "错误"
Exit Sub
end if
dim rs as recordset
sql = "select * from " & tname & " where id=" & "'" & txtid.Text & "'"
rs.open sql,conn,adOpenKeyset, adLockPessimistic
if (rs.eof = false) then
msgbox("记录已存在")
exit sub
end if
rs.addnew
........作程序的时候,最好是在检测完输入的条件合法时,在打开表添加数据,不要在添加的过程中再去检测输入的数据类型是否合法