简体   繁体   中英

MS ACCESS vba - error

I'm creating a MS Access report. I came across a small issue and I have no idea where it's going wrong. So what I'm trying to do is

1) Select everything from a query (query requires "TO" and "FROM" dates. I pass these values to the frmX which then gets referenced in the query). When i run a query by itself with the frmX open - it runs fine. 2) Im trying to change some values in the data 3) Insert the new values into tempTable1

Here's my code:

dim rs1 as DAO.Recordset
dim rs2 as DAO.Recordset
CurrentDb.Execute "DELETE FROM [tempProvider-Detail]"

'Repopulating temp table
DoCmd.OpenQuery "qryProvider-FINAL"

'Input Source
Set rs1 = CurrentDb.OpenRecordset("Select * from [qryProvider-Final]", , dbOpenSnapshot)

'Target Source
Set rs2 = CurrentDb.OpenRecordset("Select * from tempProvider-DETAIL", dbOpenDynamic)

What's interesting here is that it does not hang up on DoCMD.OpenQuery - however when I get to set rs1...... then it tells me that it expects 2 parameters. I don't know why - since the query already opened - and it works fine when I try opening it by itself it opens (with dates in frmX that i reference in the query).

Please help me out!

So I did this as Heinzi helped me.. still getting same error What is wrong??????

DoCmd.OpenQuery "qryProvider-FINAL"

Set qdf = CurrentDb.QueryDefs("qryProvider-FINAL")
qdf.Parameters(0) = [Forms]![frmX]![txtFrom]
qdf.Parameters(1) = [Forms]![frmX]![txtTo]
Set rs1 = qdf.OpenRecordset

strSQL = "SELECT * FROM [qryProvider-FINAL];"

'Input Source
Set rs1 = CurrentDb.OpenRecordset(strSQL, , dbOpenSnapshot) ---this is where it hangs up

Would this work:

Sub Test()

    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rst As DAO.Recordset
    Dim prm As DAO.Parameter

    Set db = CurrentDb
    Set qdf = db.QueryDefs("qryProvider-FINAL")
    For Each prm In qdf.Parameters
        prm.Value = Eval(prm.Name)
    Next prm
    Set rst = qdf.OpenRecordset

End Sub

You cannot reference form controls when opening a recordset with CurrentDb.OpenRecordset. It's just not supported. Details can be found in the following MSDN article:

The answer is that youre invoking the Jet engine in a different context here, and that makes all the difference. When you get data from a parameter query that uses a form to supply the parameter via the Access user interface, as in the earlier example, Access can evalute the expression involved and supply a value to Jet. When you get data from a parameter query that uses a form to supply the parameter via VBA, instead of through a form, the bits of Access that manage user interface matters arent involved. Consequently, Jet is passed the string "[Forms]![frmSelectCountry]![cboCountry]" instead of the value in cboCountry. Because Jet doesnt know how to evaluate the expression, it cant open the recordset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM