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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does Access AutoNumber mismatch with VB?

Status
Not open for further replies.

MarcMellor

Programmer
Jan 12, 2002
34
0
0
US
I need to download some questions from an Access database by selecting according to certain pre-chosen parameters and then pick one of these at random. QuestionID is the Primary Key AutoNumber in Access, RowNumber is the VB equivalent, QuestionLimit is the full count of questions under the selection, NumberOfSteps is my randomised number and Text1 is linked to the QuestionID field in Access.
This is my code:

Dim RowNumber As Long
Dim QuestionLimit As Integer
Dim NumberOfSteps As Integer
Randomize
Data1.RecordSource = "SELECT * FROM QuestionsDatabase_
WHERE Subject = 'Biology'
Data1.Refresh
QuestionLimit = Data1.Recordset.RecordCount
Dati.Refresh
NumberOfSteps = Int(Rnd * QuestionLimit) + 1
Data1.RecordSource.Move NumberOfSteps
RowNumber = Text1.Text
Data1.RecordSource = "SELECT * FROM QuestionsDatabase_
WHERE QuestionID = " & "'" & RowNumber & "'"
Data1.Refresh

This generates a "Run-time error 3464, Data type mis-match in criteria expression" and the last Data1 Refresh is highlighted by the debugger. I changed my Access QuestionID Field to Text (and Dim'ed in VB to suit) and it worked fine. But to work out code to replace the automatic action of AutoNumber in Access seems crazy. There must be a better way ........?
 
Since the autonumber field is a numeric type, you do not surround it with the apostrophe.

Data1.RecordSource = "SELECT * FROM QuestionsDatabase_
WHERE QuestionID = " & RowNumber
David Paulson

 
Oops! Now I understand why all this complicated use of ' and " and & was needed! Unfortunately I'm still having a problem. My Data1.RecordSource Move NumberOfSteps is intended to put the QuestionID into a TextBox whose value can then be picked up with my RowNumber variable. I always get RowNumber as 2 even though I am getting suitably random NumberOfSteps. Ive got a feeling that the TextBox is not fast enough or something. Is there a way of setting the RowNumber variable to the QuestionID number after it has moved through the selected recordset the random number of steps in a more direct way? I lookedat the GetRows method but I can't quite understand how to set it up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top