查询语法混淆

时间:2014-12-15 23:20:21

标签: ms-access access-vba

对于我在Access DB中使用VB编写的这段代码,我的语法有点混淆。

Public Sub ()

 Dim dbs As DAO.Database
 Dim TrainingID As Integer
 Dim EmployeeIDTb1 As Integer
 Dim JobIDTb1 As Integer
 Dim CompetencyTb1 As String
 Dim DateApprovedTb1 As String
 Dim InsertStr As String

 TrainingID = 100


 Set dbs = CurrentDb
 Set dbs1 = CurrentDb.OpenRecordset("SELECT * FROM Table1")

 'Do Until dbs1.Fields("ID").Value <> Null
 'set values
  EmployeeIDTbl1 = dbs1.Fields("EmployeeID").Value
 'JobIDTb1 = dbs1.Fields("JobID").Value
 'CompetencyTb1 = dbs1.Fields("Competency").Value
 'DateApprovedTb1 = dbs1.Fields("DateApproved").Value
 'Set Insert String
  InsertStr = "INSERT INTO TrainingList (EmployeeID.[Value]) Values(1) Where TrainingID = TrainingID;"

  dbs.Execute InsertStr
  TrainingID = TrainingID + 1
  'dbs1.MoveNext
  'Loop


 dbs.Close
 Set dbs = Nothing

 End Sub

基本上我感到困惑的地方就在这里。

InsertStr = "INSERT INTO TrainingList (EmployeeID.[Value]) Values(1) Where TrainingID = TrainingID;"

我正在尝试将EmployeeIDTbl1传递给Value(EmployeeIDTbl1)作为参数,但我无法弄清楚要在其中使用什么语法。注意EmployeeIDTbl1返回值= 1

2 个答案:

答案 0 :(得分:0)

您的插入SQL应为:

InsertStr =&#34; INSERT INTO TrainingList(EmployeeID。[Value])值(&#34;&amp; EmployeeIDTbl1&amp;&#34;)其中TrainingID =&#34; &安培; TrainingID

答案 1 :(得分:0)

以下是我编写的解决方案,用于处理使用Access 2013 for SharePoint中的多个值字段插入记录。

第一步:

创建一个与SharePoint列表具有相同列名的本地表。 (对于本代码,我们可以说它的名为&#34; Table1&#34;)

第二步:使用Excel将数据导入到创建的表中。

第三步:计算表格中的记录数量。

Forth Step:执行InitializeRecords函数

Option Compare Database

Public Sub InitalizeRecords()

Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim BufferVal As Integer
Dim RecordSize As Integer


Set dbs = CurrentDb
Set dbs2 = CurrentDb.OpenRecordset("SELECT * FROM 'The Table You're Using'")

'Intialize Variables'
BufferVal = 1
RecordSize = 'Enter Amount of Records to be Inserted'

For i = 1 To RecordSize
'Insert Buffer Records'
 dbs2.AddNew
 dbs2("Title").Value = BufferVal
 BufferVal = BufferVal + 1
 dbs2.Update
 Next



End Sub

此函数初始化表并将空记录插入Share Point中的表以填充主键值,在此示例中&#34; TrainingID&#34;

步骤5:执行InsertRecords函数

Dim DateApprovedTbl1 As String
Dim InsertStr As String
Dim InsertStr2 As String
Dim InsertStr3 As String
Dim InsertStr4 As String
Dim BufferVal As Integer
Dim MaxRecords As Integer

 Set dbs = CurrentDb
 Set dbs1 = CurrentDb.OpenRecordset("SELECT * FROM Table1")
 Set dbs2 = CurrentDb.OpenRecordset("SELECT * FROM TrainingList")


'Intialize Training ID Value'
 TrainingID = dbs2.Fields("TrainingID").Value


Do Until dbs1.Fields("ID") <> Null

   'set values'
    EmployeeIDTbl1 = dbs1.Fields("EmployeeID").Value
    JobIDTbl1 = dbs1.Fields("JobID").Value
    CompetencyTbl1 = dbs1.Fields("Competency").Value
    DateApprovedTbl1 = dbs1.Fields("DateApproved").Value

   'Set Insert String'
    InsertStr = "INSERT INTO TrainingList (JobID.[Value]) Values (" & JobIDTbl1 & ") 
    WHERE TrainingID = " & TrainingID & " "

    dbs.Execute InsertStr
    InsertStr2 = "INSERT INTO TrainingList (EmployeeID.[Value]) Values (" &
    EmployeeIDTbl1  & ") 
    WHERE TrainingID = " & TrainingID & " "
    dbs.Execute InsertStr2

'Update Competency And DateApproved'
InsertStr3 = "Update TrainingList SET Competency = '" & CompetencyTbl1 & " ' WHERE   
TrainingID = " & TrainingID & "; "

dbs.Execute InsertStr3
InsertStr4 = "Update TrainingList SET DateApproved = '" & DateApprovedTbl1 & " ' WHERE 
TrainingID = " & TrainingID & "; "

dbs.Execute InsertStr4

    TrainingID = TrainingID + 1
    BuffVal = BuffVal + 1
    dbs1.MoveNext
 Loop


dbs.Close
Set dbs = Nothing

End Sub

关于运行此代码的说明:

此代码的Skeleton在此前提下运行

INSERT INTO Issues ( AssignedTo.[Value] )
VALUES (6)
WHERE ID = 10;  
  1. 确保列中的数据&#34; DO NOT&#34;具有空值,因为它会破坏它,但是你可以在我的代码中插入一些逻辑来检查你是否需要。我用它的效果还不错。

  2. 我有一个示例数据库将要插入大约2000个记录,对于这个较小规模的项目它工作正常,但是初始化和插入记录需要相当多的时间。

  3. 让脚本运行直至完成,否则如果您在运行时尝试使用访问权限,它将停止响应。如果有人有解决方案来加快这些功能的进程,我会跟着赐教,因为我很好奇。

    我真的希望能够使用这个解决方案,因为只使用某些工具来绕过SharePoint内部的禁用工具,这是令人沮丧的。