Solved

VB-script in Excel -  creating QueryTables

Posted on 2002-03-13
3
480 Views
Last Modified: 2008-02-01
Hi!

I have recorded a macro in Excel which I need some help editing. The code:

Sub QueryOffer()

Dim BTdir As String


'--- Find Correct Path For Excel Workbook ---
    If Right(ActiveWorkbook.Path, 1) <> "\" Then
        xlwbpath = ActiveWorkbook.Path & "\"
    Else
        xlwbpath = ActiveWorkbook.Path
    End If

BTdir = xlwbpath & "BT2002.mdb"

    With Sheet14.QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DSN=MS Access Database;DBQ=V:\BT2002\BT2002.mdb;DriverId=25;FIL=MS Access;" _
        ), Array("MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("A1"))
       
       
        .CommandText = Array( _
        "SELECT OffHeader.Offer, OffHeader.CustArticle, OffHeader.IntArticle, OffHeader.Date, " & _
        "OffHeader.MadeBy, OffHeader.CustomerName, OffHeader.DrawingNo, OffHeader.Kode" & Chr(13) & "" & Chr(10) & "FROM `" & BTdir & "`.OffHeader OffHeader")
       
        .Name = "Query from MS Access Database"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
    Cells.Select
    Selection.AutoFilter
    Range("A2").Select

End Sub

Where it say: DBQ=V:\BT2002\BT2002.mdb I want the DBQ to point to BTdir instead of the written path.
Is this possible? Can the code be written in another way maybe ?

please help!

/Ecmil

0
Comment
Question by:Ecmil
3 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 50 total points
ID: 6861770
Hi Ecmill,

This will do i guess

With Sheet14.QueryTables.Add(Connection:=Array(Array( _
       "ODBC;DSN=MS Access Database;DBQ=" & BTDir & ";DriverId=25;FIL=MS Access;" _
       ), Array("MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("A1"))

Just replace the mdb string with the btdir string

HTH:O)Bruintje
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6862446
From where sheet14 is comming from?
0
 

Author Comment

by:Ecmil
ID: 6862496
I tried to put in BTdir before and it didn't work but when I see your code, I see that I missed the " " around BTdir.

Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 69
MS Access - Capture pressed key onclick 4 41
VBS file using code from 2nd file (txt or vbs) 4 42
MsgBox 4 68
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question