Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

QueryDef Problem

Status
Not open for further replies.

devGarfield

Programmer
Mar 23, 2004
31
GB
Hi All,

I have successfully created a query using CreateQueryDef which is used by my report. The problem is that sometimes there are no records to display. I would like a way to check if there is anything to display within my VBA code, so that I can display a message to the screen.

a snipette of the code is below.
Code:
    Set qdf = db.CreateQueryDef("qryTempLearners", strSql)
    Set rs = db.OpenRecordset(strSql)
    If (rs.NoMatch) Or rs.RecordCount = 0 Then
        MsgBox "No Data"
    End If

I get an error 'To few paramenters. Expected 2' The strSql is dynamically built based and using controls selected by the user.

Any ideas would be appreciated.
 
Do this instead...

Code:
    Set qdf = db.CreateQueryDef("qryTempLearners", strSql)
    Set rs = qdf.OpenRecordset

    If (rs.NoMatch) Or rs.RecordCount = 0 Then
        MsgBox "No Data"
    End If

I left your rs.NoMatch, but I am not sure it is necessary.
 
Hello Again,

I had this function working fine, but for some reason it keeps on giving me the error 'Too few Parameters". Does anybody know why this is? is it a bug within access?

Code:
Public Function NoDataCheck(inQry As String, inSql As String) As Boolean
'Check if the query actually produces anything.  Returns True or False
'Stops the reports from having #Error's displayed on it.

'inQry = name of the temp query definition
'inSql = SQL string

    NoDataCheck = True
    Set qdf = db.CreateQueryDef(inQry, inSql)
    Set rs = qdf.OpenRecordset
    
    If rs.RecordCount = 0 Then  'if the record count is 0 nothing found
        NoDataCheck = False
    End If

End Function
 
You will receive the 'Too Few Parameters' error if you have a query that either has parameters that you are not assigning values in code, or if you are referencing an object on a form directly as a query's criteria when using a querydef in VBA.

Can you post an example of the SQL Statement?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top