2012-01-10 36 views
0

我有以下問題:從斯普利特()相結合的結果

下面的字符串是通過鏈接表在MS Access數據庫中檢索:

string = Text1, Text2, Text3, "This, belongs, together", Text7, Text8, "This, Also, Belongs, Together", Text13, etc., etc., etc. 

所引用的字段可以在多長時間改變自己是。

當我使用拆分(字符串,「,」)它現在返回13個值,並且我可以刪除替換沒有問題的引號。 我面臨的問題是引號之間的文本應該是1值。 在上面的例子中,這意味着我應該得到8個值而不是13個。這對於對數據庫的INSERT INTO查詢是必需的。

這是我走到這一步,這就給錯誤: 下標越界:「我」

SqlJunk = "SELECT * FROM Con_Temp" 
Set rsCon = Server.CreateObject("ADODB.Recordset") 
rsCon.Open SqlJunk, dbGlobalWeb, 3 

Do While Not rsCon.EOF 
Field = split(rsCon("Field1"),",") 
For i = 0 to UBound(Field) 
    If InStr(Field(i),"""") > 0 Then 
     Field(i) = Replace(Field(i), """", "") 
    End if 
    If Field(i) <> "" Then 
     If dbfields <> "" Then 
      dbfields = dbfields & ",[" & Conveldnaam(i) & "]" 
     Else 
      dbfields = "[" & Conveldnaam(i) & "]" 
     End if 
     If dbvalues <> "" Then 
      dbvalues = dbvalues & ",""" & Field(i) & """" 
     Else 
      dbvalues = """" & Field(i) & """" 
     End if 
    End if 
    response.write(dbfields) 
Next 
SQL = "INSERT INTO ConInventory (" & dbfields & ") VALUES (" & dbvalues & ")" 
response.write(SQL & "<br>") 
dbGlobalWeb.Execute(SQL) 
rsCon.MoveNext 
dbfields = "" 
dbvalues = "" 
Loop 

任何想法我怎麼能引號內結合值?

在此先感謝!

埃裏克

回答

0

試試這個邏輯,並相應地修改代碼: -

Dim str, strArray, count, combineStr 
count = 0 
combineStr = "" 
str = "Text1, Text2, Text3, ""This, belongs, together"", Text7, Text8, ""This, Also, Belongs, Together"", Text13" 
strArray = split(str, ", ") 


For Each item In strArray 
    If (Left(item, 1) = """") Then 
     combineStr = combineStr & " " & item 
     count = 1 
    ElseIf (count = 1) Then 
     If (Right(item, 1) = """") Then 
      count = 0 
     End If 
     combineStr = combineStr & " " & item 
    End If 
    If (count = 0) Then 
     If (combineStr = "") Then 
      Response.Write("<br>" & item) 
     ElseIf (combineStr <> "") Then 
      Response.Write("<br>" & combineStr) 
      combineStr = "" 
     End If 
    End If 
Next 
+0

你是我的英雄!太感謝了!不得不做一些很小的調整來實現它(如你所說),但現在它已經工作了大約90%!你剛剛度過了我的一天,再次感謝! – Meridius 2012-01-10 13:44:21