2017-04-16 59 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