2013-02-24 59 views
1

我正在使用MS Access 2010-2013,我有一個更新查詢,用另一個文本替換幾個文本。我想在我的表單上將查詢轉換爲VBA「Sub Removereplace()」,以便爲每個有問題的文本選擇一個可能的案例進行循環,並在其存在時替換它。我不知道如何做到這一點,一直試圖通過護目鏡找到一個工作的例子,但沒有運氣。有任何想法嗎?謝謝!!!通過一個表格選擇案例替換文本

UPDATE tbl_ImportedTabDelimited SET tbl_ImportedTabDelimited.[Long Description] = Replace(Replace(Replace(Replace([Long Description]," ft","'")," in","""")," ""L""",""),",","") 
WHERE (((tbl_ImportedTabDelimited.[Long Description]) 
Like "*ft*" Or (tbl_ImportedTabDelimited.[Long Description]) 
Like "*in*" Or (tbl_ImportedTabDelimited.[Long Description]) 
Like "*,*" Or (tbl_ImportedTabDelimited.[Long Description]) 
Like "*""L""*")); 
+1

你所建議的是遠比查詢做慢。 – 2013-02-24 20:19:50

+0

好吧,我明白,但我一直在試圖解決這個問題,所以我只想說我想看看如何做到這一點,讓您安心。 – SoggyCashew 2013-02-24 23:35:25

回答

0

好吧,如果你真的必須在一個環路更新VBA的事情:

Sub ThisIsABadIdea() 
    Dim rs As DAO.Recordset 
    Set rs = CurrentDb.OpenRecordset("SELECT Title, Name, Salary FROM Table1") 

    rs.MoveFirst 
    While Not rs.EOF 
     rs.Edit 

     ' Multiple field update 
     If Left(rs!Title, 1) = "Z" Then 
      rs!Title = "Omega Man" 
      rs!Name = "Heston" 
      rs!Salary = 15000 
     End If 

     ' Multiple condition on one field 
     Select Case rs!Title 
     Case rs!Title = "aa": 
      rs!Title = "no way!" 
     Case Left(rs!Title, 4) = "xyxy": 
      rs!Title = "Double Fem!" 
     Case Right(rs!Title, 2) = "-2": 
      rs!Title = "It's a dash two experience" 
     Case Else 
      rs!Title = "It is indescribable" 
     End Select 

     rs.Update 
     rs.MoveNext 
    Wend 
    rs.Close 
End Sub 
+0

我怎樣才能循環不止一個(3-4)的變化。謝謝! – SoggyCashew 2013-02-25 09:25:44

+0

添加更多if/thens? – 2013-02-25 11:42:34