Compile error Type mismatch (1 Viewer)

detrie

Registered User.
Local time
Today, 16:50
Joined
Feb 9, 2006
Messages
113
I am trying to count the occurrences of the LEFT 6 characters on my form field.

This code throws a "Compile Error: Type mismatch" on intDailyCount =rs I cant seem to figure it out

Code:
Private Sub Command79_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim intDailyCount As Integer
Dim sqlRoot As String

Set db = CurrentDb

sqlRoot = "SELECT tbl_SampleGroups.GroupNumber, Count(tbl_Samples.GroupID) AS CountOfGroupID " & _
        "FROM tbl_Samples INNER JOIN tbl_SampleGroups ON tbl_Samples.GroupID = tbl_SampleGroups.SampleGroupID " & _
        "GROUP BY tbl_SampleGroups.GroupNumber " & _
        "HAVING (((tbl_SampleGroups.GroupNumber)=Left([Forms]![frm_Login]![GroupNumber],6)));"

Set rs = db.OpenRecordset("sqlRoot")


intDailyCount = rs
Me.txtSampleCount = intDailyCount

      
Set rs = Nothing

End Sub
 

MarkK

bit cruncher
Local time
Today, 14:50
Joined
Mar 17, 2004
Messages
8,199
A couple of changes you need to make...
Code:
[COLOR="Green"]'remove the quotes around sqlRoot[/COLOR]
Set rs = db.OpenRecordset(sqlRoot)

[COLOR="green"]'rs is a recordset, and has a fields collection, you need to use a field, maybe...[/COLOR]
intDailyCount = rs![COLOR="Blue"]CountOfGroupID[/COLOR]
See how the field name in blue is a field in your SQL?
 

detrie

Registered User.
Local time
Today, 16:50
Joined
Feb 9, 2006
Messages
113
Ok.. that makes sense. CountofGoupID doesn't need to exist on form does it? (easy to add if it does)
Also I think my syntax is still off. I am getting a Run-Time error '3061': Too few parameters. Expected 1 on set rs
Code:
Set rs = db.OpenRecordset(sqlRoot)
 

MarkK

bit cruncher
Local time
Today, 14:50
Joined
Mar 17, 2004
Messages
8,199
There's an unrecognized identifier in your SQL. Debug.print the value of the sqlRoot variable to the immediate pane, copy that SQL text to a new test query, and test run it in the designer.
Code:
debug.print sqlRoot
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:50
Joined
Aug 30, 2003
Messages
36,140
OpenRecordset won't be able to resolve the form reference, you'll need to concatenate it, like:

"HAVING (((tbl_SampleGroups.GroupNumber)=Left(" & [Forms]![frm_Login]![GroupNumber] & ",6)));"
 

MarkK

bit cruncher
Local time
Today, 14:50
Joined
Mar 17, 2004
Messages
8,199
Oh, that makes sense. Thanks Paul.
 

detrie

Registered User.
Local time
Today, 16:50
Joined
Feb 9, 2006
Messages
113
Ok I would have never found that. Thanks Paul

Things seem to run on one record but I get a Run-Time error: '3021': no current record here

Code:
intDailyCount = rs!CountOfGroupID

If I comment out
Code:
'Set rs = Nothing
it will run on all records. Is it required?

I need to revisit the SQL. It only works when the value is exactly 6 characters instead
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:50
Joined
Aug 30, 2003
Messages
36,140
Mark appears to be offline. You'll get that error when the recordset doesn't return any records. You can avoid it with:

Code:
If Not rs.EOF Then
  intDailyCount = rs!CountOfGroupID
Else
  MsgBox "No records found"
End If
 

detrie

Registered User.
Local time
Today, 16:50
Joined
Feb 9, 2006
Messages
113
Thanks Paul,
Do you see anything in my now concatenated form reference that is preventing the LEFT 6 function from working?
 

detrie

Registered User.
Local time
Today, 16:50
Joined
Feb 9, 2006
Messages
113
Paul and Mark. Thank you for your help with this. Because of the Debug.Print statement, I was able to see that I was trying to match the LEFT 6 of the form value against the entire 10 character of the table value instead of the LEFT 6.

Thank you again for your help and suggestions

Here is the revised working code
Code:
Private Sub Command79_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Dim intDailyCount As Integer
Dim sqlRoot As String

Set db = CurrentDb

Me.txtSampleCount = ""

sqlRoot = "SELECT Left([tbl_SampleGroups].[GroupNumber],6) AS GroupNo, Count(tbl_Samples.GroupID) AS CountOfGroupID " & _
        "FROM tbl_Samples LEFT JOIN tbl_SampleGroups ON tbl_Samples.GroupID = tbl_SampleGroups.SampleGroupID " & _
        "GROUP BY Left([tbl_SampleGroups].[GroupNumber],6) " & _
        "HAVING (((Left([tbl_SampleGroups].[GroupNumber],6))=Left(" & [Forms]![frm_Login]![GroupNumber] & ",6)));"

'Debug.Print sqlRoot

Set rs = db.OpenRecordset(sqlRoot)

If Not rs.EOF Then
  intDailyCount = rs!CountOfGroupID
Else
  MsgBox "No records found"
End If

Me.txtSampleCount = intDailyCount

Set rs = Nothing

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:50
Joined
Aug 30, 2003
Messages
36,140
Mark was happy to help and I was happy to butt in. :p
 

MarkK

bit cruncher
Local time
Today, 14:50
Joined
Mar 17, 2004
Messages
8,199
Teamwork makes the dream work! :)
 

Users who are viewing this thread

Top Bottom