【问题标题】:Find the highest value within a specified range of values查找指定值范围内的最大值
【发布时间】:2022-01-04 02:06:20
【问题描述】:

我有一个 MS Access 表单,我在其中输入了一个代表每条记录的 SECTION 的值。然后我想根据该部分下其他小节的值以编程方式计算其 SUBSECTION 的值。

例如:

  • 第一条记录:Section = 1,Subsection = 1.00
  • 第二条记录:Section = 1,Subsection = 1.01
  • 第三条记录:Section = 2,Subsection = 2.00
  • 第 4 条记录:Section = 2,Subsection = 2.01
  • 第 5 条记录:Section = 2,Subsection = 2.02
  • 第 6 条记录:Section = 3,Subsection = 3.00

所以,例如,当我创建一个节值为 2 的新记录时,我想:

  • 查看第2节对应的小节值(即小数点前为2的小节),
  • 确定当前的最高分段值是多少(此处为 2.02),
  • 将第 2 节下的下一个增量值分配给变量,在本例中,下一个增量为 2.03。

对于解决此问题的最佳方法有什么建议或建议吗? tia

【问题讨论】:

  • 到目前为止你尝试过什么代码?你在哪里遇到了麻烦?请在您的问题中包含这一点。
  • 我试图找出最好的方法是什么。我看过 DMax 和 DLookup,但想知道 Between...And 运算符是否更好。

标签: sql vba ms-access


【解决方案1】:

如果您将自动编号 ID 添加到表中,您可以使用我的项目 VBA.RowNumbers 中的 RowNumber 函数,如下所示:

SELECT 
    Section, 
    Section + RowNumber(CStr([ID]), CStr([Section]))/100 AS SubSection
FROM 
    YourTable
WHERE 
    RowNumber(CStr([ID])) <> RowNumber("", "", True);

例子:

' Builds consecutive row numbers in a select, append, or create query
' with the option of a initial automatic reset.
' Optionally, a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query having an ID with an index):
'   SELECT RowNumber(CStr([ID])) AS RowID, *
'   FROM SomeTable
'   WHERE (RowNumber(CStr([ID])) <> RowNumber("","",True));
'
' Usage (typical select query having an ID without an index):
'   SELECT RowNumber(CStr([ID])) AS RowID, *
'   FROM SomeTable
'   WHERE (RowNumber("","",True)=0);
'
' Usage (with group key):
'   SELECT RowNumber(CStr([ID]), CStr[GroupID])) AS RowID, *
'   FROM SomeTable
'   WHERE (RowNumber(CStr([ID])) <> RowNumber("","",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
'   Call RowNumber(vbNullString, True)
' 2. Run query:
'   INSERT INTO TempTable ( [RowID] )
'   SELECT RowNumber(CStr([ID])) AS RowID, *
'   FROM SomeTable;
'
' Usage (typical append query, automatic reset):
'   INSERT INTO TempTable ( [RowID] )
'   SELECT RowNumber(CStr([ID])) AS RowID, *
'   FROM SomeTable
'   WHERE (RowNumber("","",True)=0);
'
' 2020-05-29. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RowNumber( _
    ByVal Key As String, _
    Optional ByVal GroupKey As String, _
    Optional ByVal Reset As Boolean) _
    As Long
    
    ' Uncommon character string to assemble GroupKey and Key as a compound key.
    Const KeySeparator      As String = "¤§¤"
    ' Expected error codes to accept.
    Const CannotAddKey      As Long = 457
    Const CannotRemoveKey   As Long = 5
  
    Static Keys             As New Collection
    Static GroupKeys        As New Collection

    Dim Count               As Long
    Dim CompoundKey         As String
    
    On Error GoTo Err_RowNumber
    
    If Reset = True Then
        ' Erase the collection of keys and group key counts.
        Set Keys = Nothing
        Set GroupKeys = Nothing
    Else
        ' Create a compound key to uniquely identify GroupKey and its Key.
        ' Note: If GroupKey is not used, only one element will be added.
        CompoundKey = GroupKey & KeySeparator & Key
        Count = Keys(CompoundKey)
        
        If Count = 0 Then
            ' This record has not been enumerated.
            '
            ' Will either fail if the group key is new, leaving Count as zero,
            ' or retrieve the count of already enumerated records with this group key.
            Count = GroupKeys(GroupKey) + 1
            If Count > 0 Then
                ' The group key has been recorded.
                ' Remove it to allow it to be recreated holding the new count.
                GroupKeys.Remove (GroupKey)
            Else
                ' This record is the first having this group key.
                ' Thus, the count is 1.
                Count = 1
            End If
            ' (Re)create the group key item with the value of the count of keys.
            GroupKeys.Add Count, GroupKey
        End If

        ' Add the key and its enumeration.
        ' This will be:
        '   Using no group key: Relative to the full recordset.
        '   Using a group key:  Relative to the group key.
        ' Will fail if the key already has been created.
        Keys.Add Count, CompoundKey
    End If
    
    ' Return the key value as this is the row counter.
    RowNumber = Count
  
