473,385 Members | 1,712 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,385 software developers and data experts.

VBA SQL query resets variable values to "0"

I'm constructing a SQL statement called strTxSQL that is
supposed to use two variables: lngItemCode (long integer) and datMyDate (date defined as MM/DD/YYYY).
The SQL statement will not work with either variable, as it thinks they are both "0". If I hard-code
the values in the SQL statement then it works. What needs to be changed to make it work with the variables?

Expand|Select|Wrap|Line Numbers
  1.    strTxSQL = "SELECT tblTaxType.TaxTypeID,tblTaxType.Desc,tblTaxType.TaxCode,tblTaxRates.Rate,"
  2.    strTxSQL = strTxSQL & " tblTaxRates.EffectiveDate"
  3.    strTxSQL = strTxSQL & " FROM tblTaxRates, tblTaxType, tblItemsReqdTaxes"
  4.    strTxSQL = strTxSQL & " WHERE (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID)"
  5.    strTxSQL = strTxSQL & " AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax)"
  6. 'THIS LINE WILL NOT WORK   strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType like " & lngItemCode & " )"
  7. 'USED NEXT LINE INSTEAD
  8.    strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = 1 )"
  9.    strTxSQL = strTxSQL & " AND (tblTaxRates.EffectiveDate)="
  10.    strTxSQL = strTxSQL & " (select max(tblTaxRates.EffectiveDate)"
  11.    strTxSQL = strTxSQL & " FROM tblTaxRates"
  12. 'THIS LINE WILL NOT WORK   strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
  13. 'USED NEXT LINE INSTEAD
  14. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= (#" & Forms!frmReservation!ArrivalDate + intInvcCtr & "#)"
  15.    strTxSQL = strTxSQL & " AND (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID)"
  16.    strTxSQL = strTxSQL & " AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax)"
  17. 'THIS LINE WILL NOT WORK   strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = " & lngItemCode & ")))"
  18. 'USED NEXT LINE INSTEAD
  19.    strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = 1)))"
  20.    strTxSQL = strTxSQL & " ORDER BY tblTaxRates.Rate DESC"
  21.  
  22.    intInvcCtr = 0
  23.    intLengthOfStay = Forms!frmReservation!DepartureDate - Forms!frmReservation!ArrivalDate
  24.    For intInvcCtr = 0 To intLengthOfStay - 1
  25.      '================================================================
  26.      ' set datMyDate
  27.      '================================================================
  28.       datMyDate = Format(DateAdd("d", intInvcCtr, Forms!frmReservation!ArrivalDate), "MM/DD/YYYY")
  29.      '================================================================
  30.      ' Load the room information in the RsRoomRates recordset
  31.      '================================================================
  32.       Set cnnRoom = CurrentProject.Connection
  33.       Set RsRoomRates = New ADODB.Recordset
  34.       RsRoomRates.ActiveConnection = cnnRoom
  35.       RsRoomRates.Open strRoomRatesSQL
  36.       strItemCode = RsRoomRates.Fields(1).Value
  37.       lngItemCode = RsRoomRates.Fields(0).Value
  38.      '================================================================
  39.      ' Load the tax types and tax rates in the RsTaxRates recordset
  40.      '================================================================
  41.       Set cnnTax = CurrentProject.Connection
  42.       Set RsTaxRates = New ADODB.Recordset
  43.       RsTaxRates.ActiveConnection = cnnTax
  44.       RsTaxRates.Open strTxSQL
  45.       Debug.Print strTxSQL    
  46.        :
  47.        :
  48.    Next
