【问题标题】:Access Continuos Subform Repeating Information访问 Continuos 子表单重复信息
【发布时间】:2013-02-06 19:57:47
【问题描述】:

Access 数据库 Form1 是一个连续表单,它有一个 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,其中一个选项卡包含一个连续子表单,我试图填充员工信息,但不是向下填充信息(假设员工 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

谁能解释一下这种情况?谢谢!

【问题讨论】:

  • 这是 mdb/accdb 还是 adp?
  • 那为什么不链接表来避免这些问题呢?您可以将记录源设置为完整的记录集并使用链接子/链接主字段进行过滤。
  • 因为这些表与 Access 数据库位于不同服务器上的 SQL 服务器中。我继承了这个程序,我没有构建它,我无法以这种方式链接信息。

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


【解决方案1】:

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

所以

  ''********************
     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

【讨论】:

    猜你喜欢
    • 2014-09-17
    • 2020-08-15
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多