-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteToAccess.R
75 lines (53 loc) · 2.51 KB
/
WriteToAccess.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
rm(list = ls())
#install.packages("RODBC")
install.packages(c("dplyr"))
library(RODBC)
library(dplyr)
#load("C:/Users/MFARR/Documents/R_files/Spotfire.data/DCAwBU.RData")
load("C:/Users/MFARR/Documents/R_files/Spotfire.data/tcgroup.RData")
##purpose is to write/read table from access============================================
##user must have a "Microsoft Access Driver (*.mdb, *accdb) installed on their machine
##there are two different ways to communicate with access
##1 create an odbc connection to the access file "odbcConnect"
##2 create a driver and navigate to the access file location and then communicate
##======================================================================================
##load drivers, file location, and name of the table you want to save
driver <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)}" #driver option
dLocation <- "C:/Users/mfarr/Documents/Spotfire.accdb" #file location
dTable <- "TcParameters" #table to save
ch <- odbcDriverConnect(paste(driver,';DBQ=',dLocation)) #open a channel
TC_Parameters$TC_Name <- as.character(TC_Parameters$TC_Name)
##load table from access
ch <- odbcDriverConnect(paste(driver,';DBQ=',dLocation))
tableOut <- sqlQuery( ch , paste ("select * from ",dTable))
output <- tableOut
##write table to access
driver="Driver={Microsoft Access Driver (*.mdb, *.accdb)}"
ch<- odbcDriverConnect(paste(driver,';DBQ=',dLocation))
sqlSave(ch, TC_Parameters, tablename = dTable, append = TRUE, verbose = TRUE)
TC_Parameters$qi_month <- 10
TC_Parameters$TC_Name <- as.character(TC_Parameters$TC_Name)
sqlUpdate(ch, TC_Parameters, tablename = "TC_Parameters")
sqlDrop(ch, "TC_Parameters")
######-----------------------
ch <- odbcConnect("Spotfire_Test")
close(ch)
sqlTables(ch)
sqlFetch(ch, "TC_Groups")
sqlQuery(ch, paste("select * from TCGroups"))
sqlSave(ch, DCA.Forecast, tablename = "DCA2", append = FALSE)
sqlSave(ch, DCA.Forecast, tablename = tablenames, append = FALSE)
sqlDrop(ch, "TC_Parameters")
sqlDrop(ch, "TCCums")
#sqlUpdate(ch, dcaTbl, tablename = "DCA2")
sqlSave(ch, dcaTbl, tablename = "DCA_Table", append = FALSE)
close(ch)
driver <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)}"
dLocation <- "C:/Users/mfarr/Documents/Spotfire.accdb"
dummyTable <- "BLANK"
##load table from access
channel <- odbcDriverConnect(paste(driver,';DBQ=',dLocation))
TCGroups <- sqlQuery(ch, paste("select * from TcForecast"))
#TCCums <- sqlQuery(channel, paste("select * from TCCums"))
sqlUpdate(ch, TCGroups, tablename = "TcForecast")
TCGroups <- filter(TCGroups, Time > 120)