Solved

Excel Access VB - Database pull

Posted on 2012-12-30
5
246 Views
Last Modified: 2012-12-31
I had some VB code that worked well in the past that pulled data from an Access Query into Excel.  After making a few changes in Access, my VB code doesn't appear to work any more.

     With ActiveSheet.QueryTables.Add(connection:=Array( _
        "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin; Data Source = " & dbpath & "\KPI Database_Development_BAIN.mdb; Mode=Sha" _
        , _
        "re Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet " _
        , _
        "OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;J" _
        , _
        "et OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy L" _
        , _
        "ocale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
        ), Destination:=Range("A1"))
        .CommandType = xlCmdTable
        .CommandText = Array("Territory Query")
        .Name = "KPI Database_Development"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceDataFile = dbpath
        .Refresh BackgroundQuery:=False

The last lane regarding the refresh backgroundquery is where the error occur.

The error I get is 'Run Timer Error 1004 Too Few Parameters. Expected 2"
0
Comment
Question by:Golfer219
  • 3
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 38732125
When you get too fee parameter Expected ... this means that you maybe mispelled a field name in your querry and SQL interpret this not as a field but as a parameter and as you did not provide any parameter input then you get this error. the 2 means 2 occurences of this happens.

So you need to ask yourself:
1) Did you change anything in the querry since it was working ?
2) Did you copy it or typed it so it is getting these conflicting results ?
3) Did you change or any field in the database was changed/renamed since it was first working ?

Let me know so we can troubleshoot this further.
gowflow
0
 

Author Comment

by:Golfer219
ID: 38732557
Thanks for helping gowflow!

I did make a few minor changes to field names in the table/query in Access, however I thought that since I only referenced the query name in the VB code, that everything should be OK.  I changed 2 field names in the table and thus the query.

I've included VB code in the past where the code references specific query tables and fields and realized that the code would need to be updated if a field changed, but wasn't aware that there was something I'd need to take into account using the code above.

Any help around this would be greatly appreciated and valued!
0
 
LVL 29

Expert Comment

by:gowflow
ID: 38732595
yes easy look at your querry and change in that querry the name fields to be the new name as opposed to the old ones. What you posted here refer to the querry name but we don't have the actual querry which should be a SELECT statment or INSERT INTO or UPDATE ... or whatever if you don't know how simply post the querry here and I can help you with it.
gowflow
0
 

Author Comment

by:Golfer219
ID: 38732640
Thanks for making me realize what the issue was gowflow!

I forgot that when you make a change to a table or query in Access, you need to open the query once to show you the results before trying to run the code.  Once I opened the query, closed it and exited out of Access, everything worked.

Thanks for your help!
0
 
LVL 29

Expert Comment

by:gowflow
ID: 38732992
Glad I could help you. Meantime have a Happy new year 2013 and all the best for you.
gowflow
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 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