使用表单按钮将CSV导入MS Access,出现混淆错误
我正在尝试导入一个CSV文件,该文件是从我开发的web表单创建的。当表单提交时,它会在my CSV中创建一个包含大量客户信息的记录 根据要求,我需要将其放入CSV,然后分别将其导入Access数据库,以便其他人使用服务器安全性所需的两个步骤 我尝试的方法是使用一个简单的表单,在Access中有一个按钮,简单地说是Import,只要用户需要,它就会拉一个CSV更新 我的错误把我弄糊涂了 目标表“应用程序”中不存在字段“F1” 我的CSV中没有标记为F1的字段,甚至没有任何包含“F1”的记录,在我的access table应用程序中也没有名为F1的字段 下面是我的VB模块代码,来自Access使用表单按钮将CSV导入MS Access,出现混淆错误,csv,ms-access,import,vba,Csv,Ms Access,Import,Vba,我正在尝试导入一个CSV文件,该文件是从我开发的web表单创建的。当表单提交时,它会在my CSV中创建一个包含大量客户信息的记录 根据要求,我需要将其放入CSV,然后分别将其导入Access数据库,以便其他人使用服务器安全性所需的两个步骤 我尝试的方法是使用一个简单的表单,在Access中有一个按钮,简单地说是Import,只要用户需要,它就会拉一个CSV更新 我的错误把我弄糊涂了 目标表“应用程序”中不存在字段“F1” 我的CSV中没有标记为F1的字段,甚至没有任何包含“F1”的记录,在我的
Option Compare Database
Sub ImportingCSV()
Function Import()
On Error GoTo Macro1_Err
DoCmd.TransferText acImportDelim, "", "Applications", "C:\Users\ALee\Documents\formTesting22.csv", False, ""
Import:
Exit Function
Macro1_Err:
MsgBox Error$
Resume Macro1_Exit
End Function
这是我的CSV文件格式,为了便于阅读
错误告诉我CSV中第二个电话号码4062033985的字段在表应用程序中没有字段,但它有!CSV中的F1是客户移动的。当我通过Access的导入向导手动导入时,效果很好
希望有人能给我指出正确的方向,不熟悉access中的VB脚本或宏。不要导入文件
将csv文件链接为表。然后创建一个查询来读取和转换数据
将此查询用作进一步处理日期的源,如将数据附加到其他表。CSV文件是电子表格。。。试试看
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml,[YourDestinationTable],"C:\YourFileDirectoryPath, filename, and extension",true,[Spreadsheet name if multiple sheet names]
有各种各样的方法来做这类事情。这当然是最简单的方法
Private Sub Command0_Click()
DoCmd.TransferText acImportDelim, "", "Book1", "C:\your_path_here\Book1.csv", True, ""
End Sub
假设要将多个相同类型的CSV文件导入到同一个表中。只需运行下面的脚本
Option Compare Database
Option Explicit
Private Sub Command0_Click()
DoImport
End Sub
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\your_path_here\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function
你也可以做其他各种事情;使用msoFileDialogFilePicker导航到文件;您可以循环遍历记录集,并将它们逐个加载到表中。正如Gustav所建议的,您可以链接到文件暂存并将记录写入生产表。您可能应该尝试所有这些方法,并尝试使用@puropoix Ah Bugs beat me to it,抱歉。您是否尝试过从:DoCmd.TransferText acImportDelim、应用程序、C:\Users\ALee\Documents\formTesting22.csv、False中删除?我在看一些我以前的转会资料,但从来没有在里面放过任何占位符。里面的其他东西看起来都没问题。但这会让我的数据库面临安全威胁吗?我只想通过两步流程表单->CSV,然后是CSV->数据库来捕获数据,因为我的雇主要求我这样做,因此web表单用户没有直接链接到保存其他所有人提交的信息的数据库。他们可能有一个链接的CSV只保存了几天的提交数据,因此如果它被破坏,影响将是最小的。这与现在没有什么不同。使用传输链接runquery替换传输导入方法。不同之处在于,在查询中,您可以控制字段名,可以排除无效数据,并且可以转换一些或多个字段以适应接收表的字段。我已创建了一个链接并创建了它所创建的表。不过,我现在面临一个问题:如果客户在员工打开数据库时提交web表单应用程序,它不会将提交附加到CSV或数据库。由于数据库之间的链接使CSV保持打开状态,因此新记录将丢失。我在想如何避免这种情况,我在想一个潜在的中间数据库?一个是直接链接到CSV,另一个是让我们的员工查看数据?我不确定这是否会引起同样的问题。CSV文件仅在读取期间保持打开状态。实际上,您可以替换该文件,当您再次打开查询时,数据将被更新。或者,您可以将原始CSV文件复制到临时位置,然后链接副本。如果数据库处于打开状态,即使只是处于空闲状态,CSV文件也不会接受任何新记录。链接保持文件打开,就像CSV本身打开一样。同时创建我的CSV副本,然后链接副本,这意味着我永远无法获取用户提交给CSV原始文件的新记录。
Option Compare Database
Option Explicit
Private Sub Command0_Click()
DoImport
End Sub
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\your_path_here\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function