Exit_RowNumber:
    Exit Function
    
Err_RowNumber:
    Select Case Err
        Case CannotAddKey
            ' Key is present, thus cannot be added again.
            Resume Next
        Case CannotRemoveKey
            ' GroupKey is not present, thus cannot be removed.
            Resume Next
        Case Else
            ' Some other error. Ignore.
            Resume Exit_RowNumber
    End Select

End Function

【讨论】:

  • 谢谢,Gustav,我会努力解决这个问题的。
【解决方案2】:

以下是我的建议。

假设主表叫table_1,节表叫table_session,添加记录的表格叫frmmainform

  1. 在表单上,​​您有以下控件

A.带有行源的组合框 1 是节列表,即 1、2、3 等,您可以命名节,下面可以有一个 sql 语句 选择 DISTINCTROW 会话名称 FROM 表会话;

B.组合框 2,名称为 subsectionlook(这是根据步骤 A 中组合框 1 中选择的会话名称查找子部分的最后一条记录),行源是 table_1 中的 select 语句,如 SELECT SELECT Max(小节) 从表_1 其中 sessionname =Forms![frmmainform]![session]

现在表单上有两个组合框,我们可以根据当前表单中的会话字段选择会话名称(从组合框中)和子部分的最后一条记录。

接下来创建一个名为 subsection 的文本框字段

最后的步骤将在 vba 代码中,执行以下操作

私有子会话_AfterUpdate()

    subsectionlook.requery

私有子小节look_AfterUpdate()

    subsection.text= subsectionlook.value +0.01

【讨论】:

    【解决方案3】:

    这里有一个解决方案:

    Private Sub cmdCreateSubsection_Click()
    
        If IsNull(Me.txtBoxSection) Then
            MsgBox "Please enter number in the Section Box", vbOKOnly + vbInformation, "Section"
            Me.txtBoxSection.SetFocus
            Exit Sub
        End If
        
        Me.txtBoxSection = CLng(Me.txtBoxSection)
         
        Dim myval As Double
        myval = Nz(DMax("Subsection", "t_subsection_tracker", "Section = " & [Forms]![f_subsection_tracker]![txtBoxSection]), 0)
        
        If myval = 0 Then
            Me.txtBoxSubsection = Me.txtBoxSection
        Else
            Me.txtBoxSubsection = myval + 0.01
        End If
        
        Dim myset As DAO.Recordset
        Set myset = CurrentDb.OpenRecordset("Select * from t_subsection_tracker where ID = -1", dbOpenDynaset)
        myset.AddNew
        myset!Section = Me.txtBoxSection
        myset!Subsection = Me.txtBoxSubsection
        myset.Update
        myset.Close
        Set myset = Nothing
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2020-07-29
      • 2016-08-31
      相关资源
      最近更新 更多