我在SSTAB的每个页面都有一个Spread控件,用于编辑同一个数据库的不同表
采用ADODC做数据源现在有个问题:如果采用单个ADODC的话,所在页面的Spread相应的RecordSource就要改变,但是这个改变的时机应该在哪里呢?我发现以下的代码将几个表搞混了:
=======================================================
Private Sub SSTab1_Click(PreviousTab As Integer)Select Case SSTab1.Tab
Case 0
 Adodc1.RecordSource = "SELECT * FROM `tb01`"
Case 1
 Adodc1.RecordSource = "SELECT * FROM `tb02`"
Case 2
 Adodc1.RecordSource = "SELECT * FROM `tb03`"
Case 3
 Adodc1.RecordSource = "SELECT * FROM `tb04`"
End SelectAdodc1.Refresh
fpSpread1(SSTab1.Tab).RefreshEnd Sub
======================================================
估计是事件执行的顺序是先执行OnClick()时间再转到相应的Tab请问有什么解决方案吗?
谢谢

解决方案 »

  1.   

    要是不能解决的话,就只能用多个ADODC了,可是这样会占用服务器资源。-_-
      

  2.   

    绑定是一件不好控制的事情,我用对象(DLL)来处理了,但对数据表设计有一定要求,另外也涉及到类的写法,不过我是实现了,客户端代码很少,都用DLL来处理了。
      

  3.   

    用ado对象吧,自己动态的用代码处理相应的数据的出和进.方便!用绑定的话,很累的.肯定要用重复的多控件,从效率考虑也不好,占有的资源很大的.
      

  4.   

    "用ado对象吧,自己动态的用代码处理相应的数据的出和进"应该怎么做?我是新手。谢谢
      

  5.   

    这个是一个例子,希望对楼主有帮助.点击网格中相应的,数据的标识,就吧相应的数据填充到相应的控件中,这样做,更/删/插都很灵活,你有很多SSTAB的话,其实也一样的.总毕adodc方便多了  ^_^VERSION 5.00
    Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
    Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
    Begin VB.Form frmjiaoshi 
       AutoRedraw      =   -1  'True
       BorderStyle     =   3  'Fixed Dialog
       Caption         =   "教室信息"
       ClientHeight    =   5265
       ClientLeft      =   45
       ClientTop       =   330
       ClientWidth     =   8850
       Icon            =   "frmjiaoshi.frx":0000
       LinkTopic       =   "Form1"
       LockControls    =   -1  'True
       MaxButton       =   0   'False
       MDIChild        =   -1  'True
       MinButton       =   0   'False
       ScaleHeight     =   7293.867
       ScaleMode       =   0  'User
       ScaleWidth      =   5285.097
       ShowInTaskbar   =   0   'False
       Begin VB.TextBox txtming 
          Appearance      =   0  'Flat
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   14.25
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   375
          Left            =   1800
          MaxLength       =   9
          TabIndex        =   1
          Top             =   1080
          Width           =   1455
       End
       Begin MSFlexGridLib.MSFlexGrid msg1 
          Height          =   3435
          Left            =   5310
          TabIndex        =   9
          Top             =   270
          Width           =   2895
          _ExtentX        =   5106
          _ExtentY        =   6059
          _Version        =   393216
          FixedCols       =   0
          RowHeightMin    =   250
          FocusRect       =   0
          SelectionMode   =   1
          FormatString    =   "^教室名称     |^教室编号  "
          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9.75
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
       End
       Begin ygyPrintView.XPButton cmdqing 
          Height          =   465
          Left            =   630
          TabIndex        =   4
          ToolTipText     =   "清空界面记录"
          Top             =   4410
          Width           =   1095
          _ExtentX        =   1931
          _ExtentY        =   820
          BTYPE           =   3
          TX              =   "清空(&C)"
          ENAB            =   -1  'True
          BeginProperty FONT {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9
             Charset         =   134
             Weight          =   700
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          COLTYPE         =   3
          FOCUSR          =   -1  'True
          BCOL            =   13160660
          FCOL            =   0
          FCOLO           =   0
          MPTR            =   0
          MICON           =   "frmjiaoshi.frx":0CCA
       End
       Begin ygyPrintView.XPButton cmdtian 
          Height          =   465
          Left            =   2231
          TabIndex        =   5
          ToolTipText     =   "添加记录"
          Top             =   4410
          Width           =   1095
          _ExtentX        =   1931
          _ExtentY        =   820
          BTYPE           =   3
          TX              =   "添加(&A)"
          ENAB            =   -1  'True
          BeginProperty FONT {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9
             Charset         =   134
             Weight          =   700
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          COLTYPE         =   3
          FOCUSR          =   -1  'True
          BCOL            =   13160660
          FCOL            =   0
          FCOLO           =   0
          MPTR            =   0
          MICON           =   "frmjiaoshi.frx":0CE6
       End
       Begin ygyPrintView.XPButton cmdshan 
          Height          =   465
          Left            =   3832
          TabIndex        =   6
          ToolTipText     =   "删除记录"
          Top             =   4410
          Width           =   1095
          _ExtentX        =   1931
          _ExtentY        =   820
          BTYPE           =   3
          TX              =   "删除(&D)"
          ENAB            =   -1  'True
          BeginProperty FONT {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9
             Charset         =   134
             Weight          =   700
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          COLTYPE         =   3
          FOCUSR          =   -1  'True
          BCOL            =   13160660
          FCOL            =   0
          FCOLO           =   0
          MPTR            =   0
          MICON           =   "frmjiaoshi.frx":0D02
       End
       Begin ygyPrintView.XPButton cmdgneg 
          Default         =   -1  'True
          Height          =   465
          Left            =   5433
          TabIndex        =   7
          ToolTipText     =   "更新记录"
          Top             =   4410
          Width           =   1095
          _ExtentX        =   1931
          _ExtentY        =   820
          BTYPE           =   3
          TX              =   "更新(&U)"
          ENAB            =   -1  'True
          BeginProperty FONT {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9
             Charset         =   134
             Weight          =   700
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          COLTYPE         =   3
          FOCUSR          =   -1  'True
          BCOL            =   13160660
          FCOL            =   0
          FCOLO           =   0
          MPTR            =   0
          MICON           =   "frmjiaoshi.frx":0D1E
       End
      

  6.   

    接上:Begin ygyPrintView.XPButton cmdtui 
          Height          =   465
          Left            =   7035
          TabIndex        =   8
          ToolTipText     =   "返回"
          Top             =   4410
          Width           =   1095
          _ExtentX        =   1931
          _ExtentY        =   820
          BTYPE           =   3
          TX              =   "确定(&O)"
          ENAB            =   -1  'True
          BeginProperty FONT {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9
             Charset         =   134
             Weight          =   700
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          COLTYPE         =   3
          FOCUSR          =   -1  'True
          BCOL            =   13160660
          FCOL            =   0
          FCOLO           =   0
          MPTR            =   0
          MICON           =   "frmjiaoshi.frx":0D3A
       End
       Begin RichTextLib.RichTextBox txtbei 
          Height          =   1185
          Left            =   1800
          TabIndex        =   3
          ToolTipText     =   "备注信息"
          Top             =   2520
          Width           =   2895
          _ExtentX        =   5106
          _ExtentY        =   2090
          _Version        =   393217
          ScrollBars      =   2
          MaxLength       =   130
          Appearance      =   0
          TextRTF         =   $"frmjiaoshi.frx":0D56
          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   9
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
       End
       Begin RichTextLib.RichTextBox txtbianhao 
          Height          =   375
          Left            =   1785
          TabIndex        =   0
          Top             =   315
          Width           =   1455
          _ExtentX        =   2566
          _ExtentY        =   661
          _Version        =   393217
          Enabled         =   -1  'True
          MultiLine       =   0   'False
          MaxLength       =   9
          Appearance      =   0
          TextRTF         =   $"frmjiaoshi.frx":0DF3
          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   14.25
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
       End
       Begin RichTextLib.RichTextBox txtren 
          Height          =   375
          Left            =   1785
          TabIndex        =   2
          Top             =   1890
          Width           =   1455
          _ExtentX        =   2566
          _ExtentY        =   661
          _Version        =   393217
          Enabled         =   -1  'True
          MultiLine       =   0   'False
          MaxLength       =   3
          Appearance      =   0
          TextRTF         =   $"frmjiaoshi.frx":0E90
          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
             Name            =   "宋体"
             Size            =   14.25
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
       End
       Begin VB.Label lbl1 
          Caption         =   "教室编号"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   10.5
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   285
          Left            =   720
          TabIndex        =   13
          Top             =   450
          Width           =   915
          WordWrap        =   -1  'True
       End
       Begin VB.Label lbl2 
          Caption         =   "教室名称"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   10.5
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   285
          Left            =   720
          TabIndex        =   12
          Top             =   1170
          Width           =   915
          WordWrap        =   -1  'True
       End
       Begin VB.Label lbl3 
          Caption         =   "容纳人数"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   10.5
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   285
          Left            =   720
          TabIndex        =   11
          Top             =   1890
          Width           =   915
          WordWrap        =   -1  'True
       End
       Begin VB.Label lbl4 
          Caption         =   "备 注"
          BeginProperty Font 
             Name            =   "宋体"
             Size            =   10.5
             Charset         =   134
             Weight          =   400
             Underline       =   0   'False
             Italic          =   0   'False
             Strikethrough   =   0   'False
          EndProperty
          Height          =   285
          Left            =   990
          TabIndex        =   10
          Top             =   2520
          Width           =   645
          WordWrap        =   -1  'True
       End
    End
    Attribute VB_Name = "frmjiaoshi"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option ExplicitDim rs As New ADODB.Recordset'读取数据(教室名称,教室编号)放入栅格
    Sub RuMsg1()
        Dim i As Long
        Dim sql As String
        
        msg1.Clear
        msg1.FormatString = "^教室名称     |^教室编号  "
        For i = 1 To msg1.Rows - 1
            msg1.TextMatrix(i, 0) = ""
        Next
        If rs.State <> adStateClosed Then
            rs.Close: Set rs = Nothing
        End If
        sql = "Select * From 教室信息表 Order By 教室编号"
        rs.CursorLocation = adUseClient
        rs.Open sql, Conn, adOpenDynamic, adLockPessimistic
        If Not (rs.EOF = True And rs.bof = True) Then
            msg1.Rows = rs.RecordCount + 1
            For i = 1 To rs.RecordCount
                msg1.TextMatrix(i, 0) = rs.Fields("教室名称").Value
                msg1.TextMatrix(i, 1) = rs.Fields("教室编号").Value
                rs.MoveNext
            Next
        End If
        rs.Close: Set rs = Nothing
    End Sub'清空控件上的数据
    Sub QingKong()
        txtbianhao.Text = ""
        txtming.Text = ""
        txtren.Text = ""
        txtbei.Text = ""
        txtbianhao.SetFocus
    End SubPrivate Sub cmdgneg_Click()
        Dim i As Long
        Dim sql As String
        Dim cmdupdate As New ADODB.Command
        
        If txtbianhao.Text = "" Then
            MsgBox "教室编号不能为空!", vbInformation + vbOKOnly, "提示"
            txtbianhao.SetFocus
            Exit Sub
        End If
        If txtming.Text = "" Then
            MsgBox "教室名称不能为空!", vbInformation + vbOKOnly, "提示"
            txtming.SetFocus
            Exit Sub
        End If
        If txtren.Text = "" Then
            MsgBox "容纳人数不能为空!", vbInformation + vbOKOnly, "提示"
            txtren.SetFocus
            Exit Sub
        End If
        '判断是否有重复的记录
        For i = 1 To msg1.Rows - 1 '判断是否有重复
            If Trim(txtbianhao.Text) = Trim(msg1.TextMatrix(i, 0)) Then
                MsgBox "教室名称不能重复!", vbInformation + vbOKOnly, "提示"
                'txtming.SetFocus
                Exit Sub
            End If
        Next
      

  7.   

    接上:'更新数据
        sql = "Update 教室信息表 Set 教室名称='" & Trim(txtming.Text) & "',容纳人数='" & Trim(txtren.Text) & "',备注='" & txtbei.Text & "' Where 教室编号='" & msg1.TextMatrix(msg1.Row, 1) & "'"
        Set cmdupdate.ActiveConnection = Conn
        cmdupdate.CommandText = sql
        cmdupdate.Execute
        Set cmdupdate = Nothing
        MsgBox "数据更新成功!", vbInformation + vbOKOnly, "提示"
        Call QingKong
        Call RuMsg1
        Call msg1_Click
    '    Call RuForm
    End SubPrivate Sub cmdqing_Click()
        Call QingKong
    End SubPrivate Sub cmdshan_Click()
        Dim Cmddel As New ADODB.Command
        Dim sql As String
        Dim temp As Long       '询问的返回值
        Dim tempmsg As String  '临时询问语句
        
        If msg1.TextMatrix(msg1.Row, 1) = "" Or msg1.TextMatrix(msg1.Row, 0) = "" Then
            Exit Sub
        End If
        tempmsg = "真的要删除教室名称为: " & msg1.TextMatrix(msg1.Row, 0) & "  的记录吗?"
        temp = MsgBox(tempmsg, vbInformation + vbYesNo, "确认删除")
        '删除确认
        If temp = vbNo Then
            Exit Sub
        Else
        '删除数据
            sql = "Delete From 教室信息表 Where 教室名称='" & msg1.TextMatrix(msg1.Row, 0) & "' AND 教室编号='" & msg1.TextMatrix(msg1.Row, 1) & "'"
            Set Cmddel.ActiveConnection = Conn
            Cmddel.CommandText = sql
            Cmddel.Execute
            Set Cmddel = Nothing
        End If
        Call QingKong
        Call RuMsg1
        Call msg1_Click
    '    Call RuForm
    End SubPrivate Sub cmdtian_Click()
        Dim i As Long
        Dim addsql As String
        Dim cmdadd As New ADODB.Command
        
    '    If msg1.TextMatrix(msg1.Rows, 1) <> "" Then
    '        MsgBox "教室信息已经满了,不可以再添加!", vbOKOnly + vbInformation, "提示"
    '        Exit Sub
    '    End If
        If txtbianhao.Text = "" Then
            MsgBox "教室编号不能为空!", vbInformation + vbOKOnly, "提示"
            txtbianhao.SetFocus
            Exit Sub
        End If
        If txtming.Text = "" Then
            MsgBox "教室名称不能为空!", vbInformation + vbOKOnly, "提示"
            txtming.SetFocus
            Exit Sub
        End If
        If txtren.Text = "" Then
            MsgBox "容纳人数不能为空!", vbInformation + vbOKOnly, "提示"
            txtren.SetFocus
            Exit Sub
        End If
        
        '判断是否有重复的记录
        For i = 1 To msg1.Rows - 1 '判断是否有重复
            If Trim(txtming.Text) = Trim(msg1.TextMatrix(i, 0)) Then
                MsgBox "教室名称不能重复。", vbInformation + vbOKOnly, "提示"
                txtming.SetFocus
                Exit Sub
            End If
        Next
        For i = 1 To msg1.Rows - 1 '判断是否有重复
            If Trim(txtbianhao.Text) = Trim(msg1.TextMatrix(i, 1)) Then
                MsgBox "教室编号不能重复!", vbInformation + vbOKOnly, "提示"
                txtbianhao.SetFocus
                Exit Sub
            End If
        Next    '添加数据
        addsql = "Insert Into 教室信息表(教室编号,教室名称,容纳人数,备注) Values('" & Trim(txtbianhao.Text) & "','" & Trim(txtming.Text) & "','" & Trim(txtren.Text) & "','" & txtbei.Text & "')"
        Set cmdadd.ActiveConnection = Conn
        cmdadd.CommandText = addsql
        cmdadd.Execute
        Set cmdadd = Nothing
        Call RuMsg1
    '    Call RuForm
        Call QingKong
        txtbianhao.SetFocus
    End SubPrivate Sub cmdtui_Click()
        Unload Me
    End SubPrivate Sub Form_Deactivate()
        Unload Me
    End SubPrivate Sub Form_Load()
        Call RuMsg1
        If msg1.TextMatrix(msg1.Row, 1) = "" Then
            MsgBox "数据库暂时没有数据!", vbInformation + vbOKOnly, "提示!"
        End If
    '    Call RuForm
        Call msg1_Click
    End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
         MDIForm1.sta1.Panels(1).Text = "就绪"
    End SubPrivate Sub msg1_Click()
        Dim sql As String    If rs.State <> adStateClosed Then
            rs.Close
        End If
        '根据教室编号和教室名称将数据传入对于的控件
        sql = "Select * From 教室信息表 Where 教室编号='" & msg1.TextMatrix(msg1.Row, 1) & "'"
        rs.CursorLocation = adUseClient
        rs.Open sql, Conn, adOpenDynamic, adLockPessimistic
        If Not (rs.EOF = True And rs.bof = True) Then
            txtbianhao.Text = IIf(IsNull(rs.Fields("教室编号").Value), "", rs.Fields("教室编号").Value)
            txtming.Text = IIf(IsNull(rs.Fields("教室名称").Value), "", rs.Fields("教室名称").Value)
            txtren.Text = IIf(IsNull(rs.Fields("容纳人数").Value), "", rs.Fields("容纳人数").Value)
            txtbei.Text = IIf(IsNull(rs.Fields("备注").Value), "", rs.Fields("备注").Value)
        Else
            Exit Sub
        End If
        rs.Close: Set rs = Nothing
    End SubPrivate Sub txtbianhao_KeyPress(KeyAscii As Integer)
        KeyAscii = ValiText(KeyAscii, "0123456789-abcdefghijklmnopqrstuvwxyz", True)
    End SubPrivate Sub txtren_KeyPress(KeyAscii As Integer)
        KeyAscii = ValiText(KeyAscii, "0123456789", True)
    End SubPrivate Sub txtbianhao_GotFocus()
        txtbianhao.SelStart = 0
        txtbianhao.SelLength = Len(txtbianhao.Text)
    End SubPrivate Sub txtming_GotFocus()
        txtming.SelStart = 0
        txtming.SelLength = Len(txtming.Text)
    End SubPrivate Sub txtren_GotFocus()
        txtren.SelStart = 0
        txtren.SelLength = Len(txtren.Text)
    End Sub