Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Problem in SQL Query

Hi everyone

I am trying to retrieve the data from MS Access database through ODBC query through a function given below

Sub query_gen(fproject_master, fproject_master1, fproject_master2, fmptable,
fmptable1, fpdetail, fpdetail1, fpdetail2, fpdetail3, fpdetail4, query1, query2,
query3, query4, query5, fcycle_type

direc = Worksheets("QUERY_BUILDER").Range("BH1").Valu
datab = Worksheets("QUERY_BUILDER").Range("BH2").Valu

' Sheet2.Columns("A:BA").NumberFormat = "General


With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y(
"ODBC;DSN=MS Access Database;DBQ=" + CStr(datab) +
";DefaultDir=\\phome5\common\AT\AT-Amendments;DriverId=25;"),
Array("FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=
Range("A7")


.Sql = Array("SELECT " & fproject_master & " " & fproject_master1 & " "
& fproject_master2 & " " & fmptable & " " & fmptable1 & " " & fpdetail & " "
& fpdetail1 & " " & fpdetail2 & " " & fpdetail3 & " "
& fpdetail4 & " " & fcycle_type & " FROM `" & CStr(datab) &
"`.M_P_Table M_P_Table, `" + CStr(datab) &
"`.Cycle_Type Cycle_Type, `" & CStr(datab) + "`.P_Detail P_Detail, `"
+ CStr(datab) + "`.Project_Master Project_Master"
& " WHERE M_P_Table.M_P_No = P_Detail.M_P_No AND M_P_Table.Project_No = "
& "Project_Master.Project_No AND Cycle_Type.cycle_project=P_Detail.Serial_No"
& "AND ((" & query1 & query2 & query3 & query4 & query5 & "))"

FieldNames = Tru
.RefreshStyle = xlInsertDeleteCell
.RowNumbers = Fals
.FillAdjacentFormulas = Fals
.RefreshOnFileOpen = Fals
.HasAutoFormat = Tru
.BackgroundQuery = Tru
.TablesOnlyFromHTML = Tru
.Refresh BackgroundQuery:=Fals
.SavePassword = Tru
.SaveData = False 'Will not save the dat
End Wit
End Su

Whenever I am trying to run this query
it is giving 2 errors
First it gives error on line : Sheet2.Columns("A:BA").NumberFormat = "General
the error is
Run-Time Error "1004" :unable to set the NumberFormat property of Range Class

So I commented this line

and the second error its giving type mismatch on line

..Sql = Array("SELECT " & fproject_master & " " & fproject_master1 & " "
& fproject_master2 & " " & fmptable & " " & fmptable1 & " " & fpdetail & " "
& fpdetail1 & " " & fpdetail2 & " " & fpdetail3 & " "
& fpdetail4 & " " & fcycle_type & " FROM `" & CStr(datab) &
"`.M_P_Table M_P_Table, `" + CStr(datab) &
"`.Cycle_Type Cycle_Type, `" & CStr(datab) + "`.P_Detail P_Detail, `"
+ CStr(datab) + "`.Project_Master Project_Master"
& " WHERE M_P_Table.M_P_No = P_Detail.M_P_No AND M_P_Table.Project_No = "
& "Project_Master.Project_No AND Cycle_Type.cycle_project=P_Detail.Serial_No"
& "AND ((" & query1 & query2 & query3 & query4 & query5 & "))"

It was not giving this error earlier. Its all of a sudden. What could be the possible reason of this sudden appearance of error

TI
Shilp

  #2   Report Post  
Posted to microsoft.public.excel.programming
AL AL is offline
external usenet poster
 
Posts: 37
Default Problem in SQL Query

Philps:

Troubleshooting an entire sql statement is particularly
difficult. First i would make your sql code "modular"
so that it is easier to troubleshoot. For example
I would introduce the variables SelectClause, FromClause,
& WhereClause and have them correspond to the parts of
your sql statement. For example,

..sql= Array(SelectClause & " " & Fromclause & " " &
whereclause)

Next, you can try running your statement without the
whereclause. If the statement works, you know your
problem is in the whereclause; If not, its time to move
onto the other parts of your statement.

So then i would simplify the statement, reducing it to one
table and one variable in the select statement. Then
progressively add variables and tables, running the
statement until you come up with the error.

Hope this helps.



-----Original Message-----
Hi everyone,

I am trying to retrieve the data from MS Access database

through ODBC query through a function given below:

Sub query_gen(fproject_master, fproject_master1,

fproject_master2, fmptable, _
fmptable1, fpdetail, fpdetail1, fpdetail2, fpdetail3,

fpdetail4, query1, query2, _
query3, query4, query5, fcycle_type)

direc = Worksheets("QUERY_BUILDER").Range

("BH1").Value
datab = Worksheets("QUERY_BUILDER").Range

("BH2").Value

' Sheet2.Columns("A:BA").NumberFormat = "General"


With ActiveSheet.QueryTables.Add(Connection:=Array

(Array( _
"ODBC;DSN=MS Access Database;DBQ=" + CStr

(datab) + _
";DefaultDir=\\phome5\common\AT\AT-

Amendments;DriverId=25;"), _
Array("FIL=MS

Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=
_
Range("A7"))


.Sql = Array("SELECT " & fproject_master & " " &

fproject_master1 & " " _
& fproject_master2 & " " & fmptable & " " &

fmptable1 & " " & fpdetail & " " _
& fpdetail1 & " " & fpdetail2 & " " & fpdetail3

& " " _
& fpdetail4 & " " & fcycle_type & " FROM `" & CStr

(datab) & _
"`.M_P_Table M_P_Table, `" + CStr(datab) & _
"`.Cycle_Type Cycle_Type, `" & CStr(datab)

+ "`.P_Detail P_Detail, `" _
+ CStr(datab) + "`.Project_Master Project_Master"

_
& " WHERE M_P_Table.M_P_No = P_Detail.M_P_No AND

M_P_Table.Project_No = " _
& "Project_Master.Project_No AND

Cycle_Type.cycle_project=P_Detail.Serial_No" _
& "AND ((" & query1 & query2 & query3 & query4 &

query5 & "))")

FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = False 'Will not save the data
End With
End Sub

Whenever I am trying to run this query,
it is giving 2 errors:
First it gives error on line : Sheet2.Columns

("A:BA").NumberFormat = "General"
the error is
Run-Time Error "1004" :unable to set the NumberFormat

property of Range Class.

So I commented this line.

and the second error its giving type mismatch on line:

..Sql = Array("SELECT " & fproject_master & " " &

fproject_master1 & " " _
& fproject_master2 & " " & fmptable & " " &

fmptable1 & " " & fpdetail & " " _
& fpdetail1 & " " & fpdetail2 & " " & fpdetail3

& " " _
& fpdetail4 & " " & fcycle_type & " FROM `" & CStr

(datab) & _
"`.M_P_Table M_P_Table, `" + CStr(datab) & _
"`.Cycle_Type Cycle_Type, `" & CStr(datab)

+ "`.P_Detail P_Detail, `" _
+ CStr(datab) + "`.Project_Master Project_Master"

_
& " WHERE M_P_Table.M_P_No = P_Detail.M_P_No AND

M_P_Table.Project_No = " _
& "Project_Master.Project_No AND

Cycle_Type.cycle_project=P_Detail.Serial_No" _
& "AND ((" & query1 & query2 & query3 & query4 &

query5 & "))")

It was not giving this error earlier. Its all of a

sudden. What could be the possible reason of this sudden
appearance of error?

TIH
Shilps

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Problem in SQL Query

Note the string concatenation character in VBA code is '&' not '+'
(the latter being the C equivalent). You have a mixture of both in
your code. Using '+' with strings can have unexpected results.

--

"Al" wrote in message ...
Philps:

Troubleshooting an entire sql statement is particularly
difficult. First i would make your sql code "modular"
so that it is easier to troubleshoot. For example
I would introduce the variables SelectClause, FromClause,
& WhereClause and have them correspond to the parts of
your sql statement. For example,

.sql= Array(SelectClause & " " & Fromclause & " " &
whereclause)

Next, you can try running your statement without the
whereclause. If the statement works, you know your
problem is in the whereclause; If not, its time to move
onto the other parts of your statement.

So then i would simplify the statement, reducing it to one
table and one variable in the select statement. Then
progressively add variables and tables, running the
statement until you come up with the error.

Hope this helps.



-----Original Message-----
Hi everyone,

I am trying to retrieve the data from MS Access database

through ODBC query through a function given below:

Sub query_gen(fproject_master, fproject_master1,

fproject_master2, fmptable, _
fmptable1, fpdetail, fpdetail1, fpdetail2, fpdetail3,

fpdetail4, query1, query2, _
query3, query4, query5, fcycle_type)

direc = Worksheets("QUERY_BUILDER").Range

("BH1").Value
datab = Worksheets("QUERY_BUILDER").Range

("BH2").Value

' Sheet2.Columns("A:BA").NumberFormat = "General"


With ActiveSheet.QueryTables.Add(Connection:=Array

(Array( _
"ODBC;DSN=MS Access Database;DBQ=" + CStr

(datab) + _
";DefaultDir=\\phome5\common\AT\AT-

Amendments;DriverId=25;"), _
Array("FIL=MS

Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=
_
Range("A7"))


.Sql = Array("SELECT " & fproject_master & " " &

fproject_master1 & " " _
& fproject_master2 & " " & fmptable & " " &

fmptable1 & " " & fpdetail & " " _
& fpdetail1 & " " & fpdetail2 & " " & fpdetail3

& " " _
& fpdetail4 & " " & fcycle_type & " FROM `" & CStr

(datab) & _
"`.M_P_Table M_P_Table, `" + CStr(datab) & _
"`.Cycle_Type Cycle_Type, `" & CStr(datab)

+ "`.P_Detail P_Detail, `" _
+ CStr(datab) + "`.Project_Master Project_Master"

_
& " WHERE M_P_Table.M_P_No = P_Detail.M_P_No AND

M_P_Table.Project_No = " _
& "Project_Master.Project_No AND

Cycle_Type.cycle_project=P_Detail.Serial_No" _
& "AND ((" & query1 & query2 & query3 & query4 &

query5 & "))")

FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = False 'Will not save the data
End With
End Sub

Whenever I am trying to run this query,
it is giving 2 errors:
First it gives error on line : Sheet2.Columns

("A:BA").NumberFormat = "General"
the error is
Run-Time Error "1004" :unable to set the NumberFormat

property of Range Class.

So I commented this line.

and the second error its giving type mismatch on line:

..Sql = Array("SELECT " & fproject_master & " " &

fproject_master1 & " " _
& fproject_master2 & " " & fmptable & " " &

fmptable1 & " " & fpdetail & " " _
& fpdetail1 & " " & fpdetail2 & " " & fpdetail3

& " " _
& fpdetail4 & " " & fcycle_type & " FROM `" & CStr

(datab) & _
"`.M_P_Table M_P_Table, `" + CStr(datab) & _
"`.Cycle_Type Cycle_Type, `" & CStr(datab)

+ "`.P_Detail P_Detail, `" _
+ CStr(datab) + "`.Project_Master Project_Master"

_
& " WHERE M_P_Table.M_P_No = P_Detail.M_P_No AND

M_P_Table.Project_No = " _
& "Project_Master.Project_No AND

Cycle_Type.cycle_project=P_Detail.Serial_No" _
& "AND ((" & query1 & query2 & query3 & query4 &

query5 & "))")

It was not giving this error earlier. Its all of a

sudden. What could be the possible reason of this sudden
appearance of error?

TIH
Shilps

.

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
Problem with Web Query Albert Excel Discussion (Misc queries) 0 August 19th 09 05:23 AM
Query Problem Freshman Excel Worksheet Functions 0 September 26th 06 09:57 AM
Problem with MS Query - can't edit query jarems Excel Discussion (Misc queries) 2 December 12th 05 09:42 AM
Query problem Will Excel Discussion (Misc queries) 2 November 21st 05 03:02 PM
Problem with .Background Query option of ODBC Query Shilps Excel Programming 0 April 19th 04 06:41 AM


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

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

About Us

"It's about Microsoft Excel"