2017-04-16 76 views
-2

以下是工作代碼。以前我使用的是無效的.Name屬性。 上一個代碼:如何在訪問vba中的記錄集中設置列值

 For Each s In rs.Fields 
      word = Replace(strArray(count), """", "") 
      count = count + 1 
     'the below line shows error 
      s.Name = word 
     Next 

新的完整的工作代碼。它打開一個對話框供用戶選擇.csv文件,然後將所有數據從該csv文件導入到表中。

strMsg = "Select the file from which you want to import data" 
mypath = GetPath(strMsg, True) 
mypath = mypath 

Dim strFilename As String: strFilename = mypath 
Dim strTextLine As String 
Dim strArray() As String 
Dim count As Integer 

Dim regex As New RegExp 
regex.IgnoreCase = True 
regex.Global = True 
'This pattern matches only commas outside quotes 
'Pattern = ",(?=([^"]*"[^"]*")*(?![^"]*"))" 
regex.Pattern = ",(?=([^""]*""[^""]*"")*(?![^""]*""))" 

Dim iFile As Integer: iFile = FreeFile 
Open strFilename For Input As #iFile 
Dim db As DAO.Database 
Dim rs As DAO.Recordset 
Set db = CurrentDb 
count = 0 

Do Until EOF(1) 
    Line Input #1, strTextLine 
    count = 0 

'regex.replaces will replace the commas outside quotes with <???> and then the 
'Split function will split the result based on our replacement 
    On Error GoTo ErrHandler 
    strTextLine = regex.Replace(strTextLine, "<???>") 
    strArray = Split(regex.Replace(strTextLine, "<???>"), "<???>") 
    Set rs = db("AIRLINES").OpenRecordset 
    Dim word As Variant 
    With rs 
     .AddNew 
     For Each s In rs.Fields 
      word = Replace(strArray(count), """", "") 
      count = count + 1 
     'the below line shows error 
      s.Value = word 
     Next 
     .Update 
     .Close 
    End With 
lpp: 
    Loop 

db.Close 
Close #iFile 
MsgBox ("Imported Successfully") 
Exit Sub 
ErrHandler: 
    Resume lpp 
+2

Maybe's.Value = word'? –

+0

對我無效 – Faisal

+3

錯誤是什麼? –

回答

1

請勿使用Name屬性。使用價值。你如何填充數組?如果它的基本索引爲0,則在設置字段值後遞增計數。

+0

它不起作用 – Faisal

+2

它適合我。會發生什麼 - 錯誤信息,錯誤結果,什麼都沒有?編輯問題以發佈整個過程。 – June7

+0

請參閱最新的代碼。 – Faisal