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? - strTxSQL = "SELECT tblTaxType.TaxTypeID,tblTaxType.Desc,tblTaxType.TaxCode,tblTaxRates.Rate,"
-
strTxSQL = strTxSQL & " tblTaxRates.EffectiveDate"
-
strTxSQL = strTxSQL & " FROM tblTaxRates, tblTaxType, tblItemsReqdTaxes"
-
strTxSQL = strTxSQL & " WHERE (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID)"
-
strTxSQL = strTxSQL & " AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax)"
-
'THIS LINE WILL NOT WORK strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType like " & lngItemCode & " )"
-
'USED NEXT LINE INSTEAD
-
strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = 1 )"
-
strTxSQL = strTxSQL & " AND (tblTaxRates.EffectiveDate)="
-
strTxSQL = strTxSQL & " (select max(tblTaxRates.EffectiveDate)"
-
strTxSQL = strTxSQL & " FROM tblTaxRates"
-
'THIS LINE WILL NOT WORK strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
-
'USED NEXT LINE INSTEAD
-
strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= (#" & Forms!frmReservation!ArrivalDate + intInvcCtr & "#)"
-
strTxSQL = strTxSQL & " AND (tblTaxRates.TaxTypeID = tblTaxType.TaxTypeID)"
-
strTxSQL = strTxSQL & " AND (tblTaxRates.TaxTypeID=tblItemsReqdTaxes.ReqdTax)"
-
'THIS LINE WILL NOT WORK strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = " & lngItemCode & ")))"
-
'USED NEXT LINE INSTEAD
-
strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = 1)))"
-
strTxSQL = strTxSQL & " ORDER BY tblTaxRates.Rate DESC"
-
-
intInvcCtr = 0
-
intLengthOfStay = Forms!frmReservation!DepartureDate - Forms!frmReservation!ArrivalDate
-
For intInvcCtr = 0 To intLengthOfStay - 1
-
'================================================================
-
' set datMyDate
-
'================================================================
-
datMyDate = Format(DateAdd("d", intInvcCtr, Forms!frmReservation!ArrivalDate), "MM/DD/YYYY")
-
'================================================================
-
' Load the room information in the RsRoomRates recordset
-
'================================================================
-
Set cnnRoom = CurrentProject.Connection
-
Set RsRoomRates = New ADODB.Recordset
-
RsRoomRates.ActiveConnection = cnnRoom
-
RsRoomRates.Open strRoomRatesSQL
-
strItemCode = RsRoomRates.Fields(1).Value
-
lngItemCode = RsRoomRates.Fields(0).Value
-
'================================================================
-
' Load the tax types and tax rates in the RsTaxRates recordset
-
'================================================================
-
Set cnnTax = CurrentProject.Connection
-
Set RsTaxRates = New ADODB.Recordset
-
RsTaxRates.ActiveConnection = cnnTax
-
RsTaxRates.Open strTxSQL
-
Debug.Print strTxSQL
-
:
-
:
-
Next
Here is the "Debug.Print strTxSQL" output from the immediate window when I run it as shown above. - 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: - 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
8 1988
Try the syntax used below:
The lngItemCode is a numeric data type, therefore your SQl string should be formatted as a numeric string -
strTxSQL = strTxSQL & " AND (tblItemsReqdTaxes.ItemType = " & lngItemCode & “)"
-
Your date variable is defined as a date data type, so you shouldn’t need the delimiters -
strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= " & datMyDate
-
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: - strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
to this: - 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...
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: - strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
to this: - 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: - strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= " & DateValue(datMyDate)
-
Maybe it would work better in a string format, but datMyDate is defined as
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 - 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
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 -
PARAMETERS [datMyDate] DateTime;
-
'_______________________________________________________________
-
strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate Is Not Null And tblTaxRates.EffectiveDate <= " & [datMyDate]
-
Thanks very much, you helped me get on the right track! The secret is to use Parameters. I changed the following code - Dim cnnTax As ADODB.Connection
-
:
-
Dim RsTaxRates As ADODB.Recordset
-
:
-
strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= #" & datMyDate & "#"
-
:
-
Set cnnTax = CurrentProject.Connection
-
Set RsTaxRates = New ADODB.Recordset
-
RsTaxRates.ActiveConnection = cnnTax
-
RsTaxRates.Open strTxSQL
to: - strTxSQL = strTxSQL & " WHERE (tblTaxRates.EffectiveDate <= [parmMyDate]"
-
:
-
Dim cmdTax As New ADODB.Command
-
Dim RsTaxRates As New ADODB.Recordset
-
With cmdTax
-
.ActiveConnection = CurrentProject.Connection
-
.CommandText = strTxSQL
-
.CommandType = adCmdUnknown
-
.Parameters.Append cmdTax.CreateParameter("[parmMyDate]", adDate, adParamInput, Date)
-
.Parameters("[parmMyDate]") = datMyDate
-
End With
-
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.
You are welcome. Glad I could help you get it resolved.
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)> Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: MLH |
last post by:
What is the meaning of this error?
Operation Must Use An Updateable Query
???
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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 ,...
|
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...
|
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:
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: 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...
|
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,...
|
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...
| |