dbTalk Databases Forums  

Using the contents of a variable instead of the variable name itself

comp.databases.ms-access comp.databases.ms-access


Discuss Using the contents of a variable instead of the variable name itself in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
PW
 
Posts: n/a

Default Using the contents of a variable instead of the variable name itself - 08-04-2011 , 01:31 PM






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

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-04-2011 , 06:16 PM






PW wrote:
Quote:
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 ... ?

Quote:
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

Reply With Quote
  #3  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-04-2011 , 06:19 PM



PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in
news:u0pl37pvrlq32cas4036qhan81v1svcdiu (AT) 4ax (DOT) com:

Quote:
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


--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #4  
Old   
PW
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-04-2011 , 07:22 PM



On Thu, 4 Aug 2011 19:16:08 -0400, "Bob Barrows"
<reb01501 (AT) NOSPAMyahoo (DOT) com> wrote:

Quote:
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 used the prefix lng for long variables, and str for string
variables. I loaded strFieldName with the contents of a text field in
the table that I am reading through. I assigned the contents of a
numeric field from that table to the declared variable lngFrom that
contains a numeric value.


Quote:

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:
I just don't get the syntax rational after all these years! The Getz
97 handbook continues to confuse me further!

Quote:
Is postalcode really a numeric field?
Yes.

Quote:
If not, then you need to delimit the
content of lngFrom:


sSQL="DELETE * FROM [tblQBFTemp] WHERE " & _
strFieldName & "< """ & lngFrom & """"
debug.print sSQL



Thanks,

-pw

Reply With Quote
  #5  
Old   
PW
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-04-2011 , 07:32 PM



On Thu, 04 Aug 2011 23:19:33 GMT, Bob Quintal <rquintal (AT) sPAmpatico (DOT) ca>
wrote:

Quote:
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

Reply With Quote
  #6  
Old   
Bob Quintal
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-05-2011 , 07:47 AM



PW <emailaddyinsig (AT) ifIremember (DOT) com> wrote in
news:4fem37tpdfus8s1sd1hsq00hl7lmtcqj2k (AT) 4ax (DOT) com:

Quote:
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



--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #7  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-05-2011 , 10:26 AM



PW wrote:
Quote:
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.
I showed you what to do if postcode was a Text field: concatenate quotes
around the value.
For a date field, you have to concatenate octothorpes (#) around the value:

" ... WHERE datefield > #" & datval & "#"

Reply With Quote
  #8  
Old   
PW
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-05-2011 , 11:24 AM



On Fri, 05 Aug 2011 12:47:36 GMT, Bob Quintal <rquintal (AT) sPAmpatico (DOT) ca>
wrote:

Quote:
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


I get so confused as when to use single quotes, double quotes, &,
etc...!!

Thanks,

-paulw

Reply With Quote
  #9  
Old   
Douglas J. Steele
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-05-2011 , 04:25 PM



<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:

Quote:
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


--
Bob Q.
PA is y I've altered my address.

Reply With Quote
  #10  
Old   
PW
 
Posts: n/a

Default Re: Using the contents of a variable instead of the variable name itself - 08-05-2011 , 06:58 PM



On Fri, 5 Aug 2011 17:25:48 -0400, "Douglas J. Steele"
<NOSPAM_djsteele (AT) NOSPAM_gmail (DOT) com> wrote:

Quote:
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


lol! No, not picky! Good to know! I like to cover all bases too.

-paulw


Quote:
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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.