Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 75
Default Variable passed to database query

We have database which start with name as clientTrans and suffixed with years
... e.g. 2000,2001,2002,2003....and for the yer it only clientTrans (without
suffixing year). So year is passed through a variable, surprissingly, the
following codes runs properly (when year is "" NULL). If value is passed to
a variable as 2006 or 2007 it does not run and flashes an error "type
mismatch".

The extract of codes are as under;




Range("J14").Select
FYEAR = ActiveCell.Value
Range("J16").Select
tyear = ActiveCell.Value
Range("K14").Select
FROMYEAR = ActiveCell.Value
Range("K16").Select
TOYEAR = ActiveCell.Value
If FYEAR = 2008 Then
DATAYEAR = ""
Else
DATAYEAR = FYEAR
End If


ODBC details....

.CommandText = Array( _
"SELECT ClientTrans" & DATAYEAR & ".ClTransClient, ClientTrans"
& DATAYEAR & ".ClTransScripName, ClientTrans" & DATAYEAR & ".ClTransDate,
ClientTrans" & DATAYEAR & ".ClTrans, ClientTrans" & DATAYEAR &
".ClTransAllot, ClientTrans" & DATAYEAR & ".ClTransRate, ClientTrans" &
DATAYEAR & ".ClTransMktRate, ClientTrans" & DATAYEAR & "" _
, _
".ClTransNetValue, ClientTrans" & DATAYEAR & ".ExchCode" &
Chr(13) & "" & Chr(10) & "FROM institution.dbo.ClientTrans" & DATAYEAR & "
ClientTrans" & DATAYEAR & "" & Chr(13) & "" & Chr(10) & "WHERE (ClientTrans"
& DATAYEAR & ".ClTransDate= " & FROMYEAR & " And ClientTrans" & DATAYEAR &
".ClTransDate<= " & TOYEAR & " ) AND (ClientTrans" & DATAYEAR &
".ClTransClient= '" & ccode & "')" & Chr(13) & "" & Chr(10) & "ORDER BY
ClientTrans" & DATAYEAR & ".ClTransDate" _
)

In short, unable to run macro with a variable passed but when variable is ""
it works. Can anybody help me to resolve this problem

Thank you in anticipation.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,081
Default Variable passed to database query

I don't immediately see any issues with this. The string seems to evaluate
to a valid SQL statement with DATAYEAR = "" or =2008


You could vastly simplify the VBA by using a shorter table alias, like so,
where instead of "ClientTrans" & datayear as the table alias I use "CT"

dim strSQL as string

strSql = "SELECT CT.ClTransClient, CT.ClTransScripName, CT.ClTransDate, " & _
"CT.ClTrans, CT.ClTransAllot, CT.ClTransRate, CT.ClTransMktRate, " & _
"CT.ClTransNetValue, CT.ExchCode" & Chr(13) & "" & _
Chr(10) & "FROM institution.dbo.ClientTrans" & datayear & " CT " & _
"WHERE (CT.ClTransDate= " & fromYear & " And CT.ClTransDate<= " &
toYear & ") AND " & _
"(CT.ClTransClient= '" & ccode & "') " & "ORDER BY CT.ClTransDate"


..CommandText = strSQL


"Vijay Kotian" wrote:

We have database which start with name as clientTrans and suffixed with years
.. e.g. 2000,2001,2002,2003....and for the yer it only clientTrans (without
suffixing year). So year is passed through a variable, surprissingly, the
following codes runs properly (when year is "" NULL). If value is passed to
a variable as 2006 or 2007 it does not run and flashes an error "type
mismatch".

The extract of codes are as under;




Range("J14").Select
FYEAR = ActiveCell.Value
Range("J16").Select
tyear = ActiveCell.Value
Range("K14").Select
FROMYEAR = ActiveCell.Value
Range("K16").Select
TOYEAR = ActiveCell.Value
If FYEAR = 2008 Then
DATAYEAR = ""
Else
DATAYEAR = FYEAR
End If


ODBC details....

.CommandText = Array( _
"SELECT ClientTrans" & DATAYEAR & ".ClTransClient, ClientTrans"
& DATAYEAR & ".ClTransScripName, ClientTrans" & DATAYEAR & ".ClTransDate,
ClientTrans" & DATAYEAR & ".ClTrans, ClientTrans" & DATAYEAR &
".ClTransAllot, ClientTrans" & DATAYEAR & ".ClTransRate, ClientTrans" &
DATAYEAR & ".ClTransMktRate, ClientTrans" & DATAYEAR & "" _
, _
".ClTransNetValue, ClientTrans" & DATAYEAR & ".ExchCode" &
Chr(13) & "" & Chr(10) & "FROM institution.dbo.ClientTrans" & DATAYEAR & "
ClientTrans" & DATAYEAR & "" & Chr(13) & "" & Chr(10) & "WHERE (ClientTrans"
& DATAYEAR & ".ClTransDate= " & FROMYEAR & " And ClientTrans" & DATAYEAR &
".ClTransDate<= " & TOYEAR & " ) AND (ClientTrans" & DATAYEAR &
".ClTransClient= '" & ccode & "')" & Chr(13) & "" & Chr(10) & "ORDER BY
ClientTrans" & DATAYEAR & ".ClTransDate" _
)

In short, unable to run macro with a variable passed but when variable is ""
it works. Can anybody help me to resolve this problem

Thank you in anticipation.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
database query not showing foxpro database How I import data mangat New Users to Excel 1 June 24th 07 03:31 PM
Importing Data via Web Query - Can values be passed to query? [email protected] Excel Discussion (Misc queries) 5 May 9th 06 06:21 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 1 November 29th 05 01:44 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 0 November 28th 05 06:37 PM
How to use a Access Query that as a parameter into Excel database query Karen Middleton Excel Discussion (Misc queries) 1 December 13th 04 07:54 PM


All times are GMT +1. The time now is 06:12 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"