Excel VBA INSERT INTO statement using variables

前端 未结 1 1128
北恋
北恋 2021-01-15 00:57

I am using an SQL query to insert data from an excel worksheet into an Access database. I put all the data that I want from the worksheet into variables:

rw         


        
相关标签:
1条回答
  • 2021-01-15 01:30

    Why not make use of date/time field to include test time?

    It is best to format dates, assuming that b5 is a date type:

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

    You will end up with text, but this is not important.

    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 & ")"
    

    If any of the fields are text, the value will have to be surrounded by single quotes or two double quotes, in a similar way to date and time.

    I strongly recommend getting rid of spaces in field (column) and table names.

    0 讨论(0)
提交回复
热议问题