Here is the "Debug.Print strTxSQL" output from the immediate window when I run it as shown above.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblTaxType.TaxTypeID,tblTaxType.Desc,tblTaxType.TaxCode,tblTaxRates.Rate, tblTaxRates.EffectiveDate FROM tblTaxRates, tblTaxType, tblItemsReqdTaxes WHERE (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID) AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax) AND (tblItemsReqdTaxes.ItemType = 1 ) AND (tblTaxRates.EffectiveDate)= (select max(tblTaxRates.EffectiveDate) FROM tblTaxRates WHERE (tblTaxRates.EffectiveDate <= (#12/31/2007#) AND (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID) AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax) AND (tblItemsReqdTaxes.ItemType = 1))) ORDER BY tblTaxRates.Rate DESC

Here is the output when I try to use the variables instead:

Expand|Select|Wrap|Line Numbers
  1. ELECT tblTaxType.TaxTypeID,tblTaxType.Desc,tblTaxType.TaxCode,tblTaxRates.Rate, tblTaxRates.EffectiveDate FROM tblTaxRates, tblTaxType, tblItemsReqdTaxes WHERE (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID) AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax) AND (tblItemsReqdTaxes.ItemType like 0 ) AND (tblTaxRates.EffectiveDate)= (select max(tblTaxRates.EffectiveDate) FROM tblTaxRates WHERE (tblTaxRates.EffectiveDate <= #12:00:00 AM# AND (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID) AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax) AND (tblItemsReqdTaxes.ItemType = 0))) ORDER BY tblTaxRates.Rate DESC
Jul 28 '07 #1
8 1988
puppydogbuddy
1,923 Expert 1GB
Try the syntax used below:

The lngItemCode is a numeric data type, therefore your SQl string should be formatted as a numeric string
Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = " & lngItemCode & “)"
  2.  
Your date variable is defined as a date data type, so you shouldn’t need the delimiters
Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= " & datMyDate 
  2.  
Jul 28 '07 #2
Thanks for the quick response. Let's work one problem at a time because I still cannot get either one to work.

When I changed this line:

Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
to this:
Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= " & datMyDate
the following error pops up:

Run-time error '-2147217900 (80040e14)':

Syntax error in query expression '(tleTaxRates ...rest of query...
Jul 29 '07 #3
puppydogbuddy
1,923 Expert 1GB
Thanks for the quick response. Let's work one problem at a time because I still cannot get either one to work.

When I changed this line:

Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
to this:
Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= " & datMyDate
the following error pops up:

Run-time error '-2147217900 (80040e14)':

Syntax error in query expression '(tleTaxRates ...rest of query...
Your date variable is formatted as a string;need to convert to date.
try this:
Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= " & DateValue(datMyDate)
  2.  
Jul 29 '07 #4
Maybe it would work better in a string format, but datMyDate is defined as
Expand|Select|Wrap|Line Numbers
  1. Dim datMyDate As Date
I get the same error using the DateValue(datMyDate). Also, I tried it with the "#" around the DateValue(datMyDate). That doesn't return an error, but does return 12:00AM, which I think means Access thinks datMyDate is 0, but it is not:

? datMyDate
12/31/2007
? strTxSQL
Expand|Select|Wrap|Line Numbers
  1. SELECT tblTaxType.TaxTypeID,tblTaxType.Desc,tblTaxType.TaxCode,tblTaxRates.Rate, tblTaxRates.EffectiveDate FROM tblTaxRates, tblTaxType, tblItemsReqdTaxes WHERE (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID) AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax) AND (tblItemsReqdTaxes.ItemType = 1 ) AND (tblTaxRates.EffectiveDate)= (select max(tblTaxRates.EffectiveDate) FROM tblTaxRates WHERE (tblTaxRates.EffectiveDate <= #12:00:00 AM# AND (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID) AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax) AND (tblItemsReqdTaxes.ItemType = 1))) ORDER BY tblTaxRates.Rate DESC
Jul 29 '07 #5
puppydogbuddy
1,923 Expert 1GB
try this:
1.Put parameter statement preceding your original first line of code
2. added brackets to parameter date
3. added "is not null" to where clause

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [datMyDate] DateTime;
  2. '_______________________________________________________________
  3. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate Is Not Null And tblTaxRates.EffectiveDate <= " & [datMyDate]
  4.  
Jul 29 '07 #6
Thanks very much, you helped me get on the right track! The secret is to use Parameters. I changed the following code
Expand|Select|Wrap|Line Numbers
  1. Dim cnnTax As ADODB.Connection
  2.       :
  3. Dim RsTaxRates As ADODB.Recordset
  4.       :
  5. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
  6.       :
  7. Set cnnTax = CurrentProject.Connection
  8. Set RsTaxRates = New ADODB.Recordset
  9. RsTaxRates.ActiveConnection = cnnTax
  10. RsTaxRates.Open strTxSQL
to:
Expand|Select|Wrap|Line Numbers
  1. strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= [parmMyDate]"
  2.      :
  3. Dim cmdTax As New ADODB.Command
  4. Dim RsTaxRates As New ADODB.Recordset
  5. With cmdTax
  6.           .ActiveConnection = CurrentProject.Connection
  7.           .CommandText = strTxSQL
  8.           .CommandType = adCmdUnknown
  9.           .Parameters.Append cmdTax.CreateParameter("[parmMyDate]", adDate, adParamInput, Date)
  10.           .Parameters("[parmMyDate]") = datMyDate
  11. End With
  12. Set RsTaxRates = cmdTax.Execute
and it works perfectly for the datMyDate variant. I'm sure it will work for the lngItemCode as well.
Thanks again for your help, this has stumped me for quite a while.
Jul 29 '07 #7
puppydogbuddy
1,923 Expert 1GB
You are welcome. Glad I could help you get it resolved.
Jul 29 '07 #8
missinglinq
3,532 Expert 2GB
While single lines of code don't really require the use of Code Tags, all code posted consisting of 2 or more lines does.

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

For VBA code the tag is Code=vb (no spaces) with square brackets surrounding it.

Please use [code] tags in future.

Thank you!

Linq ;0)>
Jul 29 '07 #9

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

Similar topics

3
by: Robin Tucker | last post by:
Hi, Can anyone tell me how to select the "most recent" date values from a grouped query? Consider the following: CREATE TABLE . ( NOT NULL , NOT NULL , NOT NULL ) ON This is a...
4
by: Greg Iocco | last post by:
Simple problem, but I've checked the newsgroups and couldn't find an answer. On the main swithboard is a command button to open a report. The report I built is based off a query, and the query...
5
by: MLH | last post by:
What is the meaning of this error? Operation Must Use An Updateable Query ???
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
4
by: MDW | last post by:
Hey all. I'm confused. I'm trying to add a single record into an Access 2000 database using ASP.Net. Here is the code: objConn = New OleDbConnection(strConnect) objConn.Open objCommand =...
10
by: cyber0ne | last post by:
I'm having a bit of trouble adding records to a local .DB file (using a Paradox connection string). I checked the file permissions, and for testing purposes set Everyone to Full access. But it...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
3
by: geebanga88 | last post by:
Hi i am using oracle sql developer and am making a sub query for a question. The question states: "Find Name of patients who have been treated by Dr Brian or who have had an Extended...
3
by: situ | last post by:
Hi, i'm using Db2 Version 8.2. in a stored procedure i'm assiging a the result of sql query ( with "WITH UR" ) to a variable as shown below. SET v_temp = ( SELECT 1 FROM Table_1 A ,...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
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:
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.