Issue with SQL in VBA

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hi there,

I'm trying to set a value in a table based on a multi-select listbox. I have little to no experience of SQL.

Code:
Private Sub Command2_Click()
    Dim str_SQLText     As String, _
        str_MNVal       As String, _
        str_RTVal       As String
 
    Dim i               As Long
 
    With lst_MatSelection
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                str_RTVal = .Column(1, i)
                str_MNVal = .Column(2, i)
                str_SQLText = "UPDATE tbl_MaterialData " & _
                    "SET Complete = YES " & _
                    "WHERE Material Number = '" & str_MNVal & "' AND " & _
                    "Request Type = '" & str_RTVal & "'"
                DoCmd.RunSQL str_SQLText
            End If
        Next i
    End With
End Sub

I get a Syntax Error - Missing Operator.

Any ideas where I'm going wrong? Complete is a Yes/No field. I have also tried =True but I get the same error.

Cheers,

James
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This might not be the source of your troubles but you have spaces in your field names. Wrap the field name in brackets like this [Material Number]. Also, it's not clear if Material Number is a number or a string. If it's a number you don't want to wrap the variable in quotes as the sql will treat it as a string and you'll get other errors.

hth,

Rich
 
Upvote 0
Hi Rich,

Brackets sorted it for me.. Time to read up on ADO and SQL!

Cheers mate.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,445
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top