2013-11-27 53 views
1

我正在將sql導入到我的Access數據庫中,並且正在將數據解析到正確的表和字段中。我再次轉向你們的大師們以幫助解決問題解析Access中的字符串值

其中一個導入的字段包含逗號分隔的值,需要進行分隔。字符串中可能存在1到10個可能的值。

PHO,Rosgen,NRCS,EMAP,T-DL,YSI-DL 

我已經想通了,如果我做的所有值相同的長度(比如4個字符),我可以得到第一,最後和第一逗號之後解析,但似乎無法得到提取的中間值正確。

SELECT Left([FieldForms],InStr([FieldForms],",")-1) AS DEQ_SampleTypeID 
FROM tblSiteVisit 
UNION ALL 
SELECT Mid([FieldForms],InStr([FieldForms],",")+1,4) AS DEQ_SampleTypeID 
FROM tblSiteVisit 
UNION ALL 
SELECT Mid([FieldForms], 11, 4) AS DEQ_SampleTypeID 
FROM tblSiteVisit 
UNION ALL 
SELECT Mid([FieldForms], 16, 4) AS DEQ_SampleTypeID 
FROM tblSiteVisit 
UNION ALL 
SELECT Mid([FieldForms], 21, 4) AS DEQ_SampleTypeID 
FROM tblSiteVisit 
UNION ALL 
SELECT Mid([FieldForms],InStrRev([FieldForms],",")-4,4) AS DEQ_SampleTypeID 
FROM tblSiteVisit 
UNION ALL 
SELECT Right([FieldForms],InStr([FieldForms],",")-1) AS DEQ_SampleTypeID 
FROM tblSiteVisit 

如果我使用InStrRev或右邊的功能,我得到重複,如果有比也使用空行的中期功能結果的最大較少。

有沒有辦法來解析出像這樣的字符串,只能從字符串

+0

這將是更容易的IMO與Access VBA過程而不是查詢。這對你來說會是一個合適的選擇嗎? – HansUp

+0

任何可以工作的東西都是一個選項 – pja

+0

'Split(FieldForms,「,」)'會給你一個字符串數組。向數組中的每個項添加一行到目標表。 – HansUp

回答

1

將下面的功能集成到一個模塊得到結果:

Function CountCSWords (ByVal S) As Integer 
    ' Counts the words in a string that are separated by commas. 

    Dim WC As Integer, Pos As Integer 
    If VarType(S) <> 8 Or Len(S) = 0 Then 
     CountCSWords = 0 
     Exit Function 
    End If 
    WC = 1 
    Pos = InStr(S, ",") 
    Do While Pos > 0 
     WC = WC + 1 
     Pos = InStr(Pos + 1, S, ",") 
    Loop 
    CountCSWords = WC 
    End Function 

    Function GetCSWord (ByVal S, Indx As Integer) 
    ' Returns the nth word in a specific field. 

    Dim WC As Integer, Count As Integer, SPos As Integer, EPos As Integer 
    WC = CountCSWords(S) 
    If Indx < 1 Or Indx > WC Then 
     GetCSWord = Null 
     Exit Function 
    End If 
    Count = 1 
    SPos = 1 
    For Count = 2 To Indx 
     SPos = InStr(SPos, S, ",") + 1 
    Next Count 
    EPos = InStr(SPos, S, ",") - 1 
    If EPos <= 0 Then EPos = Len(S) 
    GetCSWord = Trim(Mid(S, SPos, EPos - SPos + 1)) 
    End Function 

然後,把一個場在您的查詢像這樣的:

MyFirstField: GetCSWord([FieldForms],1) 

把另一個像這樣:

MySecondField: GetCSWord([FieldForms],2) 

等......儘可能多的你需要。

+0

在我看來,使用[Split()](http://office.microsoft.com/en-ca/access-help/split-function-HA001228911.aspx)函數可以相當簡化這段代碼。 –

+0

找不到如何在查詢中執行此操作。 Split()會創建一個數組。如果你能想出來,發佈它! –

+0

不是世界上最好的VBA ...我創建了模塊和查詢,但它不工作,我似乎已經搞亂了某個地方。我收到關於無效圓括號的錯誤。你可以給我在SQL查詢? – pja

1

這VBA代碼在tblSiteVisitFieldForms讀取文本值,分割該文本爲子,然後存儲每個在在一個新行DEQ_SampleTypeID加入tblDestination的子串。

Dim astrItems() As String 
Dim db As DAO.database 
Dim i As Long 
Dim qdf As DAO.QueryDef 
Dim rs As DAO.Recordset 
Dim strInsert As String 

strInsert = "INSERT INTO tblDestination (DEQ_SampleTypeID)" & vbCrLf & _ 
    "VALUES ([array_item]);" 

Set db = CurrentDb 
Set rs = db.OpenRecordset("tblSiteVisit", dbOpenTable, dbOpenSnapshot) 
Set qdf = db.CreateQueryDef(vbNullString, strInsert) 
Do While Not rs.EOF 
    astrItems = Split(rs!FieldForms, ",") 
    For i = 0 To UBound(astrItems) 
     qdf.Parameters("array_item") = astrItems(i) 
     qdf.Execute dbFailOnError 
    Next 
    rs.MoveNext 
Loop 
rs.Close 
Set rs = Nothing 
Set qdf = Nothing 
Set db = Nothing 
+0

我99%確定(總是保持最後1%...),這將只爲表中的每個逗號分隔值插入一條新記錄。儘管OP發佈了一個UNION ALL SQL字符串,但他的文本顯示他正在尋找一種方法讓每個CSV分解,這告訴我他希望將它們分成單獨的**字段**。這比爲每個值創建新的**記錄**更有意義。 –