Solved

docmd.runsql argument error

Posted on 2011-03-06
3
633 Views
Last Modified: 2012-08-14
I am trying to loop through each client in the tblclients table and for each client delete some records in another table.

I have a string select statement that changes for each client BUT the docmd.runsql method says it needs an argument consisting of an sql string which I though I had passed it

if i add brackets i get the same.
Private Sub BtnRemoveDeletedCWCodesFromClients_Click()
Dim db As Database
Set db = CurrentDb

Dim rs As DAO.Recordset
Dim clientId As Integer
Dim cwcodetounmatch As String
Dim StrSelect As String
Dim strFrom As String
Dim strWhere As String

Dim newsql As String

Dim sqlstr As String





Set rs = db.OpenRecordset("tblclients")

StrSelect = "SELECT * "

With rs
rs.MoveFirst
    Do While Not rs.EOF
   
    Select Case rs.Fields("clientid").Value

        Case 1
            strFrom = "FROM AbiToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = AbiToMvris.[mvris code]))<>False));"
        Case 2
            strFrom = "FROM GlassesToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = GlassesToMvris.[mvris code]))<>False));"
        Case 3
            strFrom = "FROM CW_to_IDS "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CW_to_IDS.[mvriscode]))<>False));"
        Case 4
            strFrom = "FROM CWCode_To_Continental "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CWCode_To_Continental.[cwcode]))<>False));"
        Case 5
            strFrom = "FROM HalfordsToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = HalfordsToMvris.[mvriscode]))<>False));"
        Case 6
            strFrom = "FROM CWCode_To_Michelin "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CWCode_To_Michelin.[cwcode]))<>False));"
        Case 7
            strFrom = "FROM TechDocToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = TechDocToMvris.[mvris code]))<>False));"
        Case 8
            strFrom = "FROM VividToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = VividToMvris.[mvriscode]))<>False));"
        Case 9
            strFrom = "FROM CapToMvris-CW "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CapToMvris-CW.[mvriscode]))<>False));"

End Select
    
    sqlstr = StrSelect & strFrom & strWhere
    
    DoCmd.RunSQL sqlstr
    
    
    Loop
End With
End Sub

Open in new window

0
Comment
Question by:PeterBaileyUk
3 Comments
 

Accepted Solution

by:
r1sc earned 250 total points
ID: 35046304
Hi PeterBaileyUk,
most cases where RunSQL asks for an argument is related to an column name which is not spelled correctly.
I would bet you wanted to join two tables and select only few rows with matching [mvris code], but sometimes this column has an similar name.
You try to use the second table name in the query in the where clause, but it is not included in your from clause, so the name can not be resolved to a column name in all tables in your from clause.
You need to write:
...from QryCWCodesRemoved, AbiToMvris where QryCWCodesRemoved.[mvris code] = AbiToMvris.[mvris code]
or
...from QryCWCodesRemoved inner join AbiToMvris on QryCWCodesRemoved.[mvris code] = AbiToMvris.[mvris code]

best regards,
r1sc
0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 250 total points
ID: 35046354
I see that you are using different version of mvriscode/ [mvris code] in different places.  This looks like the most obvious place where something could be wrong. But when ypou get the error you need to go into the immediate windoe and do..
?sqlstr
and look at that particular statment.

You could copy it into a new empty query and get a precise error message if necessary.
0
 

Author Comment

by:PeterBaileyUk
ID: 35046449
it was me being stupid I was testing but change the query to a select query which I subsequently discovered docmd.runsql does not run, now its changed to delete ..... its working.
Private Sub BtnRemoveDeletedCWCodesFromClients_Click()
Dim db As Database
Set db = CurrentDb

Dim rs As DAO.Recordset
Dim clientId As Integer
Dim cwcodetounmatch As String
Dim StrSelect As String
Dim strFrom As String
Dim strWhere As String
Dim newsql As String


'Set qd = db.QueryDefs(stringfull)

Dim sqlstr As String
Dim qd As DAO.QueryDef
Set qd = db.QueryDefs("QryDelFromMatchTable")
sqlstr = qd.SQL


'DoCmd.SetWarnings False

Set rs = db.OpenRecordset("tblclients")


' DoCmd.SetWarnings = False
With rs
rs.MoveFirst
    Do While Not rs.EOF
   
    Select Case rs.Fields("clientid").Value

        Case 1
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = AbiToMvris.[mvris code]), * "
            strFrom = "FROM AbiToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = AbiToMvris.[mvris code]))<>False));"
        Case 2
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = GlassesToMvris.[mvris code]), * "
            strFrom = "FROM GlassesToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = GlassesToMvris.[mvris code]))<>False));"
        Case 3
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CW_to_IDS.[mvriscode]), * "
            strFrom = "FROM CW_to_IDS "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CW_to_IDS.[mvriscode]))<>False));"
        Case 4
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CWCode_To_Continental.[cwcode]), * "
            strFrom = "FROM CWCode_To_Continental "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CWCode_To_Continental.[cwcode]))<>False));"
        Case 5
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = HalfordsToMvris.[mvriscode]), * "
            strFrom = "FROM HalfordsToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = HalfordsToMvris.[mvriscode]))<>False));"
        Case 6
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CWCode_To_Michelin.[cwcode]), * "
            strFrom = "FROM CWCode_To_Michelin "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CWCode_To_Michelin.[cwcode]))<>False));"
        Case 7
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = TechDocToMvris.[mvris code]), * "
            strFrom = "FROM TechDocToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = TechDocToMvris.[mvris code]))<>False));"
        Case 8
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = VividToMvris.[mvriscode]), * "
            strFrom = "FROM VividToMvris "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = VividToMvris.[mvriscode]))<>False));"
        Case 9
            StrSelect = "DELETE Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CapToMvris-CW.[mvriscode]), * "
            strFrom = "FROM CapToMvris-CW "
            strWhere = "WHERE (((Exists (Select [mvris code] from QryCWCodesRemoved where QryCWCodesRemoved.[mvris code] = CapToMvris-CW.[mvriscode]))<>False));"

End Select
    
   newsql = StrSelect & strFrom & strWhere
   
'   Debug.Print newsql
'   qd.SQL = newsql
'   DoCmd.OpenQuery ("QryDelFromMatchTable")
    DoCmd.RunSQL newsql
    rs.MoveNext
    Loop
End With
'DoCmd.SetWarnings True
End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending email from List Data 2 51
Explanation of Access VBA code 2 36
MS Access VBA Print PDF just Current Page 9 5
Access coding 2 0
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now