Excel passing parameters to Access for queries

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I've got a piece of code in Excel which links to an Access database and runs queries in it.
Some of the queries need parameters which are being passed to it using this piece of code:-
Code:
If param1 <> "" Then cmd.Parameters(0) = param1

The query I'm running requires 1 parameter which is contained in param1 (and I've stepped through the code to make sure it's there).

When I try to pass the parameter, I get the following error message and the code stops:-
Item cannot be found in the collection corresponding to the requested name or ordinal

If I miss out the the line of code above (leaving the parameter empty) and try to execute the query, I get the following error message:-
Too few parameters. Expected 1

It looks like I need to pass the parameter, but when I do it isn't accepted.

Anyone any ideas?
 
Norie,
There's nothing in the Parameters collection (but there should be) - that's the whole problem. :)
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Thanks for all the help so far, if you were fairly local there'd be :beerchug: aplenty for you.

This is the code I'm using for checking the parameter counts:-
Code:
Sub test_data()
    db_pass = ""
    Set twb = ThisWorkbook
    Application.ScreenUpdating = True
'----- Start setting up objects, set up database name/location -----
    Set querydata = Worksheets("Query_List1")
    querydata.Columns("D:D").ClearContents
    Set rngData = querydata.Range("A2")
    Set member_sheet = Worksheets("Member_sheet")
    Set member_list = member_sheet.Range("A3")
    file = querydata.Range("F2")
    Set db = Workspaces(0).OpenDatabase(file, False, False, "pwd=password;")
    Application.ScreenUpdating = False
    On Error GoTo 0
    While rngData.Value <> ""
        Set query = db.QueryDefs(rngData.Value)
        paramcount = query.Parameters.Count
        rngData.Offset(0, 4) = paramcount
        Set rngData = rngData.Offset(1)
        UserForm1.Repaint
    Wend
    Set query = Nothing
    db.Close
    Set db = Nothing
End Sub

It's telling me that the ODBC call failed on the file name.
I'm doing something wrong the the Set db line.

Any ideas?
 
Upvote 0
Do you have a password on the database? If so, is it a db password, or workgroup security or what?
 
Upvote 0
It's a database password (password to open). The database is also encrypted if it makes any difference.

::edit::
Solved it. I needed a semi-colon just before pwd.
I'm also getting the right number of parameters for each query now.
 
Last edited:
Upvote 0
Your connect part needs to look like this then:
Code:
Connect:="MS Access;pwd=password"
 
Upvote 0
IT WORKS!!!!! :biggrin:

I feel I must :pray: before your knowledge.

Have a theoretical :beerchug: and my thanks for all your help and patience.
 
Upvote 0
Glad to help.
Theoretical beer is about all I can cope with at the moment... :)
 
Upvote 0
Can I be cheeky and ask just one more question?
Can I make a parameter in a query optional?

I'm talking about the usable C4CIDs query where it now accepts one parameter.
If I wanted the entire list, is it possible to bypass the parameter bit or pass it something so it collects everything?
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,290
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top