Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT INTO query not doing its job 1

Status
Not open for further replies.

davyre

Programmer
Oct 3, 2012
197
AU
Hi,
I have an append query to insert some data into a table from ms excel. Heres the code:

Code:
Dim partNum As String
Dim desc As String
Dim Material As String
Dim cost As Long
Dim vendorPartNum As String
Dim VendorID As Long
Dim ctr As Long

Dim xlLine As Integer
Dim allBlank As Integer

Dim insQry As String

Dim strFilter As String
Dim StrInputFileName As String
Dim xlApp As Excel.Application
Dim wkb As Excel.workbook
Dim wks As Excel.Worksheet

strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsx)", "*.xlsx")
strFilter = ahtAddFilterItem(strFilter, "Excel 97-2003 Files (*.xls)", "*.xls")
strFilter = ahtAddFilterItem(strFilter, "all Files (*.*)", "*.*")

StrInputFileName = ahtCommonFileOpenSave( _
                            filter:=strFilter, _
                            OpenFile:=True, _
                            DialogTitle:="Choose an image file...", _
                            Flags:=ahtOFN_HIDEREADONLY)

If Len(StrInputFileName) > 0 Then
    ' Do nothing as a file was chosen MsgBox strInputFileName, vbOKOnly
Else
    'No file chosen, or user canceled
    Exit Sub
End If
    
Set xlApp = CreateObject("Excel.application")
Set wkb = GetObject(StrInputFileName)
Set wks = wkb.Worksheets("Sheet1")


allBlank = 0
xlLine = 2
ctr = 0

Do While allBlank <= 3
    partNum = wks.Cells(xlLine, "A")
    If partNum = "" Or IsNull(partNum) Then
        allBlank = allBlank + 1
        xlLine = xlLine + 1
    Else
        allBlank = 0
        desc = Replace(wks.Cells(xlLine, "F"), "'", "''")
        Material = wks.Cells(xlLine, "I")
        cost = wks.Cells(xlLine, "K")
        vendorPartNum = wks.Cells(xlLine, "D")
        VendorID = 43
        
        insQry = "INSERT INTO TblParts([PartNumber],[PartDescription],[Material],[PartCost],[VendorPart],[VendorID]) " _
                & "VALUES('" & partNum & "','" & desc & "','" & Material & "'," & cost & ",'" & vendorPartNum & "'," & VendorID & ")"
        
        DoCmd.SetWarnings False
        DoCmd.RunSQL insQry
        DoCmd.SetWarnings True
        
        Debug.Print insQry
        
        ctr = ctr + 1
        xlLine = xlLine + 1
    End If
    
Loop

MsgBox (ctr)

The issue is that it wont append the whole data. THe total data it needs to append is 174, but instead it only does 25. And there are some autonumber that jumps (i.e after 1011 then 1040)
I have checked using debug.print and it seems perfectly fine (I can see 174 records in Immediate Window), so my suspect is the docmd.runsql insQry...but what happen?anyone can help?
Thanks
 
yes, I run the insQry but it seems did not insert all records..
what do you mean by constraints? It has relationship with tblPartStock and tblPartOrder but that's it..
I have checked the datatypes for each field and it should be okay. Otherwise there will be no records being inserted.
 
update: I tried to delete all the tblparts, tblpartstock, and tblpartorder. Now it seems working (it got 174 records on tblparts). Probably there are something that prevents access to insert the record freely.
 
update 2 :

Okay, now I understand why. The 149 of 174 records that are not appended to the table are duplicates to the existing records. But the duplicates are only on PartNumber field (not the PK). The reason I entering these duplicates because I have different price and vendorID (thus these are not much duplicates except the PartNumber).
So for example, the existing records are like this:
Code:
PartID  | PartNumber | ... | PartCost | VendorID |
1       | [highlight #FCE94F]A00001[/highlight]     |     | 5.99     | 15       |
2       | [highlight #8AE234]A00010[/highlight]     |     | 3.99     | 15       |

and the ones that I want to insert are
Code:
PartNumber | ... | PartCost | VendorID |
[highlight #FCE94F]A00001[/highlight]     | ... | 4.99     | 22       |
[highlight #8AE234]A00010[/highlight]     | ... | 3.49     | 22       |

so how can I insert data like those?
 
Be sure that no unique key is created on PartNumber.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top