#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Make query

Hi All,

I have problem with query acces. I have created query from acces query
design. But I got difficult to implemented this query to vba macro.


SELECT tabel2.tgl, tabel2.num, tabel1.dates,
abs(tabel2.tgl-tabel1.dates) AS ddif
FROM tabel2 INNER JOIN tabel1 ON tabel2.site = tabel1.site;

this query i save with name query1

SELECT query1.dates, min(ddif) AS [min]
FROM Query1
GROUP BY query1.dates;

that query i save with name query2

how to implemented that query to vba macro. tabel2 should be variable.

(tabel2 must be changed to tabel1,tabel2, tabel3 etc...) ?

Thanks

Ardi

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Make query

You can use a subquery in SQL like this:
SELECT dates, min(ddif) AS [min]
FROM
(SELECT tabel2.tgl, tabel2.num, tabel1.dates as dates,
abs(tabel2.tgl-tabel1.dates) AS ddif
FROM tabel2 INNER JOIN tabel1 ON tabel2.site = tabel1.site)
GROUP BY dates

And, because you don't use tgl or num in your results you could leave them
out:
SELECT dates, min(ddif) AS [min]
FROM
(SELECT tabel1.dates as dates, abs(tabel2.tgl-tabel1.dates) AS ddif
FROM tabel2 INNER JOIN tabel1 ON tabel2.site = tabel1.site)
GROUP BY dates

You can go into MSQuery (Data... Get External Data... New Database Query)
and from the MSQuery window (not the Query Wizard!) press the "SQL" toolbar
button. You should be able to manually type (or copy/paste) the SQL command
into the SQL window. Then return the data to Excel. This gives you the
basic query for tabel2.

To change the table name being used: you can set up a macro that alters the
QueryTable.CommandText. The following example will assume that the table
name you wish to use is in Cell A1 on Sheet "Sheet1":
Sub RunQuery(TableName as String)
Dim OriginalSQL as String, NewSQL as String
With Worksheets("SheetName").QueryTables(1)
OriginalSQL = .CommandText ' Store the original SQL statement with
'tabel2'
' Now modify the SQL command to use the table specified in Sheet1 cell A1:
NewSQL = Replace(OriginalSQL, "tabel2", Sheets("Sheet1").Range("A1").Text)
.CommandText = NewSQL
' Refresh the query:
.Refresh
' Restore the SQL so it again refers to 'tabel2';
' Otherwise we cannot easily find and replace the table name next time
it is run
.CommandText = OriginalSQL
End With
End Sub
--
- K Dales


"ardi" wrote:

Hi All,

I have problem with query acces. I have created query from acces query
design. But I got difficult to implemented this query to vba macro.


SELECT tabel2.tgl, tabel2.num, tabel1.dates,
abs(tabel2.tgl-tabel1.dates) AS ddif
FROM tabel2 INNER JOIN tabel1 ON tabel2.site = tabel1.site;

this query i save with name query1

SELECT query1.dates, min(ddif) AS [min]
FROM Query1
GROUP BY query1.dates;

that query i save with name query2

how to implemented that query to vba macro. tabel2 should be variable.

(tabel2 must be changed to tabel1,tabel2, tabel3 etc...) ?

Thanks

Ardi


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
Make table query will work in datasheet view but will not make tab WildlyHarry Excel Discussion (Misc queries) 0 August 28th 07 03:06 PM
make column lists for select query. kang New Users to Excel 1 July 16th 07 01:32 PM
How do i make excel open a new workbook and run a query from sql Dhruv Sampat Excel Programming 3 November 15th 05 01:39 PM
Make Table Query limitation quartz[_2_] Excel Programming 2 September 30th 05 01:26 PM
Make table query works intermittently quartz[_2_] Excel Programming 2 September 14th 05 09:16 PM


All times are GMT +1. The time now is 04:18 PM.

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"