Excel VBA使用variablesINSERT INTO语句

我正在使用SQL查询从Excel工作表插入数据到Access数据库。 我把工作表中的所有数据放到variables中:

rwyNumber = Range("b13").Value & Range("c13").Value testDate = Range("b5").Value testTime = Range("b6").Value rwySide1 = Range("b14").Value firstThird1 = Range("b28").Value secondThird1 = Range("b29").Value thirdThird1 = Range("b30").Value averageFriction1 = Range("b23").Value 

如果我想将testDate和testTime存储为Date / Time,那么当我将它们存储在variables(上面)中时是否需要转换它们,或者只要将数据types设置为Date(date/时间) /表中的那些列的时间? 还有双打和string。

什么是我的查询的其余部分正确的语法:

 stSQL1 = "INSERT INTO Friction Tests (Runway Number, Runway Test Side, Test Date, Test Time, 1/3, 2/3, 3/3, Average)" stSQL1 = "VALUES ( " .... 

该表有一个自动PK。 前两个值是string,接下来的两个是date/时间,最后四个是双打。

为什么不使用date/时间字段来包含testing时间?

假设b5是datetypes,最好格式化date:

 testDate = Format(Range("b5").Value,"yyyy/mm/dd") 

你会以文字结束,但这并不重要。

 stSQL1 = "INSERT INTO [Friction Tests] ([Runway Number], [Runway Test Side], " _ & "[Test Date], [Test Time], [1/3], [2/3], [3/3], [Average]) " _ & "VALUES ( " & rwyNumber & ",#" & testDate & "#,#" & testTime _ & "#," & rwySide1 & "," & firstThird1 _ & "," & secondThird1 & "," & thirdThird1 _ & "," & averageFriction1 & ")" 

如果任何字段是文本,那么值必须用单引号或两个双引号包围,与date和时间类似。

我强烈build议删除字段(列)和表名中的空格。