Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB-script in Excel -  creating QueryTables

Posted on 2002-03-13
3
Medium Priority
?
496 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

824 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