| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1500 人关注过本帖
标题:如果连接一个空的数据库时,如何写代码呢
只看楼主 加入收藏
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
结帖率:84.62%
收藏
 问题点数:0 回复次数:16 
如果连接一个空的数据库时,如何写代码呢
如果连接一个空的数据库时,如何写代码呢?
这是我的“添加”控件代码如下:
Private Sub Command2_Click()
'Set Rst = CreateObject("ADODB.Recordset")
If (Rst.State = adStateOpen) Then
Rst.Close
End If
Rst.CursorLocation = adUseClient
Rst.Open "Select * From 同事信息", Conn, adOpenKeyset, adLockPessimistic
Rst.AddNew
Rst.Fields("姓名").Value = Text1.Text
Rst.Fields("单位名称").Value = Text2.Text
Rst.Fields("办公电话").Value = Text3.Text
Rst.Fields("办公电话二").Value = Text4.Text
Rst.Fields("手机").Value = Text5.Text
Rst.Fields("手机二").Value = Text6.Text
Rst.Fields("宅电").Value = Text7.Text
Rst.Fields("宅电二").Value = Text8.Text

Rst.Fields("电子邮箱").Value = Text9.Text
Rst.Fields("电子邮箱二").Value = Text10.Text
Rst.Fields("QQ号").Value = Text11.Text
Rst.Fields("QQ号二").Value = Text12.Text
Rst.Fields("地址").Value = Text13.Text
Rst.Fields("邮政编码").Value = Text14.Text
Rst.Fields("其他").Value = Text15.Text
Rst.Update
MsgBox "添加成功"
Text16.Text = Rst.RecordCount
CmdAdd.Visible = True
Command2.Visible = False

CmdDelete.Enabled = True
CmdPrevious.Enabled = True
CmdNext.Enabled = True
CmdFirst.Enabled = True
CmdLast.Enabled = True
End Sub

当我连接的数据库表为空记录时,则显示为:无效使用NULL!
黄条覆盖在“Text1.Text = Rst.Fields("姓名").Value”(第一字段上)

不是空记录时,则正常!

搜索更多相关主题的帖子: Rst 代码呢 数据库 Value 
2006-10-30 21:09
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
难道要修改字段的每个字段的常规属性里的某个属性么?
2006-10-30 21:42
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
不知道..你的题目和你的问题有什么直接关联....空的数据库....不什么是啥....既然已是空...你还连接数据库干啥?...有实际意义吗....
而你的问题..是 字段值内容为Null,,,而不是数据库...搞清楚了..免得别人笑话了....
你只需要加个判断去判断是否为Null..就可以...
If Not isNull(Rst.Fields("姓名").Value) Then
Text1.Text = Rst.Fields("姓名").Value
Else
Text1.Text = "Null"
End If

我的msn: myfend@
2006-10-31 09:25
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

Private Sub Form_Load()
Dim ConString As String
ConString = "Provider=Microsoft.Jet.OleDb.4.0;Persist Security Info = False;" _
& "Data Source =" & App.Path & "\cl.mdb"

Set Conn = CreateObject("ADODB.Connection")
With Conn
.ConnectionString = ConString
.Open
End With

Rst.CursorLocation = adUseClient
Rst.Open "Select * From cl2", Conn, adOpenKeyset, adLockPessimistic, adCmdText

If Not IsNull Then
Text1.Text = Rst.Fields("dd").Value
Text2.Text = Rst.Fields("ff").Value
Text3.Text = Rst.Fields("mm").Value
Text4.Text = Rst.Fields("ss").Value
Text5.Text = Rst.Fields("zz").Value
Text6.Text = Rst.Fields("xx").Value
Text7.Text = Rst.Fields("cj").Value
Else
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
End If

End Sub

执行后出现提示:IsNull是必选参数

是什么意思?

我没有那CL--CL2表的所有字段的默认值作出改动,还是为"0"

再把“”换成NULL,结果还是一样!!

2006-11-19 15:39
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
If Not IsNull Then '什么意思?..IsNull是个函数..

我的msn: myfend@
2006-11-19 15:45
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

能详细介绍下,可以么?

有时侯在书上,找不到啊!

谢谢!

2006-11-19 15:52
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
大哥...看3楼..我不是已经回答了吗..

我的msn: myfend@
2006-11-19 16:02
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

Private Sub Form_Load()
Dim ConString As String
ConString = "Provider=Microsoft.Jet.OleDb.4.0;Persist Security Info = False;" _
& "Data Source =" & App.Path & "\cl.mdb"

Set Conn = CreateObject("ADODB.Connection")
With Conn
.ConnectionString = ConString
.Open
End With

Rst.CursorLocation = adUseClient
Rst.Open "Select * From cl2", Conn, adOpenKeyset, adLockPessimistic, adCmdText

If Not IsNull(Rst.Fields("dd").Value) Then
Text1.Text = Rst.Fields("dd").Value
Else
Text1.Text = Null
End If

If Not IsNull(Rst.Fields("ff").Value) Then
Text2.Text = Rst.Fields("ff").Value
Else
Text2.Text = Null
End If
...
...
If Not IsNull(Rst.Fields("cj").Value) Then
Text7.Text = Rst.Fields("cj").Value
Else
Text7.Text = Null
End If

End Sub

执行后出现提示框:

实时错误"3021"
BOF或EOF有一个是"真",或者当前的记录已被删除,所需要的一个操作要求一个当前的记录!

2006-11-19 16:39
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
If rst.recordCount > 0 Then
If Not rst.Eof Or Not rst.Bof Then
if Not IsNull(Rst.Fields("dd").Value) Then
text1.text = Rst.Fields("dd").Value
else
text1.text = "null"
End if

'.......
'.......
End if
End If

我的msn: myfend@
2006-11-19 18:41
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

我这样写,就可以了!
If rst.RecordCount >0 Then

Text1.Text = Rst.Fields("dd").Value
Text2.Text = Rst.Fields("ff").Value
Text3.Text = Rst.Fields("mm").Value
Text4.Text = Rst.Fields("ss").Value
Text5.Text = Rst.Fields("zz").Value
Text6.Text = Rst.Fields("xx").Value
Text7.Text = Rst.Fields("cj").Value
Else
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
End If

但因为是计算程序,所以在保存时,就出现了问题:

在ACCESS表里显示的却是整数!

因为"CJ"字段在TEXT7通过计算用COMMAND2计算按钮得出,而在ACCESS打开时,却为 "0" 没有输出答案!

2006-11-19 20:42
快速回复:如果连接一个空的数据库时,如何写代码呢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012313 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved