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: - MyField = "Completed" 'Completed is a field of tbValve
-
strSQL = "Select [ValveName] from tbValve Where MyField Is Null"
Thank you in advance for the help.
Best regards.
Hi Simão.
Try this : - MyField = "Completed" 'Completed is a field of tbValve
-
strSQL = Replace("Select [ValveName] from tbValve Where ([%FN] Is Null)" _
-
, "%FN", MyField)
7 1793 NeoPa 32,556
Expert Mod 16PB
Hi Simão.
Try this : - MyField = "Completed" 'Completed is a field of tbValve
-
strSQL = Replace("Select [ValveName] from tbValve Where ([%FN] Is Null)" _
-
, "%FN", MyField)
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.
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 : - Where ([Completed] Is Null)
Instead of : - 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).
Hi NeoPa
I am just curious as to why you suggest the Replace() function in stead of just concatenating the variable in the string - strSQL = "Select [ValveName] from tbValve Where [" & MyField & "] Is Null"
or have I missed something??
MTB
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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....
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |