【问题标题】:How to assign the next incremented alpha-numeric string value in a text box using VBA Access?如何使用 VBA Access 在文本框中分配下一个增量字母数字字符串值?
【发布时间】:2018-04-22 05:14:18
【问题描述】:

目前,我有一个数据表,其中包含以下数据作为示例:

FieldOne       FieldTwo
TestOne.01     TestOne.01.01
TestOne.01     TestOne.01.02
TestTwo.02     TestTwo.02.01
TestThree.03   TestThree.03.03
TestFour.02    TestFour.02.02
TestFour.02    TestFour.02.03

我有一个包含组合框的表单。组合框的值是 FieldOne 中的所有值。我在相同的表单上还有一个文本框,在从组合框中选择一个值后,将该特定记录的 FieldTwo 中的下一个递增值分配到文本框中。

例如,如果我从组合框中选择值TestThree.03,我想用TestThree.03.04 填充文本框。如果我选择值TestTwo.02,文本框应该填充TestTwo.02.02

到目前为止我尝试过的内容:

Dim frmDS As SubForm
Dim criteria As String

Set frmDS = getSubForm("form", "datasheet")

criteria = "FieldOne = '" & Me.comboBox & "'"

With frmDS.Form
    .RecordsetClone.FindFirst criteria

    If Not .RecordsetClone.NoMatch Then
        MsgBox .RecordsetClone.Fields("FieldTwo")
    End If
End With

但是我面临两个难题:

  1. 如果我选择TestOne.01,由于有2条记录包含TestOne.01,它将显示TestOne.01.01而不是lastTestOne.01.02
  2. 我不确定如何提取 FieldTwo 值中最右边小数点后的数字,将其递增,然后将其附加回FieldOne 以便将其分配给文本框

我不确定我最初的方法是否朝着正确的方向前进。

我该如何克服这些问题?

谢谢

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    对于问题1,答案很简单

    而不是.RecordsetClone.FindFirst criteria

    使用.RecordsetClone.FindLast criteria

    对于问题 2,您只需要一点 VBA 代码即可首先获取最后一个代码,然后将其格式化以追加。

    您没有提供格式的所有详细信息,因此我将假设最简单的解决方案 - 即最后一个代码的长度为 2 位。如果没有,您可以使用 Instr 和 Mid 或 Right 函数的变体来查找最后一个代码

    例子:

    Dim intLastCode as Integer
    Dim strLastCode as String
    
    With frmDS.Form
        ' Get last two digits from Field 2
        strLastCode = Right(.RecordsetClone.Fields("FieldTwo"),2)
        intLastCode = CInt(strLastCode) + 1
    
        ' Format and append incremented value to Field 1
        MyTextBox.Value = .RecordsetClone.Fields("FieldOne") & "." & Format(intLastCode,"00")
    End With
    

    【讨论】:

    • 优秀 - 感谢您的快速反馈和投票!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多