在Access vba中循环遍历表

时间:2014-11-13 18:18:00

标签: sql vba ms-access for-loop access-vba

我在访问中有一个for循环,从1到表中记录的计数。我想循环,基本上说如果客户端名称的第一个单词与之前的单词相同,给它与之前的单词相同的内部ID,但我不确定如何使用索引引用记录。我将使用mid函数来提取我想要比较的名称的一部分。我对Access中的VBA很新,所以对此主题的任何帮助将不胜感激。我已粘贴到目前为止的内容。

Sub RevH()
Dim dte As String, clientQry As String, db As Database, clientTable As Recordset

Set db = CurrentDb


dte = InputBox("What date was the Data Dump run?", "Please Input a date")


clientQry = "SELECT DISTINCT t.[CLIENT ID], t.[CLIENT NAME] " & _
            "INTO Clients " & _
            "FROM FN_DataDump_ALL_" & dte & " as t WHERE " & _
            "((t.[CLIENT NAME] Not Like ""*Test*"") AND (t.[CLIENT NAME] Not Like ""*Demo*"") AND ( t.[CLIENT NAME] Not Like ""*Client*"" )AND (t.[CLIENT NAME] Not Like ""Tufts"") AND (t.[CLIENT NAME] Not Like ""SCAN"" ) AND (t.[CLIENT NAME] Not Like ""US Script"") AND (t.[CLIENT NAME] Not Like ""Care1st Health Plan"") AND (t.[CLIENT NAME] Not Like ""PBM View"") AND (t.[CLIENT NAME]Not Like ""ScriptGuideRx"") AND (t.[CLIENT NAME] Not Like ""Argus"") AND (t.[CLIENT NAME] Not Like ""Centene"") AND (t.[CLIENT NAME] Not Like ""MMIT-FN - FormularyEditor"")  AND (t.[CLIENT NAME] Not Like ""CVS Caremark"") AND (t.[CLIENT NAME] Not Like ""Caterpiller"") AND (t.[CLIENT NAME] Not Like ""Horizon BCBS"") AND (t.[CLIENT NAME] Not Like ""Health Net"")  AND (t.[CLIENT NAME] Not Like ""CMS"")) " & _
            "ORDER BY [CLIENT NAME]"


If TableExists("Clients") Then
    CurrentDb.TableDefs.Delete ("Clients")
End If

CurrentDb.Execute clientQry


DoCmd.RunSQL "ALTER TABLE Clients ADD COLUMN [Internal_ID] DOUBLE;"

Set clientTable = CurrentDb.OpenRecordset("Clients")


For i = 1 To DCount("[CLIENT NAME]", "Clients")



Next i





End Sub

1 个答案:

答案 0 :(得分:3)

尝试使用此代替for循环。

Dim intID As Integer
Dim sName As String

intID = 0
sName = vbNullstring
If Not clientTable.EOF And Not clientTable.BOF Then
  clientTable.MoveFirst
  Do While Not clientTable.EOF

    --apply your name comparison logic
    If sName <> Left(clientTable("[Client Name]"),5) Then
        sName = Left(clientTable("[Client Name]"),5)
        intID = intID + 1
    End If
    clientTable.Edit
    clientTable("[Internal_ID]") = intID
    clientTable.Update
    clientTable.MoveNext
  Loop
End If