View Full Version : Obtaining a value from a QueryDef


aziz rasul
08-23-2007, 05:53 AM
I have the following code: -

Dim strSQL As String
Dim qdf As DAO.QueryDef
Dim intModelCodes As Integer
Dim strMin As String
Dim strMax As String
Dim fld As Field

If Not IsNull(Me.MasterLicenseeCode) And Not IsNull(Me.cboDescriptor) And Not IsNull(Me.cboType) Then
strSQL = "SELECT tblProducts.ModelCode FROM tblProducts WHERE (((tblProducts.ProductIDprod) Like '" & Me.MasterLicenseeCode & "-" & Me.cboType & Me.cboDescriptor & "*'));"
Call DeleteQuery("qryModelCodes")
Set qdf = CurrentDb.CreateQueryDef("qryModelCodes", strSQL)
intModelCodes = DCount("[ModelCode]", "qryModelCodes")
qdf.SQL = "SELECT Min(tblProducts.ModelCode) AS MinOfModelCode, Max(tblProducts.ModelCode) AS MaxOfModelCode " & _
"FROM tblProducts " & _
"HAVING (((tblProducts.ProductIDprod) Like '" & Me.MasterLicenseeCode & "-" & Me.cboType & Me.cboDescriptor & "*'));"
For Each fld In qdf.Fields
If fld.Name = "MinOfModelCode" Then
strMin = fld.Value 'Run time error 3219 - Invalid operation
ElseIf fld.Name = "MaxOfModelCode" Then
strMax = fld.Value
End If
Next fld
Me.txtMin = strMin
Me.txtMax = strMax
If intModelCodes = 0 Then
Me.txtModelCodes = "There are no models for " & Me.MasterLicenseeCode & "-" & Me.cboType & Me.cboDescriptor
ElseIf intModelCodes = 1 Then
Me.txtModelCodes = "There is " & intModelCodes & " model for " & Me.MasterLicenseeCode & "-" & Me.cboType & Me.cboDescriptor
ElseIf intModelCodes > 1 Then
Me.txtModelCodes = "There are " & intModelCodes & " models for " & Me.MasterLicenseeCode & "-" & Me.cboType & Me.cboDescriptor
End If
'CurrentDb.QueryDefs.Delete qdf.Name
Else
Me.txtModelCodes = ""
End If


Does anyone know why I'm getting the error?

KeithG
08-23-2007, 06:01 AM
Are you trying to retrieve a value from the results of the query? IF so you need to run the query by creating a recordset.

aziz rasul
08-23-2007, 06:18 AM
Yes. I thought I could do that without creating a recordset! I will try doing that. It would be interesting to know whether it was possible to obtain the value as I was trying to do.

WayneRyan
08-23-2007, 09:39 AM
Aziz,

No, you can only retrieve field attributes from the QueryDef, no data.

You'll have to use the QueryDef to open a recordset.

Wayne

aziz rasul
08-24-2007, 01:04 AM
OK, I was beginning to realise that.