How to set column values in a record set in access vba

好久不见. 提交于 2019-12-14 04:12:57

问题


Given below is the working code. Previously I was using the .Name property that didn't work. Previous code:

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

New Complete working code. It opens a dialog for user to select the .csv file and then imports all the data into the table from that csv file.

    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

回答1:


Don't use the Name property. Use Value.

How are you populating the array? If it has base index of 0, then increment Count after setting the field value.



来源:https://stackoverflow.com/questions/43439579/how-to-set-column-values-in-a-record-set-in-access-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!