msaccess adodb 插入语句

问题描述 投票:0回答:1

我有这段代码,我从表中获取变量,并希望将它们插入到新表中。 我在插入语句上不断收到错误,但我似乎无法弄清楚为什么。 我只在 insert into 语句中收到错误语法错误,仅此而已。

'阿多

Sub ProcessRecords()
    Dim conn As Object
    Dim rs As Object
    Dim strSQL As String
    Dim DateTimeData As String
    Dim CallerNumberData As String
    Dim CalledNumberData As String
    
    Set conn = CreateObject("ADODB.Connection")
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & CurrentDb.Name
    
    strSQL = "SELECT * FROM Clean_Log_Telefooncentrale;"
    Set rs = conn.Execute(strSQL)
    
    Do While Not rs.EOF
        If InStr(rs("Param"), "Date event") > 0 Then
            DateTimeData = rs("ParamData")
        ElseIf InStr(rs("Param"), "Calling party number") > 0 Then
            CallerNumberData = rs("ParamData")
        ElseIf InStr(rs("Param"), "Called party number") > 0 Then
            CalledNumberData = rs("ParamData")
            
        ' Debug output
        Debug.Print "DateTimeData: " & DateTimeData
        Debug.Print "CallerNumberData: " & CallerNumberData
        Debug.Print "CalledNumberData: " & CalledNumberData
            
            ' Insert the data into the new table if all values are present
            If DateTimeData <> "" And CallerNumberData <> "" And CalledNumberData <> "" Then
                conn.Execute "INSERT INTO Tbl_Telefooncentrale (DateTime, CallerNumber, CalledNumber) VALUES ('" & DateTimeData & "', '" & CallerNumberData & "', '" & CalledNumberData & "')"
            End If
            
            ' Reset variables
            DateTimeData = ""
            CallerNumberData = ""
            CalledNumberData = ""
        End If
        rs.MoveNext
    Loop
    
    rs.Close
    conn.Close
    
    Set rs = Nothing
    Set conn = Nothing
End Sub

我尝试过 DAO,但我的库有问题,所以这不是一个选择。 我想保留 ADODB,我也尝试单独放置参数,但插入语句仍然存在问题。

ms-access parameters adodb
1个回答
0
投票

DateTime
是保留字,你的逻辑似乎不对,所以尝试:

    Do While Not rs.EOF
        If InStr(rs("Param"), "Date event") > 0 Then
            DateTimeData = rs("ParamData")
        ElseIf InStr(rs("Param"), "Calling party number") > 0 Then
            CallerNumberData = rs("ParamData")
        ElseIf InStr(rs("Param"), "Called party number") > 0 Then
            CalledNumberData = rs("ParamData")
        End If
            
        ' Debug output
        Debug.Print "DateTimeData: " & DateTimeData
        Debug.Print "CallerNumberData: " & CallerNumberData
        Debug.Print "CalledNumberData: " & CalledNumberData
            
        ' Insert the data into the new table if all values are present
        If DateTimeData <> "" And CallerNumberData <> "" And CalledNumberData <> "" Then
            conn.Execute "INSERT INTO Tbl_Telefooncentrale ([DateTime], CallerNumber, CalledNumber) VALUES ('" & DateTimeData & "', '" & CallerNumberData & "', '" & CalledNumberData & "')"
            
            ' Reset variables
            DateTimeData = ""
            CallerNumberData = ""
            CalledNumberData = ""
        End If
        rs.MoveNext
    Loop
© www.soinside.com 2019 - 2024. All rights reserved.