Excel Access VB - Database pull

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"
Golfer219Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gowflowConnect With a Mentor Commented:
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
 
Golfer219Author Commented:
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
 
gowflowCommented:
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
 
Golfer219Author Commented:
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
 
gowflowCommented:
Glad I could help you. Meantime have a Happy new year 2013 and all the best for you.
gowflow
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.