如何解决VBA代码中ORA-01861错误,当从Oracle数据库中检索数据时基于标题名称?

huus2vyu  于 2023-05-28  发布在  Oracle
关注(0)|答案(1)|浏览(114)

我的VBA代码显示错误“ORA-01861:文字与格式字符串“”不匹配。然而,当我键入这个标题名称“711_23PS”时,代码正在工作。其他可用的标题名称,代码出现错误。此VBA代码是通过使用数据库中“TITLE”列下的“title name”从Oracle数据库中获取数据。

Sub RetrieveSpecificTitleFromOracle()
    ' Declare variables
    Dim conn As Object ' Connection
    Dim rs As Object ' Recordset
    Dim strSql As String ' SQL query
    Dim i As Integer ' Column counter
    Dim titleName As String ' Title name
    Dim colCount As Integer ' Number of columns

    ' Set up Oracle connection
    Set conn = CreateObject("ADODB.Connection")
    conn.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=DATASOURCEXXX;User ID=USERIDXXX;Password=PASSWORDXXX;"
    conn.Open

    ' Get the title name from the user
    titleName = InputBox("Enter the title name:")
    If titleName = "" Then Exit Sub ' If the user cancels, exit the macro

    ' Set up SQL query to retrieve specific row by title name
    strSql = "SELECT * FROM FAR_OWNER.FAR_VX_REQUESTS WHERE TITLE = '" & titleName & "'"

    ' Execute the query
    Set rs = CreateObject("ADODB.Recordset")
    rs.Open strSql, conn

    ' Check if any data is returned
    If Not rs.EOF Then
        ' Get the number of columns
        colCount = rs.Fields.Count

        ' Copy data to Excel sheet
        For i = 1 To colCount
            Cells(1, i).Value = rs.Fields(i - 1).Name ' Copy column headers
            Cells(2, i).Value = rs.Fields(i - 1).Value ' Copy data from the selected row
        Next i

        ' Auto-fit columns
        Columns.AutoFit
    Else
        MsgBox "No data found for the specified title name."
    End If

    ' Clean up
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing

    ' Inform user about the completion
    MsgBox "Data retrieval completed."
End Sub
yizd12fk

yizd12fk1#

一个更大的问题是下面的代码

' Get the title name from the user
titleName = InputBox("Enter the title name:")
If titleName = "" Then Exit Sub ' If the user cancels, exit the macro

' Set up SQL query to retrieve specific row by title name
strSql = "SELECT * FROM FAR_OWNER.FAR_VX_REQUESTS WHERE TITLE = '" & titleName & "'"

搜索这个网站,或者在谷歌上搜索“SQL注入”,你会发现你所拥有的代码实际上是你将要被黑客攻击的保证。
先解决这个问题,然后再考虑日期。

相关问题