访问连续子表单重复信息

时间:2013-02-21 13:14:45

标签: ms-access loops subroutine subform continuous-forms

Access Database Form1是一个Continuous Form,它有一个EmployeeID字段,你可以双击它带你到另一个包含Employee信息的表单。为了保留正确的员工,我使用此代码...

Private Sub EmployeeID_DblClick(cancel as integer)
  Dim myID as variant 
  myID = me.EmployeeID

  DoCmd.OpenForm "frm_EmployeeInfo",,,,,,myID
End Sub

这不仅会显示正确的员工信息,还会将数字填充到隐藏的文本框中以保留信息。

在员工表单上有一个带有4个选项卡的TabControl,其中一个选项卡包含一个Continous子表单,我试图填充员工信息,而不是填充的信息(让我们说员工X有8行不同的属性)显示)它重复相同的一个。这是我的子表单代码:

Option Compare Database

Private Sub Form_open(cancel As Integer)
  Dim strConnection, strSQL As String
  Dim conn As ADODB.Connection
  Dim tbl As ADODB.Recordset
  Dim SourceCode As String
  Dim myID As Variant

  Set conn = New ADODB.Connection
  strConnection = "ODBC;Driver={SQLserver};DSN=AccessDatabase;Server=Labor;DATABASE=Source;Trusted_Connection=Yes;"
  conn.Open strConnection

  myID = CInt(Me.OpenArgs)
  SourceCode= Nz(DLookup("[SourceCode]", "Locaton", "[LOC_ID] = Forms!frmUtility![Site].value"), "")

  If SourceCode<> "" Then
    strSQL = "SELECT EmployeeID,BenefitID,DeductionAmount,BenefitAmount,CoverageAmount,EffectiveDate,"
    strSQL = strSQL & "EligibleDate,ExpirationDate FROM "
    strSQL = strSQL & SourceCode & "_EmployeesBenefitsNew WHERE EmployeeID= " & myID
  Else
    strSQL = "SELECT EmployeeID,BenefitID,DeductionAmount,BenefitAmount,CoverageAmount,EffectiveDate,"
    strSQL = strSQL & "EligibleDate,ExpirationDate FROM "
    strSQL = strSQL & "EmployeesBenefitsNew WHERE EmployeeID= " & myID
  End If

  Set tbl = New ADODB.Recordset

  With tbl
    Set .ActiveConnection = conn
    .Source = strSQL
    .LockType = adLockOptimistic
    .CursorType = adOpenKeyset
    .CursorLocation = adUseClient
    .Open
  End With

  With tbl
    On Error Resume Next
      .MoveFirst
      Do Until tbl.EOF
        Me.txtBenefitID.Value = tbl!BenefitID
        Me.txtDeductionAmt.Value = tbl!DeductionAmount
        Me.txtBenefitAmt.Value = tbl!BenefitAmount
        Me.txtCoverageAmt.Value = tbl!CoverageAmount
        Me.txtEffDt.Value = tbl!EffectiveDate
        Me.txtTermDt.Value = tbl!ExpirationDate
        Set Me.Recordset = tbl
        .MoveNext

      Loop
    .Close
  End With



  conn.Close
  Set conn = Nothing
  Set tbl = Nothing

End Sub

有人能否对这种情况有所了解?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要使用数据设置记录集或记录源,不能在不同的行上写入连续的表单,如果您有记录集,则行只显示为不同。

所以

  ''********************
     Set Me.Recordset = tbl
  ''********************

在你的代码中:

Private Sub Form_open(cancel As Integer)
  Dim strConnection, strSQL As String
  Dim conn As ADODB.Connection
  Dim tbl As ADODB.Recordset
  Dim SourceCode As String
  Dim myID As Variant

  Set conn = New ADODB.Connection
  strConnection = "ODBC;Driver={SQLserver};DSN=AccessDatabase;Server=Labor;DATABASE=Source;Trusted_Connection=Yes;"
  conn.Open strConnection

  myID = CInt(Me.OpenArgs)
  SourceCode= Nz(DLookup("[SourceCode]", "Locaton", "[LOC_ID] = Forms!frmUtility![Site].value"), "")

  If SourceCode<> "" Then
    strSQL = "SELECT EmployeeID,BenefitID,DeductionAmount,BenefitAmount,CoverageAmount,EffectiveDate,"
    strSQL = strSQL & "EligibleDate,ExpirationDate FROM "
    strSQL = strSQL & SourceCode & "_EmployeesBenefitsNew WHERE EmployeeID= " & myID
  Else
    strSQL = "SELECT EmployeeID,BenefitID,DeductionAmount,BenefitAmount,CoverageAmount,EffectiveDate,"
    strSQL = strSQL & "EligibleDate,ExpirationDate FROM "
    strSQL = strSQL & "EmployeesBenefitsNew WHERE EmployeeID= " & myID
  End If

  Set tbl = New ADODB.Recordset

  With tbl
    Set .ActiveConnection = conn
    .Source = strSQL
    .LockType = adLockOptimistic
    .CursorType = adOpenKeyset
    .CursorLocation = adUseClient
    .Open
  End With


  ''********************
  Set Me.Recordset = tbl
  ''********************


  conn.Close
  Set conn = Nothing
  Set tbl = Nothing

End Sub