两段程序几乎一样,为什么结果不同?

spr79 2003-04-04 12:49:27
我再对一个数据库进行条件查找时.想通过出发label_click或MonthView1_DateClick事件来找出对应日期的记录,下面是两断相关代码:
Private Sub Label1_Click(Index As Integer)
Dim Mydate As String
On Error GoTo Myerror
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "Rjzy.mdb"
Data1.DatabaseName = dbname
Data1.Connect = "Access 2000;"
Data1.RecordsetType = 1
Mydate = Year(Date) & "-" & Month(Date) & "-" & (Index + 1)
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #" & Mydate & "#)"
RichTextBox1.DataField = "内容"
Text1.DataField = "日期"
Text2.DataField = "时间"
Combo3.DataField = "星期"
Combo4.DataField = "天气"
Data1.Refresh
MsgBox Mydate & "你写了" & Data1.Recordset.RecordCount & "日记!"
Timer1.Enabled = False
Exit Sub
Myerror:
If Err.Number = 3021 Then
MsgBox Mydate & "你没有写日记!"
End If
On Error GoTo 0
End Sub
上面折断代码能够运行,并在richtextbox等控件中显示记录内容.
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Dim Mydate As String
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "Rjzy.mdb"
Data1.DatabaseName = dbname
Data1.Connect = "Access 2000;"
Data1.RecordsetType = 1
Mydate = MonthView1.Value
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #" & Mydate & "#)"
On Error GoTo Myerror
RichTextBox1.DataField = "内容"
Text1.DataField = "日期"
Text2.DataField = "时间"
Combo3.DataField = "星期"
Combo4.DataField = "天气"
Data1.Refresh
Timer1.Enabled = False
MonthView1.Visible = False
Exit Sub
Myerror:
If Err.Number = 3021 Then
MsgBox "这一天你没有写日记!"
End If
On Error GoTo 0
End Sub
这一段就不能够再richtextbox等控件中显示记录内容.
请高手们看一下是不是mydate=monthview1.value这存在错误.


另外,第一段也不是完全正确,当查找的条件(日期)再数据库记录中不存在时,即没有找到条件所要求的记录后,再次查找时,就是数据库中存在的记录也找不到了,我怀疑是在没有找到后,记录指针到了.EOF处,敢问老大们,小弟该怎么办?







...全文
18 点赞 收藏 19
写回复
19 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenyu5188 2003-04-06
解决了,那就UP吧
回复
spr79 2003-04-06
up
回复
ulfnboy 2003-04-05
if data1.Recordset.bof=true and data1.Recordset.eof=true then
MsgBox "这一天你没有写日记!"
end if
Data1.Recordset.FindFirst "日期 = #" & Mydate & "#"
回复
spr79 2003-04-05
感谢大家!尤其是 sxs69() ,不错,你的程序是完全正确的.非常感谢!
kmcyz(阿成) 我看你的代码和sxs69() 的差不多,我想你写的代码也一定能够通过
回复
kmcyz 2003-04-05
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Dim Mydate As String
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "Rjzy.mdb"
Data1.DatabaseName = dbname
Data1.Connect = "Access 2000;"
Data1.RecordsetType = 1
Mydate = Format(MonthView1.Value,"YYYY-MM-DD")'改为这样
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #" & Mydate & "#)"

Data1.Refresh '加入这一行
If Data1.Recordset.EOF Then
MsgBox "这一天你没有写日记!"
ELSE
RichTextBox1.DataField = "内容"
Text1.DataField = "日期"
Text2.DataField = "时间"
Combo3.DataField = "星期"
Combo4.DataField = "天气"
Timer1.Enabled = False
MonthView1.Visible = False
End If
End Sub
回复
spr79 2003-04-05
bluelang(多情浪子) 谢谢,我一定记着加上,不过我的问题不是这样,我的时间是"03-4-1".
sxs69() ,你待人的热情真让我感动,太感谢你了.我刚才已经是过了,可能是我没有认真的考虑,等我再是一次.
回复
bluelang 2003-04-05
因为你的 mydate可能等于"03-04-02"加上"20"后就是"2003-04-02"
回复
spr79 2003-04-05
bluelang(多情浪子) ,你这又是什么意思??????
回复
sxs69 2003-04-05
spr79我试过你的代码,确如你所说,但我改过以后测试过多次,没有发生错误,不知你试过我新贴代码没有?
回复
iamnotyou 2003-04-05
http://expert.csdn.net/Expert/topic/1615/1615906.xml?temp=.3189051
回复
bluelang 2003-04-05
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #20" & Mydate & "#)"
回复
spr79 2003-04-05
sxs69() ,关键是得解决,
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #" & Mydate & "#)"这句查询语句,在查询不存在的语句时,记录指针去向的问题.如果找不到,那么第二次查询时就会出错,弹出"无当前记录"的提示
回复
goodgoodgood 2003-04-05
up
回复
sxs69 2003-04-05
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Dim Mydate As String
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "Rjzy.mdb"
Data1.DatabaseName = dbname
Data1.Connect = "Access 2000;"
Data1.RecordsetType = 1
Mydate = Year(MonthView1.Value) & "-" & Month(MonthView1.Value) & "-" & Day(MonthView1.Value)
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #" & Mydate & "#)"

