问题
I have this function below that is letting user choose excel file and it imports the data into a table(MyTable). Its just a single column excel file. The table it imports into contains 2 columns(F1,F2).
I need the DoCmd.RunSQL command to get the following info into that 2nd column.
MyTable.F1 is the OEM part number I need to take that number and compare it to 2 columns (OEMPartNumer, OEMSub) in a (JDSubs) table i have setup If it finds a match i need it to compare those 2 matches from (JDSubs) table and try to find it in (AMI) table in column (OEMItem) If it finds that match i need to return value from column (Item) from table (AMI) and insert it into (MyTable) column (F2)
Table contents example
MyTable
----------------
F1 | F2
AR77530 |
AR12345 |
JDSubs
---------------------------
OEMPartNumer | OEMSub
AR65123 | AR77530
AR12345 | AR56242
AMI
---------------------------
Item | OEMItem
AMAR77530 | AR77530
AMAR56242 | AR12345
So the number being imported from the excel file could be one of 2 numbers(sometimes there is no sub number)
I just need to match up my companies part number (AMI) to the OEM number
Here is the function i am importing the worksheet with into MyTable I just need to get F2 column filled with matching AMI numbers and export back out
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
Also, I have a form setup so the user can look up one number at a time. Here is the function for it. I just need to have an automated process for it
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:
This can be solved with 2 update queries with different joins. Also, I recommend using Currentdb.Execute instead of DoCmd.Runsql because Execute doesn't throw any warning pop up boxes:
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];"
Does this work?
来源:https://stackoverflow.com/questions/16467388/need-sql-to-update-table-with-data-from-another-table-if-numbers-match