1、因为“性别”字段是文本类型,INSERT INTO 语句中不包含字段“性别”才可以默认;你的语句包含了,且也对应了Trim(txtSex.Text),如果txtSex为空自然就更新字段为空了;2、日期型字段,如果对应的写入数据为空,则MSSQL自动加一个时间1900-1-1,ACCESS是1899-1-1
建议1:在insert into 表名后写明字段 insert into 读者信息(字段1,字段2,字段3) values(...) 建议2:“性别”字段可以数值型来代替,如(0-男 1-女 2-未知) 建议3:添加日期字段时可以这样处理: "insert into 表(日期字段) values("& iif(日期变量="","NULL","'"& 日期变量 &"'") &")
建议2:“性别”字段可以数值型来代替,如(0-男 1-女 2-未知)
建议3:添加日期字段时可以这样处理:
"insert into 表(日期字段) values("& iif(日期变量="","NULL","'"& 日期变量 &"'") &")
If txtSex = "" Then txtSex = "男"
If txtCardDate = "" Then txtCardDate = Format(Now(), "yyyy-MM-dd") txtSQL = "Insert into 读者信息 values('" & Trim(txtBorrowNo.Text) & "'," & "'" & Trim(txtName.Text) & "'," & "'" & Trim(txtSex.Text) & "'," & "'" & Trim(txtDepartmentNo.Text) & "'," & "'" & Trim(txtSpeciality.Text) & "'," & "'" & Trim(txtCardDate.Text) & "'," & "'" & Trim(txtReaderTypeNo.Text) & "'," & "'" & Trim(txtDemo.Text) & "')"
txtSQL = "select * from [读者信息] where (借书证号='" & Trim(txtBorrowNo.Text) & "')"
cmd.CommandText = txtSQL
Set rs = cmd.Execute
If rs.EOF = False Then
MsgBox "借书证号已经存在,请重新输入!", vbOKOnly + vbExclamation, "警告"
Else
If Trim(txtSex.Text) = "" Then
txtSex.Text = "男"
end if
If Trim(txtCardDate.Text) = "" Then
txtCardDate.Text = Format$(Now(), "yyyy-MM-dd")
end if
txtSQL = "Insert into [读者信息] values('" & Trim(txtBorrowNo.Text) & "'," & "'" & Trim(txtName.Text) & "'," & "'" & Trim(txtSex.Text) & "'," & "'" & Trim(txtDepartmentNo.Text) & "'," & "'" & Trim(txtSpeciality.Text) & "'," & "'" & Trim(txtCardDate.Text) & "'," & "'" & Trim(txtReaderTypeNo.Text) & "'," & "'" & Trim(txtDemo.Text) & "')"
cmd.CommandText = txtSQL
cmd.Execute
End If
End Sub
楼主您用的控件有些地方不对。比如默认为男(txtsex.txt)和日期型(txtCardDate.Text)应该对应使用Combo控件和DTPicker控件。