ms-access - 自动递增字母到特定数字

标签 ms-access vba

我需要有关自动递增字母的帮助。

表 1 中的描述字段具有如下值:B39

这个Table1记录,在Table2有相关记录:

B39_a
B39_b
B39_c
B39_d

我只想让表2中的描述自动取表1中的记录并添加特定的字母。它总是以“a”开头,永远不会达到完整的字母表。

我已经尝试过这个网站的一些代码:http://www.freevbcode.com/ShowCode.asp?ID=5440

  Function IncrementString(ByVal strString As String) As String
  '
  ' Increments a string counter
  ' e.g.  "a" -> "b"
  '             "az" -> "ba"
  '       "zzz" -> "aaaa"
  '
  ' strString is the string to increment, assumed to be lower-case alphabetic
  ' Return value is the incremented string
  '

Dim lngLenString As Long
Dim strChar As String
Dim lngI As Long

lngLenString = Len(strString)

' Start at far right
For lngI = lngLenString To 0 Step -1

' If we reach the far left then add an A and exit
If lngI = 0 Then
   strString = "a" & strString
   Exit For
End If

' Consider next character
strChar = Mid(strString, lngI, 1)
If strChar = "z" Then
  ' If we find Z then increment this to A
  ' and increment the character after this (in next loop iteration)
  strString = Left$(strString, lngI - 1) & "a" & Mid(strString, lngI + 1, lngLenString)
Else
  ' Increment this non-Z and exit
  strString = Left$(strString, lngI - 1) & Chr(Asc(strChar) + 1) & Mid(strString, lngI + 1, lngLenString)
  Exit For
End If

 Next lngI

 IncrementString = strString
 Exit Function

 End Function

显然它没有正常工作。它增加了字母,但增加了两倍! (i, i, j, j, 等等)

描述文本框(对于 Table2 记录)具有默认值:

 =IncrementString(DLast("[SeqNo]","[table2]"))

但就像我说的那样,它通过加倍来增加数量。我还必须通过输入“a”手动启动该过程。

最佳答案

函数和调用代码目前都不允许使用“A##_”前缀。如果您真的必须将此前缀保存到 Table2,则必须调整代码来处理它。按照原样,建议不要将“A##”组标识符保存为表 2 中的前缀。使用连接 PK/FK 字段上的表的查询来检索相关数据以进行导出。

DLast() 搜索必须考虑“A##”组标识符,因为每个组都会重复该序列。

不幸的是,尝试使用依赖于主窗体 ID 的动态参数设置 DefaultValue 属性是不切实际的。一方面,子表单在主表单之前加载,因此无法构建默认值,因为主表单数据和控件不可用。此外,当主窗体移动到新记录时,同样没有用于构建默认值的数据。结果是新记录行上的控件显示错误。

使用 PK/FK 字段进行搜索。

在当前事件子窗体中调用递增函数的代码:

If Me.NewRecord And Not IsNull(Me.Parent.ReferenzNR) Then
    Me!SerienBezeichnung = IncrementString(Nz(DLast("SerienBezeichnung", "tbl_GrundminenSerie", "ID_FK=" & Me.Parent.ReferenzID), ""))
End If

请注意,DLast() 即使现在可以工作,最终也可能会失败,因为记录没有固有的顺序。替代方案可能涉及记录集或嵌套域聚合。在 VBA 即时窗口中测试的示例:
?DMax("SerienBezeichnung","tbl_GrundminenSerie","ID_FK=5 AND Len([SerienBezeichnung])="& DMax("Len([SerienBezeichnung])","tbl_GrundminenSerie","ID_FK=5"))

或者如果你觉得自动编号 PK 可以依赖于总是增加(这一直是我的观察,尽管不能保证自动编号):
?DLookup("SerienBezeichnung","tbl_GrundminenSerie","ID_FK=5 AND SerienID="& DMax("SerienID","tbl_GrundminenSerie","ID_FK=5"))

关于ms-access - 自动递增字母到特定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55525189/

相关文章:

windows - 如何在 Ruby 中将字符串从 windows-1252 转换为 utf-8?

regex - 使用 RegEx 搜索 VBA 源代码

Excel VBA : Is it possible to get to the "New-School" print preview via VBA?

ms-access - 源具有密码时的 MS Access DoCmd.Transferdatabase

sql-server - 为什么 Access/SQL Server 中的零日期是 1899-12-30,而不是 12/31?

ms-access - 在 VBA/Access 中,更改记录时从当前记录获取值

c# - 如何使用设计器生成的数据表手动设置自动增量列的 ID?

excel - 仅复制一个范围中的文本并仅将前三个文本粘贴到另一张纸上

javascript - 查找通过 JavaScript 函数传递的变量的值

vba - 将声明为整数的变量分配给公式