Data1.Refresh
If Data1.Recordset.EOF Then
MsgBox "这一天你没有写日记!"
ELSE
RichTextBox1.DataField = "内容"
Text1.DataField = "日期"
Text2.DataField = "时间"
Combo3.DataField = "星期"
Combo4.DataField = "天气"
Timer1.Enabled = False
MonthView1.Visible = False
End If
End Sub
回复
spr79 2003-04-05
还是不行?
我的程序是要完成这样的功能.在form_click 事件中先将data1控件与数据库绑定,然或通过command1_click事件向数据库中添加记录,让后通过monthview1_click 事件对该数据库进行条件查询,并将找到的记录内容显示在richtext1、text等控件中。下面是部分代码:Private Sub Form_Load()
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "Rjzy.mdb"
Set db = OpenDatabase(dbname)
Data1.DatabaseName = dbname
Data1.Connect = "Access 2000;"
Data1.RecordSource = Namer
RichTextBox1.DataField = "内容"
Text1.DataField = "日期"
Text2.DataField = "时间"
Combo3.DataField = "星期"
Combo4.DataField = "天气"
RichTextBox1.Enabled = False
Combo3.Enabled = False
Combo4.Enabled = False
Command1.Enabled = False
Command2.Enabled = True
Data1.Refresh
End Sub

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Dim Mydate As String
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "Rjzy.mdb"
Data1.DatabaseName = dbname
Data1.Connect = "Access 2000;"
Data1.RecordsetType = 1
Mydate = Year(MonthView1.Value) & "-" & Month(MonthView1.Value) & "-" & Day(MonthView1.Value)
Data1.RecordSource = " select * from " & Namer & " where( 日期 = #" & Mydate & "#)"
On Error GoTo Myerror
RichTextBox1.DataField = "内容"
Text1.DataField = "日期"
Text2.DataField = "时间"
Combo3.DataField = "星期"
Combo4.DataField = "天气"
Data1.Refresh
Timer1.Enabled = False
MonthView1.Visible = False
If Data1.Recordset.EOF Then
MsgBox "这一天你没有写日记!"
Data1.Recordset.MovePrevious
End If
Exit Sub
Myerror:
If Err.Number = 3021 Then
MsgBox "这一天你没有写日记!"
End If
On Error GoTo 0
RichTextBox1.Enabled = False
Combo3.Enabled = False
Combo4.Enabled = False
End Sub

老大们,可一定要帮帮我呀,不然,我可死定了!!!
回复
spr79 2003-04-04
老大,我想你是误解了我的意思,如果我没有看错的话,您上面的程序只是求出了数据库中的总记录数,
而我的问题是,当查找不到记录(即所点击的日期不在纪录中时)后,程序就弹出"找不到当前记录"的错误提示.您看采取什么方法能够避免select时检索到EOF.
回复
sxs69 2003-04-04
Data1.Refresh
Data1.Recordset.MoveFirst
Dim sum As Integer
sum = 0
Do While Not Data1.Recordset.EOF
sum = sum + 1
Data1.Recordset.MoveNext
Loop
Data1.Recordset.Close
MsgBox Mydate & "你写了" & SUM & "日记!"

Myerror:
Data1.Recordset.Close
If Err.Number = 3021 Then
MsgBox "这一天你没有写日记!"
End If
On Error GoTo 0
End Sub

回复
spr79 2003-04-04
第一个问题解决了,请老大们帮我想一想第二个问题应该怎么解决?
回复
sxs69 2003-04-04
Mydate = format(MonthView1.Value)
回复
相关推荐
发帖

1187

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2003-04-04 12:49
社区公告
暂无公告