0

我想将数据导入 MS Access 2010 表,但我导入的数据将取决于我下载的 csv 文件。因此,我需要首先将 CSV 文件从 URL 导入到某种 VBA 容器中。

这篇文章似乎讨论了将 CSV 文件加载到 vba 数组中,但不是从 URL...

到目前为止,我已经完成了下载 CSV 文件的操作:

Public Function GetFileFromURL(ByVal url As String) As Object

Dim myURL As String
myURL = "http://www.somesite.com/file.csv"

Set GetFileFromURL = CreateObject("Microsoft.XMLHTTP")
GetFileFromURL.Open "GET", myURL, False
GetFileFromURL.Send

End Function

Sub DownloadSomething()

GetFileFromURL("ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=31&f=2010&g=d&ignore=.csv")

End Sub

这会运行,但我不知道如何处理该 GetFileFromURL 对象;在这一点上,它可能只是一个字符串列表,但我什至如何获得这些,当我这样做时,我如何将它们转换为数组?

4

1 回答 1

1

我不知道这是否会帮助你。这是我用于质量控制程序的例程。csv 文件有 4 行,最多 20 行,因此 arrData(80) 但它也可以少于 20 行。我用它从 AutoCAD 绘图(从不超过 20)中取出尺寸并填写文本框。

Kim 是对的,每次都必须是相同形式的 csv 格式。

Private Sub Import_Click()
   On Error GoTo HandleError
    RoutineName = Form.Name & " Import_Click Event"

    Dim Detline As String        'Record form selected file
    Dim I As Integer             'Index for rows (table & array)
    Dim FF As Integer            'next file number available for use by the Open statement
    Dim dlgOpen As FileDialog

    Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
    With dlgOpen
        With .Filters
            .Clear
            .Add "Import csv or txt", "*.csv; *.txt", 1
            .Add "All Files", "*.*", 2
    End With
     .InitialFileName = "c:\QControl\"
    .FilterIndex = 1
    .AllowMultiSelect = False
    .Title = "Select document to import"
    .ButtonName = "Ok"
    .Show
    If .SelectedItems.Count > 0 Then
        SelectedFile = .SelectedItems(1)
    End If

End With

ReDim arrData(80)
DoCmd.GoToRecord acDataForm, "Main QC Form", acNewRec
I = 1                        ' initalize counter
FF = FreeFile                'Use FreeFile to supply a file number that is not already in use.
Open SelectedFile For Input As #FF
Do While Not EOF(FF)         'Read selected file line by line
  Line Input #FF, Detline
  arrData = Split(Detline, ",")
  Me.Controls("letter" & I) = arrData(0)
  Me.Controls("printdim" & I) = arrData(1)
  Me.Controls("stepnumber" & I) = arrData(2)
  Me.Controls("Gage" & I) = arrData(3)
  I = I + 1   'increment counter
Loop

RunCommand acCmdSaveRecord ' Save the Record you're looking at
Me.Requery ' Force the Recordset of the Form to update
Me.Form1.Requery

Close #FF   'close text file
DoCmd.GoToRecord , , acLast

Exit_Import_Click:
    DoCmd.SetWarnings True
    Exit Sub
HandleError:
    RtnCode = ErrorHandler   (EH_NumError:=Err.Number,DescError:=
   Err.Description,NameRoutine:=RoutineName)

   Resume Exit_Import_Click
 End Sub
于 2013-10-29T03:27:08.287 回答