Yes...

By Onur Tosyali

Access VBA Open Parametrized Query As Recordset

In Access, you can open recordsets for a parametrized query only this way:

Dim qdef As New QueryDef
Dim rs As Recordset
qdef.SQL = "SELECT Users.* FROM Users WHERE Users.Usertype= '" + usercombo.Value + "' AND Users.Password= '" + passbox.Value + "';"
Set rs = CurrentDb.OpenRecordset(qdef.SQL)

where you inject your parametries instantaneously hardcoded. The reason is, it won't allow vba to populate parameters AND open a recordset. VBA will still ask you to fill parameters, or it will prompt "Too few parameters" error. So you have to inject parametries into your actual SQL, if you need a recordset.

If you do not need a recordset and just need Query output into a datasheet etc, you can use your regular ways, as such:

Populate your parameters like:
Dim qdef As New QueryDef
set qdef = CurrentDb.QueryDefs("PasswordCheck")
qdef.Parameter("UserName") = usercombo.Value
qdef.OpenQuery


or use form object links that will populate automatically, when you code your SQL as:
SELECT Users.* FROM Users WHERE Users.Usertype=Forms!LoginForm!usercombo

where you have a combobox named usercombo in a form named LoginForm.

So the only way you can open a recordset for a parametrized query is as the first code, where you inject the values instantaneously.

I guess that's another problem in MS Access, so don't freak out trying.

No comments:

Post a Comment