我在下面有这个功能,让用户选择 excel 文件并将数据导入表(MyTable)。它只是一个单列excel文件。它导入的表包含 2 列(F1,F2)。
我需要 DoCmd.RunSQL 命令将以下信息放入第二列。
MyTable.F1 是 OEM 零件编号,我需要获取该编号并将其与我已设置的(JDSubs)表中的 2 列(OEMPartNumer,OEMSub)进行比较如果找到匹配项,我需要它比较来自(JDSubs ) 表并尝试在 (AMI) 列 (OEMItem) 的表中找到它如果找到匹配项,我需要从表 (AMI) 的列 (Item) 返回值并将其插入 (MyTable) 列 (F2)
表格内容示例
MyTable
----------------
F1 | F2
AR77530 |
AR12345 |
JDSubs
---------------------------
OEMPartNumer | OEMSub
AR65123 | AR77530
AR12345 | AR56242
AMI
---------------------------
Item | OEMItem
AMAR77530 | AR77530
AMAR56242 | AR12345
所以从 excel 文件中导入的数字可能是 2 个数字之一(有时没有子数字)
我只需要将我公司的零件编号 (AMI) 与 OEM 编号相匹配
这是我将工作表导入 MyTable 的功能,我只需要让 F2 列填充匹配的 AMI 编号并导出回来
Sub Import()
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim CustomerFile As String
Dim LUser As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Please select your OEM part number file."
.Filters.Clear
.Filters.Add "Excel Spreadsheets", "*.xlsx"
.Filters.Add "Excel Spreadsheets", "*.xls"
.InitialFileName = "C:\Users\" & LUser & "\Desktop"
If .Show = True Then
'Loop through each file selected and add it to the list box.
For Each varFile In .SelectedItems
CustomerFile = varFile
Next
End If
End With
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "MyTable", CustomerFile, False, "sheet1!A:A"
DoCmd.RunSQL ?????
Exit Sub
End Sub
另外,我有一个表单设置,因此用户可以一次查找一个数字。这是它的功能。我只需要一个自动化的过程
Function fnSearchAndPopulate() As Boolean
Dim d As DAO.Database, r As DAO.Recordset, strSQL As String
Set d = CurrentDb
If Me.txtEnterNumber.Value = "" Then
MsgBox "Please Enter Number", , "Error"
Exit Function
End If
strSQL = " SELECT * FROM JDSubs Inner Join AMI on " & _
" AMI.OEMItem=JDSubs.OEMPartNumber WHERE " & _
" JDSubs.OEMPartNumber= '" & txtEnterNumber.Value & "' or " & _
" JDSubs.OEMSub= '" & txtEnterNumber.Value & "
Debug.Print strSQL
Set r = d.OpenRecordset(strSQL)
If r.EOF Then
MsgBox "OEM # " & Me.txtEnterNumber & " does not exist!", , "No AMI #"
Set d = Nothing
Exit Function
End If
'get here if there is a record
r.MoveFirst
'populate whatever textboxes
Me.txtAMINumber = r!Item
Me.txtDescription = r!Description
Me.txtOEMsubnumber = r!OEMSub
Set d = Nothing
Exit Function
End Function