以前我一直对SQL数据库进行编程操作,没什么问题。现在要用到access发现有些问题无法解决,而且不知原因,请大家赐教!测试代码如下:
Private Sub Command1_Click()
Set rec1 = CreateObject("adodb.recordset")
Set conn1 = CreateObject("adodb.connection")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\wy.mdb"
conn1.Open strcon
strsql = "insert into wy(a,b,c) values (" & 1 & "," & 2 & "," & 3 & ")"
rec1.Open strsql, conn1  MSFlexGrid1.Clear
  MSFlexGrid1.Refresh
       With MSFlexGrid1
       '.Rows = 2
          .Cols = 3
          .Row = 0
          .Col = 0
          .Text = "a"
          .Col = 1
          '.ColWidth(0) = 1600
          .Text = "b"
          .Col = 2
          '.ColWidth(1) = 600
          .Text = "c"
          
       End With
       
       
Set rec2 = CreateObject("adodb.recordset")
Set conn2 = CreateObject("adodb.connection")
strcon2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\wy.mdb"
conn2.Open strcon2
strsql = "select * from wy "
rec2.Open strsql, conn2
Do While Not rec2.EOF
   With MSFlexGrid1
   .Col = 0
   .Rows = .Rows + 1
   .Text = IIf(Left(rec2!a, 1) > "", rec2!a, "")
   .Col = 1
   .Text = IIf(Left(rec2!b, 1) > "", rec2!b, "")
   .Col = 2
   .Text = IIf(Left(rec2!c, 1) > "", rec2!c, "")
   .Row = .Row + 1
   End With
   rec2.MoveNext
Loop
End Sub
按按钮后新数据保存后没有即时显示出来,再按按钮后才显示第一次的数据,而第二次保存的数据又没能显示出来!怎么回事?请赐教(该段代码已调试好,可直接引用后调试!)

解决方案 »

  1.   

    Private Sub Command1_Click()
    Set rec1 = CreateObject("adodb.recordset")
    Set conn1 = CreateObject("adodb.connection")
    strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\wy.mdb"
    conn1.Open strcon
    strsql = "insert into wy(a,b,c) values (" & 1 & "," & 2 & "," & 3 & ")"
    conn1.execute strsql  MSFlexGrid1.Clear
      MSFlexGrid1.Refresh
           With MSFlexGrid1
           '.Rows = 2
              .Cols = 3
              .Row = 0
              .Col = 0
              .Text = "a"
              .Col = 1
              '.ColWidth(0) = 1600
              .Text = "b"
              .Col = 2
              '.ColWidth(1) = 600
              .Text = "c"
              
           End With
           
    strsql = "select * from wy "
    rec1.Open strsql, conn1
    Do While Not rec1.EOF
       With MSFlexGrid1
       .Col = 0
       .Rows = .Rows + 1
       .Text = IIf(Left(rec2!a, 1) > "", rec2!a, "")
       .Col = 1
       .Text = IIf(Left(rec2!b, 1) > "", rec2!b, "")
       .Col = 2
       .Text = IIf(Left(rec2!c, 1) > "", rec2!c, "")
       .Row = .Row + 1
       End With
       rec1.MoveNext
    Loop
    rec1.close
    conn1.close
    set rec1=nothing
    set conn1=nothing
    End Sub
      

  2.   

    楼上的兄弟能说说为什么会这样吗?干吗一定要close和nothing
      

  3.   

    把MSFlexGrid换成MSHFlexGrid
    条件在SQL语句中整理
    然后把记录集赋给MSHFlexGrid:Set MSHFlexGrid1.DataSource = adors