473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with WHERE syntax!

Hello,
In ACCESS 2003, I have a combox with a drop-down menu where I can select a name from a table where one of the fields is FactorName. I'm selecting a name from this field.
I have another table called FactorsCol1 which has 4 fields (including the same FactorName):
ClassName, FactorName, FactorIndex, ClassID

I want to extract a FactorIndex from this table based on information obtained after the choice in my drop down menu. Which means if I select a name say "ABC" I want to exctract the FactorIndex say "005", which is associated with this name. But here is the problem:

I use the following code:

Expand|Select|Wrap|Line Numbers
  1. MyVar = Me.FactorSelector1
  2.  
  3. Debug.Print MyVar
  4.  
  5.  DoCmd.RunSQL " SELECT FactorsCol1.FactorIndex, FactorsCol1.FactorName INTO TestTable1 FROM FactorsCol1 WHERE (([FactorsCol1].[FactorName]) = ' & MyVar & ') "
  6.  
MyVar is the name that I get from the field FactorName in my first table. It is always correct because I'm checking it with debug.print. For an unknown reason I get always an empty TestTable1. If however I replace the MyVar directly with the name (say ABC, see the code before) then the SQL is executed and I get the corresponding FactorIndex. Is there something wrong with using variable in this statment?

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL " SELECT FactorsCol1.FactorIndex, FactorsCol1.FactorName INTO TestTable1 FROM FactorsCol1 WHERE (([FactorsCol1].[FactorName]) = ' ABC') "
  2.  
Feb 23 '10 #1
2 1103
Stewart Ross
2,545 Expert Mod 2GB
Hi. You are missing two double quotes - one just after the single quote and before your variable, and one just after. Without them your string cannot include the value of your variable, as you have not split it up into its component parts.

To make it easier to read I have introduced a string variable to hold the SQL string and split it onto separate lines:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL as String
  2.  
  3. strSQL = "SELECT FactorIndex, FactorName " 
  4. strSQL = strSQL & "INTO TestTable1 FROM FactorsCol1 "
  5. strSQL = strSQL & "WHERE [FactorName] = '" & MyVar & "';"
  6.  
  7. DoCmd.RunSQL strSQL
-Stewart
Feb 23 '10 #2
Many thanks Stewart.
This definitely solved the problem. Also in another query I was trying to write.

Marcin
Feb 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
7
by: Aaron Prohaska | last post by:
I have just run into a problem where I have a page that posts back to itself to execute code, except when the page does the post back it somehow executes code that is in our home page for the site....
0
by: Gamb? | last post by:
Hello! I've got some sort of syntax problem that doesn't seem to make a lot of sense. I'm developing a Categories Theory application and because of that I need to make big, and by "big" I mean...
5
by: Aamer Nazir | last post by:
Hi, I am having problems setting the value of a variable in a SQL String that I have to create dynamically in my procedure. The code that I currently have is as follows: set...
4
by: Toonman | last post by:
I'm trying to use a couple of variables in a stored procedure. Things work fine when I hard code the data into the variables and also work fine when I use the variable in the WHERE clause and hard...
4
by: Bradley Burton | last post by:
I'm using Allen Brown's code for audit logging (http://allenbrowne.com/AppAudit.html), but I'm having a problem. My aud table doesn't populate with the tracking info at all. I think it might be a...
3
by: pmud | last post by:
Hi, I have 2 drop down lists on an asp.Net page. The 1st contains alphabets. When the user selects an alphabet frm the first list, the second drop down list should be populated with names from...
4
by: Call Me Tom | last post by:
I have the following PHP/MySQL code segment $query2="UPDATE reports SET fsacars_rep_url = $url_new WHERE pilot_id = $pid"; echo"$query2"; $result2=mysql_query($query2) or...
1
by: youjay | last post by:
I've been out of perl for a while, so I am starting from scratch. I have a small applet which scans a set of directories, getting information from some files in each one, and displaying selected...
0
by: Cameron.MacNeil.024 | last post by:
Hi folks, hopefully you'll be able to help me with this problem I'm having. I have a bunch of xml files with format similar to the following: <?xml-stylesheet href='syntax.xsl'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.