473,383 Members | 2,005 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,383 software developers and data experts.

Use a string to indicate a field in a SQL statment

Dear Bytes community,

Framework: I have an SQL statment in a public function at a module. This fuction will be used in different forms to populate Comboboxes. My question is:
- Is it possible to use a string instead of a field in the criteria of the SQL statment? The goal was to change only the argument of my Public Function in order to populate my combobox, depending on the form.

Problem: Using the code on the example bellow, Access cannot identify the field.

Simple debug example:
Expand|Select|Wrap|Line Numbers
  1. MyField = "Completed" 'Completed is a field of tbValve
  2. strSQL = "Select [ValveName] from tbValve Where MyField Is Null"
Thank you in advance for the help.

Best regards.
Sep 10 '17 #1

✓ answered by NeoPa

Hi Simão.

Try this :
Expand|Select|Wrap|Line Numbers
  1. MyField = "Completed" 'Completed is a field of tbValve
  2. strSQL = Replace("Select [ValveName] from tbValve Where ([%FN] Is Null)" _
  3.                , "%FN", MyField)

7 1793
NeoPa
32,556 Expert Mod 16PB
Hi Simão.

Try this :
Expand|Select|Wrap|Line Numbers
  1. MyField = "Completed" 'Completed is a field of tbValve
  2. strSQL = Replace("Select [ValveName] from tbValve Where ([%FN] Is Null)" _
  3.                , "%FN", MyField)
Sep 10 '17 #2
Hello NeoPa,

Thank you very much for your fast and accurate answer!
Worked very well.
In reality, cannot understand why using the replace function the problem is solved.
Sep 10 '17 #3
NeoPa
32,556 Expert Mod 16PB
Simão:
In reality, cannot understand why using the replace function the problem is solved.
That's because the string ends up saying :
Expand|Select|Wrap|Line Numbers
  1. Where ([Completed] Is Null)
Instead of :
Expand|Select|Wrap|Line Numbers
  1. Where ([MyField] Is Null)
SQL has no protocol that allows it to specify a field name indirectly via a VBA variable. Jet/ACE, which interprets and executes the SQL, has no idea what's going on at the VBA level at all, so wouldn't recognise MyField even if there were a protocol for indirect referencing of Fields (which there isn't of course).
Sep 11 '17 #4
MikeTheBike
639 Expert 512MB
Hi NeoPa

I am just curious as to why you suggest the Replace() function in stead of just concatenating the variable in the string
Expand|Select|Wrap|Line Numbers
  1. strSQL = "Select [ValveName] from tbValve Where [" & MyField & "] Is Null"
or have I missed something??

MTB
Sep 12 '17 #5
Hello NeoPa,

Thank you for your detailed explanation.
Always learning!

@MikeTheBike,
I was unable to put the combobox working with your solution.
Maybe NeoPa will explain later the reason for not working.

Regards.
Sep 12 '17 #6
NeoPa
32,556 Expert Mod 16PB
Hi Mike.

I very rarely use simple concatenation nowadays as I've seen too many examples where the meaning of the code is just lost in the complexity of it. Many times have I had questions where the OP was simply confused by the mixing of strings, the ampersands and the variables (Not to mention the quote characters of course).

With Replace(), and my own function MultiReplace(), this is so much easier to follow. The first parameter is always the basic template of the string and each replaced value is clearly seen in the original context. It leads to fewer mistakes, and fewer similar questions on the boards.

I've posted my MultiReplace() function previously but I'm happy to do so again if required. I don't want to keep posting it unless there's real interest though.
Sep 13 '17 #7
NeoPa
32,556 Expert Mod 16PB
Hi Mike.

I very rarely use simple concatenation nowadays as I've seen too many examples where the meaning of the code is just lost in the complexity of it. Many times have I had questions where the OP was simply confused by the mixing of strings, the ampersands and the variables (Not to mention the quote characters of course).

With Replace(), and my own function MultiReplace(), this is so much easier to follow. The first parameter is always the basic template of the string and each replaced value is clearly seen in the original context. It leads to fewer mistakes, and fewer similar questions on the boards.

I've posted my MultiReplace() function previously but I'm happy to do so again if required. I don't want to keep posting it unless there's real interest though.

@Simão.
There's no good reason for Mike's suggestion to fail. If used as suggested it should work perfectly. It uses a differnt approach at the VBA level, but a perfectly valid one.
Sep 13 '17 #8

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

Similar topics

1
by: ree32 | last post by:
I am having problems with Date Strings when using VS.net method of converting an XML document to a dataset. I need the the date nodes to be of date type. As I am loading the Dataset to a...
3
by: Lauren Quantrell | last post by:
This a a long convoluted string parsing question... I have a string in an Access 2K database table field that I use for noting if a user has checked a record. The string goes like this: xy So...
1
by: jpatterson | last post by:
Is it possible to use a text and number in the same field. Currently the field is formatted as numbers, fieldname credits, this field is used to indicate the number of credits a person gets from...
4
by: Bonzol | last post by:
..Net 2003, 1.1, Access database. oledb connection Hey there, I had my program updating to the database with a dataset,,, doing things the easy way, binding the dataset to the text box n such,...
1
by: Dan Somdahl | last post by:
Hi, I am new to ASP but have what should be a fairly simple task that I can't figure out. I need to parse a string from a single, semi-colon delimited, 60 character field (el_text) in a recordset...
1
by: cva | last post by:
I am using Access and would like to join two tables. Table1 has 3 fields: ID, Type, Date Table2 has the following fields: Date, Type1, Type2, ..., where each entry is Data. What would...
1
by: smartic | last post by:
I need to use this string "preg_match('$pattern','$value')" to return boolean value in if statment how can i do that i used eval function put it give me this error (Parse error: syntax error,...
0
by: gourisankarts | last post by:
Hi , I have an ssis package with a connection string stored in a dts config file . but while trying to retrieve the connection string it ommits the password field and gives the rest of the field....
6
by: Adam Tippelt | last post by:
I've got a form that allows users to "save the current record to drafts" if they haven't finished writing all the data for that record. This saves it in it's current state, which puts it in a...
4
NeoPa
by: NeoPa | last post by:
I've searched around for help on this but everything I find seems to be a discussion about the relative benefits of using, or not using, this setting. I want to specify in SQL Server that ZLSs are...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.