質問

I have an Access database which needs to take monthly updates from an excel spreadsheet. The spreadsheet has meaningful column headings which don't match the database tables. In addition, I need to check whether data is to be merged or appended. So I want to obtain a dataset from the Spreadsheet which I can loop through and have my code decide what to do with the data. I could create a temporary table in Access which exactly matches the spreadsheet structure, populate this with DoCmd.TransferSpreadsheet acImport, do my stuff and then delete it.

Is this the simplest method, or can I do something like:

strQuery = "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" _
   & Chr(34) & fromFile & Chr(34) & "].[sheet1]"

Set rs = CurrentDb.OpenRecordset(strQuery)

This code produces the error: Cannot Update. Database or object is read only

役に立ちましたか?

解決

I think it better to create new table in access and then first copy all excel data to that table then everything will work fine and faster

ilan

他のヒント

I think you need to use ADO, not a DOA recordset. ADO requires that you add a reference (unless you use late binding). Some of the properties and methods of ADO are different from DAO, like there is not .Edit method in ADO.

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Scripts\Test.xls;" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"";" 

objRecordset.Open "Select * FROM [Sheet1$]", _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText

Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item("Name"), _
        objRecordset.Fields.Item("Number")
    objRecordset.MoveNext
Loop

This was taken from MS's technet site where you can get more detailed information: http://technet.microsoft.com/en-us/library/ee692882.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top