1

Access 数据库 Form1 是一个具有 EmployeeID 字段的连续表单,您可以双击以转到另一个包含有关员工信息的表单。为了留住正确的员工,我使用此代码...

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

任何人都可以对这种情况有所了解吗?谢谢!

4

1 回答 1

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
于 2013-02-21T14:05:36.547 回答