我使用ado和datagrid这2个控件绑定来显示ACCESS数据库,运行时我需要把收到的数据直接在datagrid控件中显示出来,我用ado.recordset.addnew来添加新记录,运行出来确实显示添加了一行可是没数据显示,后来发现addnew打出括号后可以跟参数,我估计就是参数没选,因为datagrid显示ACCESS数据时就要对应列里的标题,不然不能显示.所以我想问下具体参数怎么带??参数好象提示fields,valuae.

解决方案 »

  1.   


    下面是完整显示数据库数据的例子 你看看,使用的就是你说的那个控件,
    Dim objRs As New Recordset, objCn As New Connection, intPage As Integer
    'Dim objDataSource As RecordsetPublic Sub ShowData(ByVal intPage As Integer)    Dim intPageCount As Integer, I As Integer, J As Integer
        objRs.PageSize = Val(txtPageSize)
        objRs.AbsolutePage = intPage
        
        
        MSFlexGrid1.Clear
        For I = 0 To objRs.Fields.Count - 1
            MSFlexGrid1.TextMatrix(0, I) = objRs.Fields(I).Name
        Next
        For I = 1 To objRs.PageSize
            For J = 0 To objRs.Fields.Count - 1
                MSFlexGrid1.TextMatrix(I, J) = objRs.Fields(J)
            Next
            objRs.MoveNext
           If objRs.EOF Then Exit For
        Next
        
    '    '创建一个局部的recordset 对象保存objrs 当前记录数据
    '    For intRecord = 0 To objRs.Fields.Count - 1
    '        objDataSource.Fields.Append objRs.Fields(intRecord).Name, adVarChar, _
    '        objRs.Fields(intRecord).DefinedSize
    '                                        '确定长度
    '     Next
    '     objDataSource.Open
    '     objRs.PageSize = Val(txtPageSize)
    '     objRs.AbsolutePage = intPage
    '     For intRecord = 1 To objRs.PageSize
    '        objDataSource.AddNew
    '        objDataSource!用户名 = objRs!用户名
    '        objDataSource!口令 = objRs!口令
    '        objDataSource!身份 = objRs!身份
    '        objRs.MoveNext
    '        If objRs.EOF Then Exit For
    '    Next
    '
    '    Set DataGrid1.DataSource = objDataSource '设置为数据网格数据源
        txtMsg = intPage & "/" & objRs.PageCount
    End Sub
    Private Sub cmdNext_Click()
        If intPage <> objRs.PageCount Then
            intPage = intPage + 1
            ShowData (intPage)
        End If
        
    End SubPrivate Sub command1_Click()
        objDataSource.MovePrevious
        If objDataSource.BOF Then
            objDataSource.MoveFirst
        End If
        
    End SubPrivate Sub Command2_Click()
        objDataSource.MoveNext
        If objDataSource.EOF Then
            objDataSource.MoveLast
        End If
        
    End SubPrivate Sub comPre_Click()
    If intPage <> 1 Then
        intPage = intPage - 1
        ShowData (intPage)
    End IfEnd SubPrivate Sub DataGrid1_Click()End SubPrivate Sub Form_Load()
        Dim strSQL As String, strCn As String, iRS As Integer, jRS As Integer
        txtPageSize = "5"
        intPage = 1
        '建立数据连接
        strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;" & _
        "Data Source=" & App.Path & "\登录.mdb"
        objCn.ConnectionString = strCn
        objCn.Open
        With objRs
            .CursorLocation = adUseClient
            .CursorType = adOpenStatic
            .Open "系统用户", objCn, adOpenStatic, adLockReadOnly
        End With
        '定义MSFLEXGRID控件
        MSFlexGrid1.Cols = objRs.Fields.Count
        MSFlexGrid1.Rows = objRs.RecordCount + 1
        
        
       ShowData (intPage)    
            
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        objCn.Close
        Set objCn = Nothing
        Set objRs = Nothing
        
    End Sub
    Private Sub txtPageSize_KeyPress(KeyAscii As Integer)
        Dim bOK As Boolean
        If KeyAscii = vbKeyReturn And Trim(txtPageSize) <> "" Then
            '如果按回车键,且记录也大小不为空,刷新当前记录页
            intPage = 1
            ShowData (intPage)
        ElseIf KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then
            '检查当前设置的记录页是否在有效范围之内
            bOK = Val(txtPageSize & Chr(kayascii) < 1 Or Val(txtPageSize) & Chr(KeyAscii)) > 10
        If bOK Then
            MsgBox "每页显示记录范围为1-10"
            KeyAscii = 0
        End If
    End If        
    End Sub
      

  2.   

        rs.AddNew
        rs.Fields(1) = "003"
        rs.Fields(2) = "003"
        rs.Fields(3) = "003"
        rs.Update