Private Sub Form_Load() On Error GoTo ErrRstEmp
Dim strSql As String
strSql = "select empid,empcode,name ,"
strSql = strSql + "department=case when department is null then ''else (select name from Department where id=department) end ,"
strSql = strSql + "duty=case when duty is null then '' else duty end,"
strSql = strSql + "countmeth=case when countmeth='True ' then '是' else '否' end ,"
strSql = strSql + "isoperate=case when Isoperate='True ' then '有' else '无' end,"
strSql = strSql + "sex=Case when sex ='True ' then '男' else '女' end ,"
strSql = strSql + "birthday=case when birthday is Null then null else birthday end,"
strSql = strSql + "email=case when email is null then ' ' else email end ,"
strSql = strSql + "culture=case when culture is null then ' ' else culture end ,"
strSql = strSql + "postdate=case when postdate is null then null else postdate end,"
strSql = strSql + "forefather=case when forefather is null then ' 'else forefather end"
strSql = strSql + " " + "From Employ"
' MsgBox strSql
Set rstEmp = New ADODB.Recordset
rstEmp.CursorLocation = adUseClient
rstEmp.Open strSql, conn, adOpenDynamic, adLockBatchOptimistic, adCmdText
If rstEmp.RecordCount = 0 Then GoTo ErrRstEmp
Set dgdEmp.DataSource = rstEmpend subPrivate Function SaveNewEmp() As Boolean
'保存新增'On Error GoTo AddEmpErr Dim varIndex As Variant
Dim nID As Integer
Dim rstID As New ADODB.Recordset
Set rstID = New ADODB.Recordset
rstID.Open "select Max(empid) as empid from employ ", conn, adOpenKeyset, adLockOptimistic, adCmdText
If rstID.RecordCount = 0 Then '数据库中无数据
nID = 1
varIndex = 1
Else
nID = rstID.Fields("empid").Value + 1 '最大的id +1
varIndex = nID
End If
rstEmp.AddNew
rstEmp!empid = nID
rstEmp!empcode = Trim(txtEmpCode.Text)
rstEmp!Name = Trim(txtEmpName.Text)
If cboDept.ListIndex = -1 Then
'rstEmp!department = ""
Else
rstEmp!department = cboDept.Text '出现上述错误
End If
’==========================以下也同样出现错误 If cboSex.ListIndex = 0 Then
rstEmp!sex = "True "
ElseIf cboSex.ListIndex = 1 Then
rstEmp!sex = "False"
End If
If chkOperate.Value = 1 Then
rstEmp!isoperate = "True"
ElseIf chkOperate.Value = 0 Then
rstEmp!isoperate = "False"
End If If chkWork.Value = 1 Then
rstEmp!countmeth = "True"
ElseIf chkWork.Value = 0 Then
rstEmp!countmeth = "False"
End If
rstEmp.UpdateBatch '提交到数据库
rstEmp.MoveLast
SaveNewEmp = True
Set rstID = Nothing
Exit Function
AddEmpErr: SaveNewEmp = False
Call SQLError(conn)
rstEmp.CancelBatch
rstEmp.MoveLast
Set rstID = Nothingend function
Dim strSql As String
strSql = "select empid,empcode,name ,"
strSql = strSql + "department=case when department is null then ''else (select name from Department where id=department) end ,"
strSql = strSql + "duty=case when duty is null then '' else duty end,"
strSql = strSql + "countmeth=case when countmeth='True ' then '是' else '否' end ,"
strSql = strSql + "isoperate=case when Isoperate='True ' then '有' else '无' end,"
strSql = strSql + "sex=Case when sex ='True ' then '男' else '女' end ,"
strSql = strSql + "birthday=case when birthday is Null then null else birthday end,"
strSql = strSql + "email=case when email is null then ' ' else email end ,"
strSql = strSql + "culture=case when culture is null then ' ' else culture end ,"
strSql = strSql + "postdate=case when postdate is null then null else postdate end,"
strSql = strSql + "forefather=case when forefather is null then ' 'else forefather end"
strSql = strSql + " " + "From Employ"
' MsgBox strSql
Set rstEmp = New ADODB.Recordset
rstEmp.CursorLocation = adUseClient
rstEmp.Open strSql, conn, adOpenDynamic, adLockBatchOptimistic, adCmdText
If rstEmp.RecordCount = 0 Then GoTo ErrRstEmp
Set dgdEmp.DataSource = rstEmpend subPrivate Function SaveNewEmp() As Boolean
'保存新增'On Error GoTo AddEmpErr Dim varIndex As Variant
Dim nID As Integer
Dim rstID As New ADODB.Recordset
Set rstID = New ADODB.Recordset
rstID.Open "select Max(empid) as empid from employ ", conn, adOpenKeyset, adLockOptimistic, adCmdText
If rstID.RecordCount = 0 Then '数据库中无数据
nID = 1
varIndex = 1
Else
nID = rstID.Fields("empid").Value + 1 '最大的id +1
varIndex = nID
End If
rstEmp.AddNew
rstEmp!empid = nID
rstEmp!empcode = Trim(txtEmpCode.Text)
rstEmp!Name = Trim(txtEmpName.Text)
If cboDept.ListIndex = -1 Then
'rstEmp!department = ""
Else
rstEmp!department = cboDept.Text '出现上述错误
End If
’==========================以下也同样出现错误 If cboSex.ListIndex = 0 Then
rstEmp!sex = "True "
ElseIf cboSex.ListIndex = 1 Then
rstEmp!sex = "False"
End If
If chkOperate.Value = 1 Then
rstEmp!isoperate = "True"
ElseIf chkOperate.Value = 0 Then
rstEmp!isoperate = "False"
End If If chkWork.Value = 1 Then
rstEmp!countmeth = "True"
ElseIf chkWork.Value = 0 Then
rstEmp!countmeth = "False"
End If
rstEmp.UpdateBatch '提交到数据库
rstEmp.MoveLast
SaveNewEmp = True
Set rstID = Nothing
Exit Function
AddEmpErr: SaveNewEmp = False
Call SQLError(conn)
rstEmp.CancelBatch
rstEmp.MoveLast
Set rstID = Nothingend function
解决方案 »
- VB新手,请问控件在网页中如何控制表单的值
- 如何操作网上数据库
- VB6.0中DataReport设计好的数据报表,报表能按这种格式导出到Excel中吗?
- 啊,为什么list控件只能载入30000多行??
- 各位高手,小弟我又遇到了一个我认为是不可思议的问题!望知情者请回帖!谢谢!50分哦!快来吧!
- 定义dim adocon As ADODB.Connection和dim adocon As new ADODB.Connection有什么区别
- 什么数据控件既可以显示数据库中表中的数据 又可以修改数据
- VB环境下access表的代码问题~~谢谢大家了
- 关于CSDN下半年的收费会员服务,大家没注意到吗?
- 2个API应用的小问题:
- 一个应该比较简单的问题(在线等待解答,谢谢)
- 一个奇怪的combobox控件问题.
在Set rstID = Nothing 前面加一个rstID.close 再看看??
字段是不是够长,如果用汉字最好用nvarchar类型。
你把On Error GoTo ErrRstEmp这句先去掉,运行就会出现err.Number及Err.Description
把字段department的長度加長一點,就是不夠長
怎么解决这个问题
只有 从数据库中直接提取 true 通过select sex case when sex='True' then '男' else '女' end from employ 再绑定到DataGrid 上 注意:sex 是 Boolean 型的
揭贴
if 条件成立 then
rs.布尔字段=1
else
rs.布尔字段=0
end if
如果向你所写的那样,可能系统会认为你将要赋值为一个长度为4的字符串了