【发布时间】:2019-04-15 01:19:21
【问题描述】:
我在自动递增字母方面需要帮助。
Table1 中的描述字段具有如下值:B39
这个Table1 Record,在Table2中有相关记录:
B39_a
B39_b
B39_c
B39_d
我要做的就是让Table2中的描述自动获取table1的记录并添加特定的字母。它总是以“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 Record )具有默认值:
=IncrementString(DLast("[SeqNo]","[table2]"))
但就像我说的那样,它通过加倍来增加数量。我还必须通过输入“a”来手动启动该过程。
【问题讨论】:
-
我不会依赖 DLast() 来返回正确的值。记录没有内在的顺序。我输入了“B39_zzz”,函数返回“B39`aaa”。您是否进行过单步调试?
-
这是一个多用户数据库吗?请注意,如果不立即提交到表,多个用户可能会生成相同的 SeqNo。
-
这个关系中的主/外键字段是什么? Table2中真的需要重复前缀吗?
-
这是一个单用户数据库。顺便说一句,它是一个 GeoData 数据库。一个 Record(B39 - main coordiante) 有几个称为 B39_a 等的子坐标... Table2 中有 table1 的外键。所以可以相互关联:
-
这并没有真正告诉我关键字段是什么。为什么在表 2 中重复 B39?如果没有将 B39_ 前缀作为传递给函数的值的一部分,Lee Mac 的函数将起作用。那么还有其他前缀吗?