这段代码如果查询刚录入的数据是可以的,其他也正常,但在关闭这个窗口后再重新打开就无法查询了。请大伙帮忙找找错误Private Sub cmd2_Click(Index As Integer)
   Dim infcomb3 As String
   Dim infcomb2 As String
   Dim infcomb1 As String
   infcomb3 = Trim(Combo3.Text)
   infcomb2 = Trim(Combo2(0).Text)
   infcomb1 = Trim(Combo2(1).Text)
   If rsd1.State = 0 Then
     rsd1.CursorLocation = adUseClient
     rsd1.Open "select * from ydgzjh where glrz_rq= '" & infcomb3 & " ' and  glrz_xm= '" & infcomb2 & "' and glrz_bm ='" & infcomb1 & "' and glrz_sj= '" & DTPicker1.Value & "'", conn, adOpenDynamic, adLockPessimistic
   End If
     If Not rsd1.EOF Then
     
       txt1(0).Text = rsd1("glrz_gzjhnr") & ""
       txt2(1).Text = rsd1("glrz_gzjhwccs") & ""
       txt3(2).Text = rsd1("glrz_wcqx") & ""
       
       txt(3).Text = rsd1("glrz_gzjhnr1") & ""
       txt(4).Text = rsd1("glrz_gzjhwccs1") & ""
       txt(5).Text = rsd1("glrz_wcqx1") & ""
       
       txt6(6).Text = rsd1("glrz_gzjhnr2") & ""
       txt7(7).Text = rsd1("glrz_gzjhwccs2") & ""
       txt8(8).Text = rsd1("glrz_wcqx2") & ""
       
       txt9(9).Text = rsd1("glrz_gzjhnr3") & ""
       txt10(10).Text = rsd1("glrz_gzjhwccs3") & ""
       txt11(11).Text = rsd1("glrz_wcqx3") & ""
       
       txt9(0).Text = rsd1("glrz_gzjhnr4") & ""
       txt9(1).Text = rsd1("glrz_gzjhwccs4") & ""
       txt9(2).Text = rsd1("glrz_wcqx4") & ""
       
       txt9(3).Text = rsd1("glrz_gzjhnr5") & ""
       txt9(4).Text = rsd1("glrz_gzjhwccs5") & ""
       txt9(5).Text = rsd1("glrz_wcqx5") & ""
       
       txt12(12).Text = rsd1("glrz_zdgzsy") & ""
       txtbmjl(13).Text = rsd1("glrz_bmldyj") & ""
       txtfgld(14).Text = rsd1("glrz_fgldyj") & ""
              
       Combo1.Text = rsd1("glrz_rq")
       rsd1.Close
       Set rsd1 = Nothing
       
     ElseIf rsd1.EOF Then
       MsgBox "没有要找的数据!", vbOKOnly, "提示"
       rsd1.Close
       Set rsd1 = Nothing
       Exit Sub
     End If
End Sub

解决方案 »

  1.   

    Private Sub cmd2_Click(Index As Integer)
        Dim infcomb3 As String
        Dim infcomb2 As String
        Dim infcomb1 As String
        infcomb3 = Trim(Combo3.Text)
        infcomb2 = Trim(Combo2(0).Text)
        infcomb1 = Trim(Combo2(1).Text)
        
        '当记录打开时,则关闭
        If rsd1.State = adStateOpen Then rsd1.Close
        '打开记录集
        rsd1.CursorLocation = adUseClient
        rsd1.Open "select * from ydgzjh where glrz_rq= '" & infcomb3 & " ' and  glrz_xm= '" & infcomb2 & "' and glrz_bm ='" & infcomb1 & "' and glrz_sj= '" & DTPicker1.Value & "'", conn, adOpenDynamic, adLockPessimistic
        '显示记录
        If Not rsd1.RecordCount > 0 Then
            txt1(0).Text = rsd1("glrz_gzjhnr").Value
            txt2(1).Text = rsd1("glrz_gzjhwccs").Value
            txt3(2).Text = rsd1("glrz_wcqx").Value
            txt(3).Text = rsd1("glrz_gzjhnr1").Value
            txt(4).Text = rsd1("glrz_gzjhwccs1").Value
            txt(5).Text = rsd1("glrz_wcqx1").Value
            txt6(6).Text = rsd1("glrz_gzjhnr2").Value
            txt7(7).Text = rsd1("glrz_gzjhwccs2").Value
            txt8(8).Text = rsd1("glrz_wcqx2").Value
            txt9(9).Text = rsd1("glrz_gzjhnr3").Value
            txt10(10).Text = rsd1("glrz_gzjhwccs3").Value
            txt11(11).Text = rsd1("glrz_wcqx3").Value
            txt9(0).Text = rsd1("glrz_gzjhnr4").Value
            txt9(1).Text = rsd1("glrz_gzjhwccs4").Value
            txt9(2).Text = rsd1("glrz_wcqx4").Value
            txt9(3).Text = rsd1("glrz_gzjhnr5").Value
            txt9(4).Text = rsd1("glrz_gzjhwccs5").Value
            txt9(5).Text = rsd1("glrz_wcqx5").Value
            txt12(12).Text = rsd1("glrz_zdgzsy").Value
            txtbmjl(13).Text = rsd1("glrz_bmldyj").Value
            txtfgld(14).Text = rsd1("glrz_fgldyj").Value
            Combo1.Text = rsd1("glrz_rq").Value
        ElseIf rsd1.EOF Then
            MsgBox "没有要找的数据!", vbOKOnly, "提示"
        End If
        rsd1.Close
        Set rsd1 = Nothing
    End Sub代码先改成这样,另外,不知你的conn是在那里打开的。执行这段代码之前,一定要把conn打开。建议在程式一开始时,就打开conn.这样,你的程式应当就正常。另外,建议写代码时,不要太偷懒。
      

  2.   

    是在启动窗口启动时就打开了的,帮忙看看SELECT 语句有没有错啊,谢谢了
      

  3.   

    看了一下数据库插入的数据。
    我在FORM LOAD的时候加了一句DTPicker1.value=now
    数据库里的时间变成了yyyy-mm-dd hh:mm:ss的格式了。
      

  4.   

    那你要检查,你的数据库中存储的值。
    你可试着把DTpick1.value 改为 Format(DTPicker1.value,"yyyy-MM-dd"),再试试了。
    另外,你的语句中都是栏位=值的,所以必须核对数据库中对应栏位到底有没有值。