Solved

VB-script in Excel -  creating QueryTables

Posted on 2002-03-13
3
478 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 51
VBA to copy paste columns form one file to other 20 86
VB6 Compile Compatibility Issue 4 90
Excel - Save a copy of work book 13 83
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now