代码之家  ›  专栏  ›  技术社区  ›  Alfredo A.

返回窗体后,DataCombo未获得刷新

  •  0
  • Alfredo A.  · 技术社区  · 6 年前

    我有一个非常古老的vb6 winforms应用程序。它有一个非常简单的初始窗口,显示一个组合框,其中包含使用adodb 2.8从ms sql服务器检索到的一些数据。它有一个复选框来定义如何从服务器中筛选检索到的数据。

    加载表单时,组合框的内容将被正确添加。不过,我还有一个按钮,它显示一个新窗口,其中包含与组合框中选定项相关的数据。一旦我关闭另一个窗口,我将重新打开原始窗口,并且组合框为空,即使 形式载荷 方法被执行,因此, 检查1-点击 方法也被执行。不过,如果我点击了复选框, 检查1-点击 事件被触发并执行,确定并填充组合框。

    当第一次加载窗口时, 小精灵 执行并正确填充组合。每一次 检查1-点击 ,记录集包含正确的信息,只是返回到窗口时,它不会显示在组合框中。

    在窗口重新打开后,是否需要采取任何步骤来刷新组合框的内容?

    组合框的类型为 msdatalistlib.datacombo

    click方法是这样的。

    Private Sub Check1_Click()
        Dim SQL As String
    
        'If checked'
        If Me.Check1.Value = 1 Then
            SQL = "SELECT TABLE1.NAME, TABLE1.LASTNAME, TABLE1.NAME + TABLE1.LASTNAME AS FULLNAME " & _ 
                  " WHERE TABLE1.ACTIF = 'Y' ORDER BY TABLE1.NAME, TABLE1.LASTNAME"
        'If not checked'
        Else
            SQL = "SELECT TABLE1.NAME, TABLE1.LASTNAME, TABLE1.NAME + TABLE1.LASTNAME AS FULLNAME " & _ 
                  " ORDER BY TABLE1.NAME, TABLE1.LASTNAME"
        End If
    
        Disconnect
        Set Cn = Connect
        Set objRec = CloseRecordSet(objRec)
        Set objRec = New ADODB.Recordset
        With objRec
            .CursorLocation = adUseServer
            .Open SQL, Cn, adOpenStatic, adLockOptimistic, adCmdText
        End With
        With Me.FullName
            Set .RowSource = objRec
            .ListField = "FULLNAME"
            .DataField = "FULLNAME"
        End With
        Me.FullName.Refresh
        Me.Refresh
    End Sub
    

    这是其余方法的实现

    'Connexion ADODB'
    Public adoDbConn As ADODB.Connection
    
    
    Private Sub Form_Load()
        Check1_Click
        Me.Refresh
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Set objRec = CloseRecordSet(objRec)
        Disconnect
        Set Cn = Nothing
    
        With Me.FullName
            Set .RowSource = Nothing
            .ListField = ""
            .DataField = ""
        End With
        Me.FullName.Refresh
        Me.Refresh
    End Sub 
    
    
    Sub Disconnect()
        If Not adoDbConn Is Nothing Then
            If adoDbConn.State = adStateOpen Then adoDbConn.Close
        End If
        Set adoDbConn = Nothing
    End Sub
    
    Public Function Connect() As ADODB.Connection
        If adoDbConn Is Nothing Then        
            Set adoDbConn = New ADODB.Connection        
            ' Specify the OLE DB provider.'
            adoDbConn.Provider = "sqloledb"
            adoDbConn.Mode = adModeReadWrite
            ' Specify connection string on Open method.'
            adoDbConn.Open ChaineConnexion      
        End If      
        Set Connect = adoDbConn     
    End Function
    
    Public Function CloseRecordSet(rs As ADODB.Recordset) As Object
        If Not rs Is Nothing Then
            If rs.State = adStateOpen Then rs.Close
        End If
        Set CloseRecordSet = Nothing
    End Function    
    

    更新

    加载详细信息窗口的方法

        'Showing the Detail Window'
    Private Sub OpenFormDetail_Click()
    
        'If none is selected'
        If Me.FullName = "" Then
            'Show warning'
            Ret = MsgBox("Select one element!", vbExclamation, "ATTENTION !")
            Me.FullName.SetFocus
        Else
    
            'Storing fullname to use in detail other window'
            Globale.FullName = Right(Me.FullName, 11)
    
            'Load Detail Window'
            Load frmDetailWindow
            frmDetailWindow.Show
    
            'Unloads this window'
            Unload Me
    
        End If
    End Sub
    

    方法的详细信息窗口,该窗口关闭并重新打开原始窗口。

        'Showing the original Window'
    Private Sub Return_Click()
    
        'Open initial window'
        Load frmInitialWindow
        frmInitialWindow.Show
    
        'Closes Detail Window'
        Unload frmDetailWindow
    
    End Sub
    
    0 回复  |  直到 6 年前