简体   繁体   中英

How do I insert into an Office 365 Access database using Excel VBA

I've recently upgraded to office 365 and now find myself attempting to use excel VBA to insert from an Excel sheet into an Access database. Here is the VBA code I'm trying to use:

Sub ExportDataToAccess()

    Dim cn As Object
    Dim strQuery As String
    Dim myDB As String
    Dim creditDate As Date
    Dim regionalTeam As String


    'Initialize Variables
    creditDate = Worksheets("Treasury").Range("E20").Value
    regionalTeam = Worksheets("Treasury").Range("e21").Value

    myDB = "Y:\Credit DB\Credit.accdb"

    Set cn = CreateObject("ADODB.Connection")

    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"    'For *.ACCDB Databases
        .ConnectionString = myDB
        .Open
    End With

    strQuery = "INSERT INTO Credit ([creditDate], [regionalTeam]) " & "VALUES (""" & creditDate & """, " & regionalTeam & ");"

    cn.Execute strQuery
    cn.Close
    Set cn = Nothing

End Sub

When I run this subroutine, I get the following error message:

Runtime error: No value given for one or more required parameter.

I've tried to Google the error message but didn't have much luck. Can anyone tell me where I have gone astray? I've also confirmed that creditDate and regionalTeam have valid values. I should add that the cn.Execute strQuery seems to be the offending code (highlighted).

Thanks for your input.

Format your date expression:

strQuery = "INSERT INTO Credit ([creditDate], [regionalTeam]) VALUES (#" & Format(creditDate, "yyyy\/mm\/dd") & "#, " & regionalTeam & ");"

or, if the team is text:

strQuery = "INSERT INTO Credit ([creditDate], [regionalTeam]) VALUES (#" & Format(creditDate, "yyyy\/mm\/dd") & "#, '" & regionalTeam & "');"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM