Delete Query in VBA based on Combobox gives error, but runs in SQL query

KyleB

Registered User.
Local time
Today, 10:26
Joined
Sep 14, 2001
Messages
71
When attempting to run a Delete query via VBA based on a combobox, I'm getting the following error:

"Object does not support this property or method"

I'm unable to find anything via search that says this is, or is not possible.

I currently have a simple form, with a combobox and a command button. The combobox is based on a query to select and group the filenames stored within a table. The user selects one of these filenames, and uses the command button. The command button runs a Delete query which should delete the records from the table. The error is all I get. When the delete query is run in SQL I get the correct results, but when I attempt to represent that string in VBA I think I may be losing something, or framing it incorrectly.

SQL Version from a regular query that I attempted to represent in VBA:

Code:
Delete *
FROM tblDrillStudyData
WHERE (((tblDrillStudyData.FileName)=[Forms].[frmRecordDelete].[cboFileName]));

VBA Function attached to command button:

Code:
Private Sub cmdDelRecSet_Click()
On Error GoTo Err_cmdDelRecSet_Click
    Dim strQuery As String
    
    strQuery = "DELETE * FROM [tblDrillStudyData] WHERE(((tblDrillStudyData.FileName) = '" & [Forms].[frmRecordDelete].[cboFileName] & "'));"
    DoCmd.RunSQL strQuery

Exit_cmdDelRecSet_Click:
    Exit Sub

Err_cmdDelRecSet_Click:
    msgbox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_cmdDelRecSet_Click
    
End Sub

I also attempted to create a text box on the form and link it as a text string from the combobox, but I get the same results. Any Ideas?
 
Try changing:
Code:
strQuery = "DELETE * FROM [tblDrillStudyData] " _
    & "WHERE(((tblDrillStudyData.FileName) = '" _
    & [Forms].[frmRecordDelete].[cboFileName] & "'));"
...to:
Code:
strQuery = "DELETE * FROM [tblDrillStudyData] " _
    & "WHERE tblDrillStudyData.FileName = '" _
    & Forms("frmRecordDelete").cboFileName & "';"
 
That worked just fine, I just wish I understood why VBA can be so picky, and different for syntax. I really don't see that much difference between the two statements. About all I can do is chalk the differences up to the quirky way in which VBA expects syntax, vs. the general SQL format. Thank you for the help.
 

Users who are viewing this thread

Back
Top Bottom