【问题标题】:VBA/Access INSERT into statement passing a numeric string as a numberVBA/Access INSERT into 将数字字符串作为数字传递的语句
【发布时间】:2019-01-25 18:35:32
【问题描述】:

我正在将工作簿中的数据读取到访问表中。我遇到的问题是我在文本格式的单元格中有数字数据,这些数据有一个前导零,当 INSERT into 语句读取它时,它会切断前导零。传递给它的数据具有前导零,并且它进入的表列是文本格式。

是否有解决方法让 SQL 将数字字符串作为字符串传递?

您看到的各种 msgbox 用于调试目的。

Dim StringVar As Variant
Dim strLn As String
    'Asks user for Filepath
    StringVar = InputBox("Please enter file path", "Import", "")
    'Ends Function if no input or cancel is detected
    MsgBox (StringVar)
    If StringVar = "" Or StringVar = False Then
        MsgBox ("No input, Please try again")
        Exit Sub
    End If
    'Scrubs outer quotes if present
    StringVar = Replace(StringVar, Chr(34), "", 1, 2)
    MsgBox ("File Path Checked")
Dim FSO As Object
    Set FSO = CreateObject("Scripting.Filesystemobject")
    'Checks that our file exists, exits if not
    If (Not FSO.FileExists(StringVar)) Then
        MsgBox ("File does not exist, try again")
        Exit Sub
    End If
Dim xlApp As Object 'Excel.Application
Dim xlWrk As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim i As Long
    MsgBox ("working on an excel sheet")
    Set xlApp = VBA.CreateObject("Excel.Application")

    'toggle visibility for debugging
    xlApp.Visible = False

    Set xlWrk = xlApp.Workbooks.Open(StringVar) 'opens the excel file for processing
    Set xlSheet = xlWrk.Sheets("Sheet1") 'modify to your perticular sheet
    MsgBox ("About to change the format")
    xlSheet.Columns("A").NumberFormat = "@"

    MsgBox ("Format changed")
    'walks through the excel sheet to the end and inserts the lines below the headerline into the database
    For i = 2 To xlSheet.UsedRange.Rows.Count
        DoCmd.RunSQL "Insert Into Split_List(Criteria) values(" & xlSheet.Cells(i, 1).Value & ")"
        If (MsgBox(i, vbOKCancel) = vbCancel) Then Exit Sub
    Next i
    MsgBox ("Brought in " & i & " lines")
    'Quits the file and closes the object
    xlWrk.Save
    xlWrk.Close
    xlApp.Quit

    Set xlSheet = Nothing
    Set xlWrk = Nothing
    Set xlApp = Nothing

【问题讨论】:

  • Access 中的列是定义为文本还是数字?如果访问列是数字列,那么它当然会去掉前导零。所以要检查的一件事是确保 Access 列是文本列而不是数字列。因此,这应该保留前导零。
  • 列是短文本。我刚刚尝试使用 .Text 并得到了相同的结果。是否有关于 SQL 插入语句如何传递值的怪癖?
  • 抱歉 - 线程错误 - 删除了 cmets。正在回复另一个线程。在您的情况下,如果访问列必须是文本数据类型,否则它将删除前导零。 (对 bigint 大雁追逐感到抱歉)。
  • Access 将该列视为数字列。因此,从 Excel 中提取的值将没有前导零。如果访问将该列(单元格)视为文本,那么这将/应该工作。所以真正的问题是如何强制访问将该单元格查看/格式化为文本列而不是数字列。

标签: sql excel vba ms-access


【解决方案1】:

我在这个问题中找到了我的问题的答案 Should I quote numbers in SQL?

我没有将作为代码的 ID 作为字符串传递,而是将它们作为数字传递,因此在插入 'xlSheet.Cells(i, 1).Value' 的语句中,我需要将单引号放入围绕该值的 SQL 语句,因此它作为字符串传递

声明应如下所示

DoCmd.RunSQL "Insert Into Split_List(Criteria) values('" & xlSheet.Cells(i, 1).Value & "')"
                                                      ^                                 ^

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2017-07-26
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多