![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. strFieldName = "postalcode" so I would expect: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [postalcode] < " & lngFrom Well, given that you didn't really concatenate the content of the variable |
|
I assume this is possible to do. Of course. First things first. Best practices include assigning the string |
#3
| |||
| |||
|
|
Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. strFieldName = "postalcode" so I would expect: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [postalcode] < " & lngFrom I assume this is possible to do. -paulw DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] |
#4
| ||||
| ||||
|
|
PW wrote: Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. strFieldName = "postalcode" so I would expect: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [postalcode] < " & lngFrom Well, given that you didn't really concatenate the content of the variable with the string, I'm not sure why you would expect that. It might b e what you _wanted", but ... I'm assuming lngFrom is also a variable ... ? |
| I assume this is possible to do. Of course. First things first. Best practices include assigning the string to be executed to a variable for easy debugging: dim sSQL as string 'no need to qualify the wildcard as there's only one table 'in the FROM clause - no chance of ambiguity 'also, there is no point at all in that DISTINCTROW keyword sSQL="DELETE * FROM [tblQBFTemp] WHERE " & _ strFieldName & "< " & lngFrom debug.print sSQL DoCmd.RunSQL sSQL: |
|
Is postalcode really a numeric field? |
|
If not, then you need to delimit the content of lngFrom: sSQL="DELETE * FROM [tblQBFTemp] WHERE " & _ strFieldName & "< """ & lngFrom & """" debug.print sSQL |
#5
| |||
| |||
|
|
PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in news:u0pl37pvrlq32cas4036qhan81v1svcdiu (AT) 4ax (DOT) com: Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. strFieldName = "postalcode" so I would expect: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [postalcode] < " & lngFrom I assume this is possible to do. -paulw DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE " & strFieldName & " < " & lngFrom |
#6
| |||
| |||
|
|
On Thu, 04 Aug 2011 23:19:33 GMT, Bob Quintal rquintal (AT) sPAmpatico (DOT) ca> wrote: PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in news:u0pl37pvrlq32cas4036qhan81v1svcdiu (AT) 4ax (DOT) com: Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. strFieldName = "postalcode" so I would expect: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [postalcode] < " & lngFrom I assume this is possible to do. -paulw DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE " & strFieldName & " < " & lngFrom That works Bob. What would I do with an integer and especially a date field? Thanks! -paul |
#7
| |||
| |||
|
|
On Thu, 04 Aug 2011 23:19:33 GMT, Bob Quintal <rquintal (AT) sPAmpatico (DOT) ca wrote: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE " & strFieldName & " < " & lngFrom That works Bob. What would I do with an integer and especially a date field? The integer field is as written: no delimiters around the criterion value. |
#8
| |||
| |||
|
|
PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in news:4fem37tpdfus8s1sd1hsq00hl7lmtcqj2k (AT) 4ax (DOT) com: On Thu, 04 Aug 2011 23:19:33 GMT, Bob Quintal rquintal (AT) sPAmpatico (DOT) ca> wrote: PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in news:u0pl37pvrlq32cas4036qhan81v1svcdiu (AT) 4ax (DOT) com: Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. strFieldName = "postalcode" so I would expect: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [postalcode] < " & lngFrom I assume this is possible to do. -paulw DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE " & strFieldName & " < " & lngFrom That works Bob. What would I do with an integer and especially a date field? Thanks! -paul do the same thing, basically. The principle is to have the compiler concatenate the variable by leaving it outside the quote marks ex: Docmd Whatever "text" & variable & " moreText " & othervariable |
#9
| |||
| |||
|
|
Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] |
#10
| |||
| |||
|
|
picky Just in case there's ever a change hat strFieldname might contain blanks, it would be safer to use DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE [" & strFieldName & "] < " & lngFrom /picky |
|
Doug Steele, Microsoft Access MVP http://www.AccessMVP.com/djsteele Co-author: "Access Solutions", published by Wiley (no e-mails, please!) "Bob Quintal" wrote in message news:Xns9F37C4981A1E0BQuintal (AT) 69 (DOT) 16.185.252... PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in news:u0pl37pvrlq32cas4036qhan81v1svcdiu (AT) 4ax (DOT) com: Hi, I want to delete some rows in a table depending on the contents of a string (which contains the field name in the table). For Instance: DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE &[strFieldName] < " & lngFrom Obviously &[strFieldName] generates and error and if I use [strFieldName] I will also get an error because strFieldName isn't a field in the table. DoCmd.RunSQL "DELETE DISTINCTROW [tblQBFTemp].* FROM [tblQBFTemp] WHERE " & strFieldName & " < " & lngFrom |
![]() |
| Thread Tools | |
| Display Modes | |
| |