我在sql server2000下建立一个表。想把表中数据写入MSHFLEXGRID控件和DataGrid控件当中。请问改如何编写?

解决方案 »

  1.   

    dim cnns as adodb.connection
    dim rs as adodb.recordset
    dim Sql as string      sql = "Provider=sqloledb;" & _
          "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
       Set cnn1 = New ADODB.Connection
       cnn.Open strCnn
    '''首先初始化msf
    With Msf
        .Clear
        .Rows = 2
        
        '清空第一行的数据
        .Row = 1
        For i = 0 To .Cols - 1
            .Col = i
            .Text = ""
        Next
        .Cols = 8
        '''定义列的宽度
        .ColWidth(0) = 1500
        .ColWidth(1) = 1000
        .ColWidth(2) = 500
        .ColWidth(3) = 1200
        .ColWidth(4) = 2000
        .ColWidth(5) = 1200
        .ColWidth(6) = 1200
        .ColWidth(7) = 1200
        '''''''''''''''''''
        .TextMatrix(0, 0) = "个人编码"
        .TextMatrix(0, 1) = "姓名"
        .TextMatrix(0, 2) = "性别"
        .TextMatrix(0, 3) = "出生日期"
        .TextMatrix(0, 4) = "身份证号"
        .TextMatrix(0, 5) = "登记人"
        .TextMatrix(0, 6) = "登记日期"
        .TextMatrix(0, 7) = "家庭类型"
    End With
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''生成查询的sql语句
    Sql = "select * from rydj where grbm<>'0'"Set Rs = New ADODB.RecordsetRs.Open Sql, Cnns, adOpenForwardOnly, adLockReadOnlyR = 1
    If Not Rs.EOF Then
        Msf.Rows = Rs.RecordCount + 1
    End If
    With Msf
        While Not Rs.EOF
            
            .Row = R
            ''个人编码
            .TextMatrix(R, 0) = IIf(IsNull(Rs!grbm), "", Rs!grbm)
            .TextMatrix(R, 1) = IIf(IsNull(Rs!xm), "", Rs!xm) ''姓名
            
            ''性别
            Select Case Rs!xb
                Case 0
                    .TextMatrix(R, 2) = "男"
                Case 1
                    .TextMatrix(R, 2) = "女"
                Case Else
                    .TextMatrix(R, 2) = "不详"
            End Select
            ''出生日期
            .TextMatrix(R, 3) = IIf(IsNull(Rs!csrq), "", Rs!csrq)
            ''身份证号
            .TextMatrix(R, 4) = IIf(IsNull(Rs!sfzh), "", Rs!sfzh)
            '''登记人
            .TextMatrix(R, 5) = IIf(IsNull(Rs!djr), "", Rs!djr)
            '''登记日期
            .TextMatrix(R, 6) = IIf(IsNull(Rs!djrq), "", Rs!djrq)
            '''家庭类型
            Select Case Rs!jtlx
                Case 0
                    .TextMatrix(R, 7) = "普通家庭"
                Case 1
                    .TextMatrix(R, 7) = "特困家庭"
                Case 2
                    .TextMatrix(R, 7) = "五保家庭"
                Case Else
                    .TextMatrix(R, 7) = "不详"
            End Select
            
            R = R + 1
            Rs.MoveNext
        Wend
    End WithRs.Close
    Set Rs = NothingEnd Sub
      

  2.   

    Adodc1.ConnectionString = 连接字符串
    Adodc1.CommandType = adCmdText
    Adodc1.RecordSource = "select * from 表"
    Adodc1.Refresh
      

  3.   

    set datagrid1.datasource=adodc1.recordset
      

  4.   

    工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)    Dim CN   As New ADODB.Connection                '定义数据库的连接
        Dim Rs   As New ADODB.Recordset
        CN.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa;"
        CN.Open
        Rs.CursorLocation = adUseClient
        Rs.Open "select * from employees", CN, adOpenDynamic, adLockOptimistic
       Set DataGrid1.DataSource = Rs
      

  5.   

    '工程--->引用--->Microsoft ActiveX Data Object 2.5(版本号)    Dim CN   As New ADODB.Connection                '定义数据库的连接
        Dim Rs   As New ADODB.Recordset
        CN.ConnectionString = "Provider=sqloledb;Data Source=服务器名;Initial Catalog=数据库;User Id=用户名;Password=密码;"
        CN.Open
        Rs.CursorLocation = adUseClient
        Rs.Open "select * from employees", CN, adOpenDynamic, adLockOptimistic
      Set MSHFlexGrid1.DataSource = Rs
      

  6.   

    MSHFLEXGRID控件和DataGrid这两种表格控件你选一个就行了,难道两种都要用!?
    DataGrid 绑定!!
    MSHFLEXGRID如下:
    MSFlexGrid1.Rows = 10: MSFlexGrid1.Cols = 5
        
        For i = 1 To MSFlexGrid1.Rows - 1
            MSFlexGrid1.TextMatrix(i, 1) = "aaaa" & i
        Next
      

  7.   

    使用绑定
    set mshflexgrid1.datasource=adodc1.recordset
      

  8.   

    Set MSHFlexGrid1.DataSource = Rs
      

  9.   

    Dim CN  As New ADODB.Connection                '定义数据库的连接
    Dim Rs   As New ADODB.Recordset
    CN.ConnectionString="server=服务器名;database=数据库名;User id=sa;Password=sa;"
    CN.Open
    Rs.CursorLocation = adUseClient
    Rs.Open "select * from tableName", CN, adOpenDynamic, adLockOptimistic
    Set DataGrid1.DataSource = Rs
    或 set mshflexgrid1.datasource = rs
      

  10.   

    还是没有解决。我的代码如下:'工程--->引用--->Microsoft ActiveX Data Object 2.6(版本号)
    Private Sub Command1_Click()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sql As String
    Set cn = New ADODB.Connection
    cn.Open "Provider=sqloledb;Data Source=网络;Initial Catalog=office;User Id=sa;Password="
    sql = "select * from 部门分类"
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.Open sql, cn, adOpenDynamic, adLockOptimistic
    Set MSFlexGrid1.DataSource = rs
    rs.Close
    Set rs = Nothing
    End Sub结果 Set MSFlexGrid1.DataSource = rs 运行出错,对话框显示:对象变量或with块变量未设置
    请问大家所写的是用纯代码编写的吗?还是有什末控件?为何运行不行呢?还请帮忙
      

  11.   

    Private Sub Command1_Click()
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim sql As String
    cn.Open "Provider=sqloledb;Data Source=网络;Initial Catalog=office;User Id=sa;Password="
    sql = "select * from 部门分类"
    rs.Open sql, cn, adOpenDynamic, adLockOptimistic
    Set MSFlexGrid1.DataSource = rs
    rs.Close
    End Sub
      

  12.   

    SKer(海) 
    不行呀,还是报同样的错
      

  13.   

    to  ukyoxh(我来学习):
    你的msf 是什么意思?
      

  14.   

    Dim CN   As New ADODB.Connection                '定义数据库的连接
        Dim Rs   As New ADODB.Recordset
     Cn.CursorLocation = adUseClient
     Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=数据库存文件路径;User ID=admin;Password=;Jet OLEDB:Database Password=密码  'User ID 注意这里的空格
        
       Rs.Open "select * from employees", CN, adOpenDynamic, adLockOptimistic
       Set DataGrid1.DataSource = Rs '用datagrid
       set msf.datasource=Rs  '用mshflexgrid