基于索引号和其他标准的MS Access更新查询

时间:2018-05-12 01:54:39

标签: ms-access sql-update access

我正在尝试为我正在为我的工作工作的数据库做一个函数。我不是最熟练的Access,所以如果我不以最好的方式措辞,我会道歉。

我要做的是创建一个模拟行为的查询/宏

enter image description here

结果如下:

enter image description here

逻辑如下

1)对于每条记录 - 在StdName中获取字符串的LEN。获取该字符数并将其更新到“名称”字段。 LEN之后的剩余字符被移动到'SuffixString'字段

2)对于每条记录 - 计算“StdName”字段中字符串的出现次数,用于索引编号之前或之前的任何记录,并使用已存在的任何内容更新“名称”字段并与“_n”连接“其中n是出现次数

示例:索引1 - 在记录1和记录1之间的StdName字段中出现一次'Car1'索引1'名称'更改为Car1_1

示例:索引2 - 在记录1和记录2之间的StdName字段中出现两次“Car1”索引2“名称”更改为Car1_2

示例:索引6 - 在记录1和记录6之间的StdName字段中出现一次'Car3'索引6'名称'更改为Car3_1

使用访问查询可以完成这样的事情吗?我以前从未在Access中开发过,而且我的老板真的希望看到这个函数保持在内部访问,而不是被移出excel。

(我已经设置了第1步,以便稍后输入StdName与Name不匹配的逻辑。例如:Name的“Car1_1”和StdName“Car2”。我意识到我可以将StdName与第2步中的函数连接起来我描述的这个例子,但我有这样做的真实世界目的)

这将在MDB格式上完成

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用我的 RowCounter 功能:

SELECT RowCounter(CStr([Index]),False,[StdName])) AS RowID, *
FROM YourTable
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));

或:

SELECT [StdName] & "_" & CStr(RowCounter(CStr([Index]),False,[StdName]))) AS RankedName, *
FROM YourTable
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));

修改 - 更新

UPDATE s_before
SET [Name] = [StdName] & "_" & CStr(RowCounter(CStr([Index]),False,[StdName])) 
WHERE (RowCounter(CStr([Index]),False) <> RowCounter("",True));

代码:

Public Function RowCounter( _
  ByVal strKey As String, _
  ByVal booReset As Boolean, _
  Optional ByVal strGroupKey As String) _
  As Long

' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' Usage (with group key):
'   SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",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 RowCounter(vbNullString, False)
' 2. Run query:
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.
' 2010-08-04. Corrected that group key missed first row in group.

  Static col      As New Collection
  Static strGroup As String

  On Error GoTo Err_RowCounter

  If booReset = True Then
    Set col = Nothing
  ElseIf strGroup <> strGroupKey Then
    Set col = Nothing
    strGroup = strGroupKey
    col.Add 1, strKey
  Else
    col.Add col.Count + 1, strKey
  End If

  RowCounter = col(strKey)

Exit_RowCounter:
  Exit Function

Err_RowCounter:
  Select Case Err
    Case 457
      ' Key is present.
      Resume Next
    Case Else
      ' Some other error.
      Resume Exit_RowCounter
  End Select

End Function