需要SQL使用来自另一个表IF数字匹配的数据来更新表

时间:2013-05-09 17:17:18

标签: sql vba ms-access

我在下面有这个功能,让用户选择excel文件,然后将数据导入表格(MyTable)。它只是一个列excel文件。它导入的表包含2列(F1,F2)。

我需要使用DoCmd.RunSQL命令将以下信息输入第二列。

MyTable.F1是OEM部件号 我需要取一个数字并将其与我在设置的(JDSubs)表中的2列(OEMPartNumer,OEMSub)进行比较 如果找到匹配,我需要它来比较来自(JDSubs)表的那两个匹配并尝试在列(OEMItem)的(AMI)表中找到它 如果它发现匹配我需要从表(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的功能 我只需要填充满足AMI编号的F2列并导出

 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

1 个答案:

答案 0 :(得分:0)

这可以通过具有不同联接的2个更新查询来解决。另外,我建议使用Currentdb.Execute而不是DoCmd.Runsql,因为Execute不会弹出任何警告弹出框:

Currentdb.Execute 
    "UPDATE 
    (MyTable INNER JOIN JDSubs ON MyTable.F1 = JDSubs.OEMPartNumber) 
    INNER JOIN AMI ON JDSubs.OEMPartNumber = AMI.OEMItem 
    SET MyTable.F2 = [AMI].[Item];"

Currentdb.Execute
    "UPDATE 
    (MyTable INNER JOIN JDSubs ON MyTable.F1 = JDSubs.OEMSub) 
    INNER JOIN AMI ON JDSubs.OEMSub = AMI.OEMItem
    SET MyTable.F2 = [AMI].[Item];"

这有用吗?