Type mismatch (Error 13)

PAMPEREDBECCA

New member
Local time
Today, 16:15
Joined
Aug 6, 2000
Messages
9
I am getting a type mismatch (Error 13) due to the following funciton. Can anyone enlighten me? Thanks in advance!

Public Function RandomValue() As Integer
Dim RndRecord As String
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("tableteams")
RndRecord = Int((rst.RecordCount * Rnd) + 1)
rst.Move RndRecord

RandomValue = rst.Fields(0)


End Function
 
Thank you for responding! You were right, my field is a Long Integer, I changed my code to the following

Option Compare Database
Option Explicit

Public Function RandomValue() As Long
Dim RndRecord As String
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("tableteams")
RndRecord = Int((rst.RecordCount * Rnd) + 1)
rst.Move RndRecord

RandomValue = rst.Fields(0)


End Function

and it still does not work. When I test the code, I still get the Type mismatch error. Then when I click DeBug, the following line

Set rst = CurrentDb.OpenRecordset("tableteams")


is highlighted. I feel like it should be something simple but I am not getting it. I appreciate your help because I am new to this part of Access. Until now I have gotten away with SQL, queries, and macros.
 
Shouldn't RndRecord also be declared as an Integer?

You Have:
Dim RndRecord As String

Try:
Dim RndRecord As Integer

HTH
RDH

[This message has been edited by R. Hicks (edited 08-07-2000).]
 
Thanks for responding!

I think this is what you are talking about.

Public Function RandomValue() As Integer
Dim RndRecord As Integer
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("tableteams")
RndRecord = Int((rst.RecordCount * Rnd) + 1)
rst.Move RndRecord

RandomValue = rst.Fields(0)

End Function

It gives me the same error. Do you think it is because I am not selecting a specific field in the table "tableteams". If this could be it, can you help with the syntax of that statement?

Thanks in advance!
 
I think that the 'Int' should be around the recordset field, rather than the whole bit, and also use 'CInt' instead of 'Int' i.e.,

RndRecord = (Cint(rst.RecordCount) * Rnd) + 1

Cint converts the expression to an integer type.
 
I think that the 'Int' should be around the recordset field, rather than the whole bit, and also use 'CInt' instead of 'Int' i.e.,

RndRecord = (Cint(rst.RecordCount) * Rnd) + 1

Cint converts the expression to an integer type.
 
Due to a version conflict, when Microsoft DAO 3.6 Object Library is checked under Tools/References, Access does not recognize some DAO objects from previous versions.

Try:
Dim rs As DAO.RecordSet

instead of,
Dim rs As RecordSet
 

Users who are viewing this thread

Back
Top Bottom