如何在VBA代码中执行插入查询

时间:2018-11-13 01:54:19

标签: sql excel ms-access access-vba

我正在尝试根据与第二个表中的用户名匹配的条件从一个表中选择行。仅运行此查询时出现错误。 接下来,我需要将这些选定的行插入到第三张表中。

当前,我正在获取strSQL1的语法错误。谢谢!

Public Sub CalculateUA1_Click()

Dim db As Database
Dim rst As Recordset
Dim UsersTname As String
Dim SCCMTname As String
Dim UsersAppList1 As String


Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQLWhere As String


Set db = DBEngine.Workspaces(0).Databases(0)

UsersTname = "UsersList"
SCCMTname = "SCCM1"
UsersAppList1 = "tbl_UsersApplicationList1"

Set rst = db.OpenRecordset(UsersTname)

strSQL1 = "SELECT s.User_Name0, s.[Machine name], s.displayName0, s.Mail0, 
s.AD_Site_Name0, s.[Application Name], s.[Application Version] FROM  '" & 
SCCMTname & "' as S  ; "

strSQLWhere = "user_name0 in  ( SELECT [SOE ID] FROM  '" & UsersTname & "' 
)"

strSQL1 = strSQL1 & " Where " & strSQLWhere

DoCmd.SetWarnings False
DoCmd.RunSQL strSQL1
DoCmd.SetWarnings True

strSQL2 = "INSERT INTO '" & UsersAppList1 & "'  VALUES ('" & strSQL1 & "' 
);"

DoCmd.SetWarnings False
DoCmd.RunSQL strSQL2
DoCmd.SetWarnings True

1 个答案:

答案 0 :(得分:0)

类似这样的东西:

library(tidyverse)
library(scales) 
dataset <- c("gg1", "gg2","gg3") 
group <- c("A 1", "A 1", "A 1", "Y 2", "Y 2", "Y 2", "P 3", "P 3", "P 3") 
val <- c("5", "4", "3", "2", "4", "5", "2", "8", "6") %>% as.numeric
stdev <- c("0.5", "0.1", "0.5","0.07","0.2", "0.5","0.2","0.4", "0.8") %>% as.numeric
dat_frame <- data.frame(dataset, group, val, stdev) %>% as.tibble()


p8 = ggplot(dat_frame, aes(fill=dataset, y=val, x=group, ymin = val-stdev, ymax = val+stdev))+
  geom_bar(position="dodge", stat="identity", width = 1)+
  scale_fill_manual(values=c('gg1' = 'red','gg2' = 'green','gg3'='grey'))

p8+geom_errorbar(width = .08, position = position_dodge(1))