Numerical values in queries

Posted on 1999-07-23
Medium Priority
Last Modified: 2010-04-30
I have two variables that I would like to use to select records in a query. One of them is a string variable (e.g., strName) and the other is a number (e.g.,RecNum). I can get the following to work: "Select * from TableName where name = '" & strName & "'". I can also get results with "Select * from TableName where RecNum = (for example)30". What I cannot figure out is how to get the proper syntax using the variable RecNum rather than a specific numerical value.
Please help.
Question by:guyjj

Expert Comment

ID: 1526878
Try this :

"Select * from TableName where RecNum = " & MyVar

Accepted Solution

jpforti earned 200 total points
ID: 1526879
Just try this :

Dim myVarNum as Integer
Dim myVarString as String
Dim sSQL as String

myVarNum = 30
myVarString = "Paul"

sSQL = "Select * from myTable Where myNum = " & CStr( myVarNum) & " and " & " Name = '" & myVarString & "'"

'Here is  you can submit the query
'be careful to not avoid the quote (') for a string !


Expert Comment

ID: 1526880
As you are passing a query to a recordset via a string you have to pass the numerical value as a string so:

qry = "SELECT * FROM TableName WHERE RecNum = " & _

Author Comment

ID: 1526881
Thanks for your immediate and very iluminating response. You guys are a good bunch to hang out with!

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question