Search code examples
rms-accessrodbc

Alternative for RODBC package to make connection from R to MS Access


I am trying to make a connection from R to a database file in Microsoft Access. I used to use the RODBC package for this.

However, the package recently got updated and now it's not compatible anymore with my version of R (I cant easily upgrade my version of R since its used for business purposes).

These are the lines of code I use where the RODBC package comes into play:

install.packages("RODBC")
library(RODBC)  
access_file     <- "file_name.accdb"
cfs <- odbcConnectAccess2007(access_file)   
df1 <- sqlFetch(cfs, "G_Company")

Does anyone know an alternative for the RODBC package?

Thanks.


Solution

  • You can use the Tidyverse way to connect with databases, packages DBI and odbc:

    library(DBI)
    library(odbc)
    connection <- dbConnect(odbc(), .connection_string = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\file_name.accdb;")
    df1 <- dbReadTable(connection , "G_Company")
    

    This has multiple advantages, such as allowing you to embed SQL statements in RMD files, and allowing you to integrate with dbplyr to form queries from dplyr statements.

    However, if you're going to use old R versions, I often use the checkpoint package, which allows me to install multiple versions of packages side-by-side and avoid incompatibilities between them by fetching packages from before a specified date.