質問

I have a database that I am trying to send information contained on a form combined with selected items in a listbox to a table when the user clicks a Send button. I have the code setup that should copy my information but get a syntax error and I am not sure why... I have tried several different things and can't get it to work. I have included the code below:

Private Sub ctrSend_Click()
Dim intI As Integer
Dim lst As ListBox
Dim varItem As Variant

Set lst = Me![lstShipping]

With lst
    If .ItemsSelected.count = 0 Then Exit Sub
        For Each varItem In .ItemsSelected
            CurrentDb.Execute "INSERT INTO ShipInv ([Order], [ShipDate], [BIN], [SKU], [Lot], [QtyProd])" _
            "VALUES ('" & Me.[ctrSOrder] & "'," & Me.[ctrSDate] & ",'" & .Column(0, varItem) & "'," & .Column(1, varItem) & "," & .Column(2, varItem) & "," & .Column(3, varItem) & ");", dbFailOnError
    Next
End With

End Sub

役に立ちましたか?

解決

For a situation like this, I always reccommend using a string to hold the constructed SQL so that you can easily print the string to the immediate window to check how certain values have broken your SQL.

So, try adding

Dim strSQL As String

strSQL = "INSERT INTO ShipInv ([Order], [ShipDate], [BIN], [SKU], [Lot], [QtyProd])" _
        "VALUES ('" & Me.[ctrSOrder] & "'," & Me.[ctrSDate] & ",'" & .Column(0, varItem) & "'," & .Column(1, varItem) & "," & .Column(2, varItem) & "," & .Column(3, varItem) & ");"
Debug.Print strSQL
CurrentDb.Execute strSQL 'remove dbFailOnError temporarily so that failure will stop code

My blind guess is that if ShipDate is a date field(not text), you'll need to format that value with Format(Me.[ctrSDate], "\#mm\/dd\/yyyy\#" before pasting it into the SQL.

他のヒント

I used a different approach and it works out great...

Private Sub ctrSend_Click()
Dim intI As Integer
Dim lst As ListBox
Dim varItem As Variant
Dim rst As DAO.Recordset

Set lst = Me![lstShipping]
Set rst = CurrentDb.OpenRecordset("ShipInv", dbOpenTable)

With lst
    If .ItemsSelected.count = 0 Then Exit Sub
        For Each varItem In .ItemsSelected
            rst.AddNew
            rst!Order = Me.[ctrSOrder]
            rst!EntDate = Date
            rst!ShipDate = Me.[ctrSDate]
            rst!BIN = .Column(0, varItem)
            rst!SKU = .Column(1, varItem)
            rst!Lot = .Column(2, varItem)
            rst!QtyProd = .Column(3, varItem)
            rst.Update
    Next
End With
rst.Close

Set rst = Nothing
MsgBox "Warehouse Inventory Updated", vbOKOnly, "Inventory Confirmation"
End Sub
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top